-
Notifications
You must be signed in to change notification settings - Fork 578
[LlvmIrGenerator] ~4x faster type map generation #12279
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
Merged
Merged
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
132b364
[LlvmIrGenerator] Speed up string blob generation
jonathanpeppers b9adc8d
[LlvmIrGenerator] fast path for basic types + shared hex helper
jonathanpeppers f7e74d8
[ScannerHashingHelper] call HexUtilities.ToHexString directly
jonathanpeppers 9a03a9f
[tests] add HexUtilities unit tests
jonathanpeppers fc03e0f
[HexUtilities] add WriteHex() overloads
jonathanpeppers f8e5d18
[BaseTasks] use InternalsVisibleTo for HexUtilities tests
jonathanpeppers 6b88e3a
[HexUtilities] avoid stackalloc in the TextWriter overload
jonathanpeppers 633bd80
[BaseTasks] make HexUtilities public
jonathanpeppers 195cab0
[HexUtilities] validate span length, fix Mono style spacing
jonathanpeppers 1016980
Assert GetHexValue() receives a nibble
jonathanpeppers de4e0c3
Inline AssertNibble() into GetHexValue()
jonathanpeppers File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| #nullable enable | ||
| using System; | ||
| using System.IO; | ||
|
|
||
| namespace Microsoft.Android.Build.Tasks | ||
| { | ||
| /// <summary> | ||
| /// Allocation-free helpers for rendering bytes as hexadecimal. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// This file is also linked into <c>Microsoft.Android.Sdk.TrimmableTypeMap</c>, which | ||
| /// deliberately does not reference <c>Microsoft.Android.Build.BaseTasks</c> (that would drag | ||
| /// Microsoft.Build.*, LibZipSharp, K4os.LZ4 and Mono.Unix into it). Only the copy compiled | ||
| /// into <c>Microsoft.Android.Build.BaseTasks</c> is <c>public</c>; the linked copy stays | ||
| /// <c>internal</c>, otherwise <c>Xamarin.Android.Build.Tasks</c> — which references both | ||
| /// assemblies — fails with <c>CS0433</c>. | ||
| /// </remarks> | ||
| #if MICROSOFT_ANDROID_BUILD_BASETASKS | ||
| public | ||
| #endif | ||
| static class HexUtilities | ||
| { | ||
| /// <summary> | ||
| /// Convert a value in the <c>0..15</c> range to its hexadecimal digit. | ||
| /// </summary> | ||
| public static char GetHexValue (int value, bool upperCase = true) | ||
|
jonathanpeppers marked this conversation as resolved.
|
||
| { | ||
| if (value < 10) | ||
| return (char)(value + '0'); | ||
| return (char)(value - 10 + (upperCase ? 'A' : 'a')); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Write <paramref name="value"/> into <paramref name="destination"/> as exactly two | ||
| /// hexadecimal digits. | ||
| /// </summary> | ||
| public static void WriteHex (Span<char> destination, byte value, bool upperCase = true) | ||
| { | ||
| destination [0] = GetHexValue (value >> 4, upperCase); | ||
| destination [1] = GetHexValue (value & 0x0f, upperCase); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Write <paramref name="value"/> to <paramref name="writer"/> as exactly two hexadecimal | ||
| /// digits, without allocating. | ||
| /// </summary> | ||
| public static void WriteHex (TextWriter writer, byte value, bool upperCase = true) | ||
| { | ||
| if (writer == null) | ||
| throw new ArgumentNullException (nameof (writer)); | ||
|
|
||
| writer.Write (GetHexValue (value >> 4, upperCase)); | ||
| writer.Write (GetHexValue (value & 0x0f, upperCase)); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Convert <paramref name="bytes"/> to a hexadecimal string, without allocating | ||
| /// intermediate strings. | ||
| /// </summary> | ||
| public static string ToHexString (ReadOnlySpan<byte> bytes, bool upperCase = true) | ||
| { | ||
| const int MaxStackCharLength = 128; | ||
|
|
||
| int charLength = bytes.Length * 2; | ||
| Span<char> chars = charLength <= MaxStackCharLength | ||
| ? stackalloc char[charLength] | ||
| : new char[charLength]; | ||
| for (int i = 0, j = 0; i < bytes.Length; i += 1, j += 2) { | ||
| WriteHex (chars.Slice (j, 2), bytes [i], upperCase); | ||
| } | ||
| return ((ReadOnlySpan<char>) chars).ToString (); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
150 changes: 150 additions & 0 deletions
150
tests/Microsoft.Android.Build.BaseTasks-Tests/HexUtilitiesTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| using System; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using Microsoft.Android.Build.Tasks; | ||
| using NUnit.Framework; | ||
|
|
||
| namespace Microsoft.Android.Build.BaseTasks.Tests | ||
| { | ||
| [TestFixture] | ||
| public class HexUtilitiesTests | ||
| { | ||
| [Test] | ||
| public void GetHexValue_UpperCase () | ||
| { | ||
| const string expected = "0123456789ABCDEF"; | ||
| for (int i = 0; i < expected.Length; i++) { | ||
| Assert.AreEqual (expected [i], HexUtilities.GetHexValue (i), $"Mismatch at {i}."); | ||
| } | ||
| } | ||
|
|
||
| [Test] | ||
| public void GetHexValue_LowerCase () | ||
| { | ||
| const string expected = "0123456789abcdef"; | ||
| for (int i = 0; i < expected.Length; i++) { | ||
| Assert.AreEqual (expected [i], HexUtilities.GetHexValue (i, upperCase: false), $"Mismatch at {i}."); | ||
| } | ||
| } | ||
|
|
||
| [Test] | ||
| public void GetHexValue_DefaultsToUpperCase () | ||
| { | ||
| Assert.AreEqual ('A', HexUtilities.GetHexValue (10)); | ||
| } | ||
|
|
||
| [Test] | ||
| public void WriteHex_Span_UpperCase () | ||
| { | ||
| Span<char> chars = stackalloc char [2]; | ||
| HexUtilities.WriteHex (chars, 0xab); | ||
| Assert.AreEqual ("AB", chars.ToString ()); | ||
| } | ||
|
|
||
| [Test] | ||
| public void WriteHex_Span_LowerCase () | ||
| { | ||
| Span<char> chars = stackalloc char [2]; | ||
| HexUtilities.WriteHex (chars, 0xab, upperCase: false); | ||
| Assert.AreEqual ("ab", chars.ToString ()); | ||
| } | ||
|
|
||
| [Test] | ||
| public void WriteHex_Span_WritesExactlyTwoChars () | ||
| { | ||
| Span<char> chars = stackalloc char [4]; | ||
| chars.Fill ('_'); | ||
| HexUtilities.WriteHex (chars.Slice (1, 2), 0x0f); | ||
| Assert.AreEqual ("_0F_", chars.ToString ()); | ||
| } | ||
|
|
||
| [Test] | ||
| public void WriteHex_Span_EveryByteValue () | ||
| { | ||
| Span<char> chars = stackalloc char [2]; | ||
| for (int i = 0; i < 256; i++) { | ||
| HexUtilities.WriteHex (chars, (byte) i); | ||
| Assert.AreEqual (i.ToString ("X2"), chars.ToString (), $"Mismatch at {i}."); | ||
| } | ||
| } | ||
|
|
||
| [Test] | ||
| public void WriteHex_TextWriter_UpperCase () | ||
| { | ||
| var writer = new StringWriter (); | ||
| HexUtilities.WriteHex (writer, 0x00); | ||
| HexUtilities.WriteHex (writer, 0x0f); | ||
| HexUtilities.WriteHex (writer, 0xff); | ||
| Assert.AreEqual ("000FFF", writer.ToString ()); | ||
| } | ||
|
|
||
| [Test] | ||
| public void WriteHex_TextWriter_LowerCase () | ||
| { | ||
| var writer = new StringWriter (); | ||
| HexUtilities.WriteHex (writer, 0x00, upperCase: false); | ||
| HexUtilities.WriteHex (writer, 0x0f, upperCase: false); | ||
| HexUtilities.WriteHex (writer, 0xff, upperCase: false); | ||
| Assert.AreEqual ("000fff", writer.ToString ()); | ||
| } | ||
|
|
||
| [Test] | ||
| public void WriteHex_TextWriter_EveryByteValue () | ||
| { | ||
| var writer = new StringWriter (); | ||
| for (int i = 0; i < 256; i++) { | ||
| HexUtilities.WriteHex (writer, (byte) i); | ||
| } | ||
| var expected = string.Concat (Enumerable.Range (0, 256).Select (i => i.ToString ("X2"))); | ||
| Assert.AreEqual (expected, writer.ToString ()); | ||
| } | ||
|
|
||
| [Test] | ||
| public void WriteHex_TextWriter_NullThrows () | ||
| { | ||
| Assert.Throws<ArgumentNullException> (() => HexUtilities.WriteHex (writer: null, value: 0x00)); | ||
| } | ||
|
|
||
| [Test] | ||
| public void ToHexString_Empty () | ||
| { | ||
| Assert.AreEqual ("", HexUtilities.ToHexString (ReadOnlySpan<byte>.Empty)); | ||
| } | ||
|
|
||
| [Test] | ||
| public void ToHexString_UpperCase () | ||
| { | ||
| var bytes = new byte [] { 0x00, 0x01, 0x0f, 0x10, 0x7f, 0x80, 0xab, 0xff }; | ||
| Assert.AreEqual ("00010F107F80ABFF", HexUtilities.ToHexString (bytes)); | ||
| } | ||
|
|
||
| [Test] | ||
| public void ToHexString_LowerCase () | ||
| { | ||
| var bytes = new byte [] { 0x00, 0x01, 0x0f, 0x10, 0x7f, 0x80, 0xab, 0xff }; | ||
| Assert.AreEqual ("00010f107f80abff", HexUtilities.ToHexString (bytes, upperCase: false)); | ||
| } | ||
|
|
||
| [Test] | ||
| public void ToHexString_EveryByteValue () | ||
| { | ||
| var bytes = Enumerable.Range (0, 256).Select (i => (byte) i).ToArray (); | ||
| var expected = BitConverter.ToString (bytes).Replace ("-", ""); | ||
| Assert.AreEqual (expected, HexUtilities.ToHexString (bytes)); | ||
| Assert.AreEqual (expected.ToLowerInvariant (), HexUtilities.ToHexString (bytes, upperCase: false)); | ||
| } | ||
|
|
||
| // The implementation stackallocs up to 128 chars (64 bytes) and heap allocates beyond that | ||
| [TestCase (63)] | ||
| [TestCase (64)] | ||
| [TestCase (65)] | ||
| [TestCase (1024)] | ||
| public void ToHexString_CrossesStackallocThreshold (int length) | ||
| { | ||
| var bytes = new byte [length]; | ||
| new Random (42).NextBytes (bytes); | ||
| var expected = BitConverter.ToString (bytes).Replace ("-", ""); | ||
| Assert.AreEqual (expected, HexUtilities.ToHexString (bytes)); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.