Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added a little bit more speed to LoadPosts with PLINQ #101

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 26 additions & 10 deletions src/Services/FileBlogService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,17 +195,11 @@ private void Initialize()

private void LoadPosts()
{
if (!Directory.Exists(_folder))
Directory.CreateDirectory(_folder);

// Can this be done in parallel to speed it up?
foreach (string file in Directory.EnumerateFiles(_folder, "*.xml", SearchOption.TopDirectoryOnly))
var addPost = new Action<XElement, string>((doc, id) =>
{
XElement doc = XElement.Load(file);

Post post = new Post
var post = new Post
{
ID = Path.GetFileNameWithoutExtension(file),
ID = id,
Title = ReadValue(doc, "title"),
Excerpt = ReadValue(doc, "excerpt"),
Content = ReadValue(doc, "content"),
Expand All @@ -217,7 +211,29 @@ private void LoadPosts()

LoadCategories(post, doc);
LoadComments(post, doc);

_cache.Add(post);
});

if (!Directory.Exists(_folder))
Directory.CreateDirectory(_folder);

// Yes, it can be done in parallel to speed it up
var filePaths = from path in Directory.EnumerateFiles(_folder, "*.xml", SearchOption.TopDirectoryOnly)
select path;

// Limit parallelism here if needed
var degreeOfParallelism = Environment.ProcessorCount;

var fileContents = from path in filePaths.AsParallel()
.WithDegreeOfParallelism(degreeOfParallelism)
let doc = XElement.Load(path)
let id = Path.GetFileNameWithoutExtension(path)
select new { Doc = doc, Id = id };

foreach (var item in fileContents)
{
addPost(item.Doc, item.Id);
}
}

Expand Down Expand Up @@ -285,7 +301,7 @@ private static string CleanFromInvalidChars(string input)
var r = new Regex($"[{regexSearch}]");
return r.Replace(input, "");
}

private static string FormatDateTime(DateTime dateTime)
{
const string UTC = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fff'Z'";
Expand Down