-
Notifications
You must be signed in to change notification settings - Fork 9
GC Perf 2 #39
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
GC Perf 2 #39
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
da9b6c5
perf(JElement):部分修正为使用基于UTF8的查找
IksRain 60d0d68
perf:使用Stream代替String减少中间数据对象以提升性能,未改动逻辑
IksRain 8e5acf3
str-cmp-fix:忽略文化差异进行字符串比较
IksRain b87ca8c
style:使用字符而不是字符串
IksRain f6b7e2c
perf-Span-GC: 使用Span优化MinecraftEntry序列化是高频调用的拼接路径操作
IksRain 528a8c3
ci:项目内部集成CI(XUnit)
IksRain f0810b3
ci:添加AI写的样例进行测试
IksRain d9839d2
fix: 修复缓冲区过小的问题,将原方法作为仅DEBUG存在
IksRain 2615879
fix-ci: 将field关键字转换为显式_url以适配ci需求. .NET 10 SDK呢
IksRain 57b8da5
ci-actions: 统一CI行为,避免找不到测试时误报
IksRain c184ed3
perf-cvt:添加自定义SHA1(反)序列化器.避免string
IksRain 5d8cc56
tran: 使用Sha1Data替换string
IksRain eef5a28
style: ForfeInstaller快速机制
IksRain 1574deb
fix: 修复无已下载版本时意外抛出异常的问题
IksRain 5aaa14e
perf: 减少重复序列化开销
IksRain 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,8 @@ | ||
| namespace MinecraftLaunch.Base.Interfaces; | ||
| using MinecraftLaunch.Base.Models.SHA1; | ||
|
|
||
| namespace MinecraftLaunch.Base.Interfaces; | ||
|
|
||
| public interface IVerifiableDependency { | ||
| long? Size { get; } | ||
| string Sha1 { get; } | ||
| Sha1Data? Sha1 { get; } | ||
| } |
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,10 @@ | ||
| | ||
| using MinecraftLaunch.Base.Models.SHA1; | ||
|
|
||
| namespace MinecraftLaunch.Base.Models.Network; | ||
|
|
||
| public record FileHashes | ||
| { | ||
| public required string Sha512 { get; init; } | ||
| public required string Sha1 { get; init; } | ||
| public required Sha1Data Sha1 { get; init; } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| using System.Runtime.CompilerServices; | ||
| using System.Text.Json; | ||
| using System.Text.Json.Serialization; | ||
|
|
||
|
|
||
| namespace MinecraftLaunch.Base.Models.SHA1; | ||
|
|
||
|
|
||
| [JsonConverter(typeof(Sha1DataJsonConverter))] | ||
| [InlineArray(20)] | ||
| public partial struct Sha1Data : IEquatable<Sha1Data> | ||
| { | ||
| public bool Equals(Sha1Data other) => _data == other._data; | ||
|
|
||
| public override bool Equals(object obj) => obj is Sha1Data other && Equals(other); | ||
|
|
||
| public override int GetHashCode() => _data.GetHashCode(); | ||
|
|
||
| private byte _data; | ||
|
|
||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| public void FormatTo(Span<char> destination) | ||
| { | ||
| for (var i = 0; i < 20; i++) | ||
| { | ||
| var b = this[i]; | ||
| destination[i * 2] = ToHexCharLower(b >> 4); | ||
| destination[i * 2 + 1] = ToHexCharLower(b & 0x0F); | ||
| } | ||
| return; | ||
|
|
||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| static char ToHexCharLower(int b) => b switch | ||
| { | ||
| < 10 => (char)('0' + b), | ||
| _ => (char)('a' + (b - 10)) | ||
| }; | ||
| } | ||
| /// <summary> | ||
| /// 返回 40 位小写十六进制字符串。 | ||
| /// </summary> | ||
| public override string ToString() | ||
| { | ||
| // 栈上分配 40 个 char,避免托管内存分配 | ||
| Span<char> buffer = stackalloc char[40]; | ||
| FormatTo(buffer); | ||
| return new string(buffer); | ||
| } | ||
|
|
||
|
|
||
|
|
||
| public sealed class Sha1DataJsonConverter : JsonConverter<Sha1Data> | ||
| { | ||
| public override Sha1Data Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
| { | ||
| if (typeToConvert != typeof(Sha1Data)) | ||
| { | ||
| ThrowHelper.ThrowInvalidTargetType(); | ||
| } | ||
| if (reader.TokenType != JsonTokenType.String) | ||
| ThrowHelper.ThrowUnexpectedTokenType(); | ||
|
|
||
| var utf8Hex = reader.ValueSpan; | ||
| if (utf8Hex.Length != 40) | ||
| ThrowHelper.ThrowInvalidLength(utf8Hex.Length); | ||
| Unsafe.SkipInit(out Sha1Data result); | ||
| for (var i = 0; i < 20; i++) | ||
| { | ||
| var idx = i * 2; | ||
| result[i] = ParseHex(utf8Hex[idx], utf8Hex[idx + 1]); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
|
|
||
| public override void Write(Utf8JsonWriter writer, Sha1Data value, JsonSerializerOptions options) | ||
| { | ||
| var buffer = (Span<byte>)stackalloc byte[40]; | ||
| for (var i = 0; i < 20; i++) | ||
| { | ||
| var b = value[i]; | ||
| buffer[i * 2] = ToHexCharLower(b >> 4); | ||
| buffer[i * 2 + 1] = ToHexCharLower(b & 0x0F); | ||
| } | ||
| writer.WriteStringValue(buffer); | ||
| } | ||
|
|
||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| private static byte ParseHex(byte c1, byte c2) | ||
| { | ||
| var h = (c1 & 0x0F) + ((c1 & 0x40) != 0 ? 9 : 0); | ||
| var l = (c2 & 0x0F) + ((c2 & 0x40) != 0 ? 9 : 0); | ||
| return (byte)((h << 4) | l); | ||
| } | ||
|
|
||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| private static byte ToHexCharLower(int b) => b switch | ||
| { | ||
| < 10 => (byte)('0' + b), | ||
| _ => (byte)('a' + (b - 10)) | ||
| }; | ||
|
|
||
| private static class ThrowHelper | ||
| { | ||
| [MethodImpl(MethodImplOptions.NoInlining)] | ||
| public static void ThrowUnexpectedTokenType() => throw new JsonException("Unexpected token type for SHA1 data, expected a string."); | ||
| [MethodImpl(MethodImplOptions.NoInlining)] | ||
| public static void ThrowInvalidLength(int len) => throw new JsonException($"SHA1 hex string must be 40 characters long, got {len}."); | ||
| [MethodImpl(MethodImplOptions.NoInlining)] | ||
| public static void ThrowInvalidTargetType() | ||
| { | ||
| throw new JsonException("Invalid target type."); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| [JsonSerializable(typeof(Sha1Data))] | ||
| [JsonSerializable(typeof(Sha1Data[]))] | ||
| public sealed partial class Sha1DataSerializerContext : JsonSerializerContext; | ||
| } | ||
|
|
||
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
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
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.