-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Use a cache for dash padding #6625
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -81,6 +81,24 @@ internal static string Padding(int countOfSpaces) | |
|
||
return result; | ||
} | ||
|
||
private const int DashCacheMax = 120; | ||
private static readonly string[] DashCache = new string[DashCacheMax]; | ||
internal static string DashPadding(int count) | ||
{ | ||
if (count >= DashCacheMax) | ||
return new string('-', count); | ||
|
||
var result = DashCache[count]; | ||
|
||
if (result == null) | ||
{ | ||
Interlocked.CompareExchange(ref DashCache[count], new string('-', count), null); | ||
result = DashCache[count]; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about change this if block to the following? You can return the new array.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From a performance point of view, it shouldn't matter - the code should be hit just once per process in the normal case. That said, returning the object from the cache is usually more desirable, and as a code pattern, I think that is reason enough to prefer the original code. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This if block may be hit 120 times per process, since the array has 120 slots. |
||
|
||
return result; | ||
} | ||
} | ||
} // namespace | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like we can leverage the new Span and Slice capabilities of C# 7.2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch! I like the idea.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TableWriter expect strings, pass them through many methods and collect them in StringCollection.
So I can not implement the idea - it requires a complete rewriting of this TableWriter and it's a big risk - seems we haven't full set tests.
I am ready to pull another PR with some optimizations in TableWriter to decrease allocations.
I suggest use the cache until we're ready to rewrite TableWriter using
Memory<char>
or something else.