-
Notifications
You must be signed in to change notification settings - Fork 1k
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
eliminate ActorPath.ToSerializationFormat
UID allocations
#6195
Changes from 1 commit
ad2ea50
25b2c78
67bef55
11932cc
9714ce0
210742b
ad15a0f
5343199
02bb271
6a9e003
8d64dc6
6ed38e3
24a0347
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,71 @@ public static int Parse(ReadOnlySpan<char> str) | |
throw new FormatException($"[{str.ToString()}] is now a valid numeric format"); | ||
} | ||
|
||
private const char Negative = '-'; | ||
private static readonly char[] Numbers = { '0','1','2','3','4','5','6','7','8','9' }; | ||
|
||
/// <summary> | ||
/// Can replace with int64.TryFormat in later versions of .NET. | ||
/// </summary> | ||
/// <param name="i">The integer we want to format into a string.</param> | ||
/// <param name="startPos">Starting position in the destination span we're going to write from</param> | ||
/// <param name="span">The span we're going to write our characters into.</param> | ||
/// <param name="sizeHint">Optional size hint, in order to avoid recalculating it.</param> | ||
/// <returns></returns> | ||
public static int TryFormat(long i, int startPos, ref Span<char> span, int sizeHint = 0) | ||
{ | ||
var index = 0; | ||
var negative = i < 0; | ||
if (i == 0) | ||
{ | ||
span[index++] = Numbers[0]; | ||
return index; | ||
} | ||
|
||
var targetLength = sizeHint > 0 ? sizeHint : Int64SizeInCharacters(i); | ||
Aaronontheweb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if(negative){ | ||
i = Math.Abs(i); | ||
} | ||
|
||
while (i > 0) | ||
{ | ||
span[startPos + targetLength - index++ - 1] = Numbers[i % 10]; | ||
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. Is there a reason we need 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. Additional Question: would it be better to do 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. I'll add a dedicated benchmark for this function so we can measure it 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.
I don't think it hurts anything - I just didn't wanted all changes to be made on the same input |
||
i /= 10; | ||
} | ||
|
||
if(negative){ | ||
span[0] = Negative; | ||
index++; | ||
} | ||
|
||
return index; | ||
} | ||
|
||
/// <summary> | ||
/// How many characters do we need to represent this int as a string? | ||
/// </summary> | ||
/// <param name="i">The int.</param> | ||
/// <returns>Character length.</returns> | ||
public static int Int64SizeInCharacters(long i) | ||
{ | ||
// still need 1 char to represent '0' | ||
if(i == 0) return 1; | ||
|
||
// account for negative characters | ||
var startLen = i < 0 ? 1 : 0; | ||
|
||
i = Math.Abs(i); | ||
|
||
// count sig figs | ||
while (i > 0) | ||
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. Would an if or Jump based table based on length potentially be better here? 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. Might very well be - I haven't benchmarked these methods themselves, only in aggregate 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. So, looking at .NET, one thing that we -can- do here, is something like:
The advantage is that division happens half as often, even if the ASM -may- be a little bit larger... but we can elide elsewhere. :) |
||
{ | ||
i = i / 10; | ||
startLen++; | ||
} | ||
|
||
return startLen; | ||
} | ||
|
||
/// <summary> | ||
/// Parses an integer from a string. | ||
/// </summary> | ||
|
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.
I wonder whether it makes more sense to do something where we can avoid the branch in
AppendUidSpan
.Ideas that come to mind: