Skip to content

Commit 87dc3ac

Browse files
committed
hex convert
1 parent 38cfb8d commit 87dc3ac

2 files changed

Lines changed: 6 additions & 11 deletions

File tree

src/Speckle.Sdk/Pipelines/Send/IdGenerator.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,17 @@ public static string ComputeId(ReadOnlySpan<byte> input)
1515
Span<byte> hash = stackalloc byte[SHA256.HashSizeInBytes];
1616
SHA256.HashData(input, hash);
1717

18+
#if NET9_0_OR_GREATER
19+
return Convert.ToHexStringLower(hash);
20+
#else
1821
Span<char> output = stackalloc char[HashUtility.HASH_LENGTH];
1922

2023
for (int i = 0, j = 0; j < HashUtility.HASH_LENGTH; i += sizeof(byte), j += sizeof(char))
2124
{
2225
hash[i].TryFormat(output[j..], out _, "x2");
2326
}
24-
2527
return new string(output);
28+
#endif
2629
}
2730
#endif
2831

src/Speckle.Sdk/Pipelines/Send/Serializer.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -115,22 +115,14 @@ private IEnumerable<PropertyInfo> ExtractProperties(Base baseObj)
115115
SerializeValue(prop.Value, jsonWriter, prop.IsDetachable, childClosures, detachedObjects);
116116
}
117117

118+
// We want to hash the json string now to calculate the id
119+
// We don't want to allocate a separate buffer for it, as this wouldn't be memory efficient
118120
jsonWriter.Flush();
119121

120122
#if NET6_0_OR_GREATER
121-
// We want to hash the json string now to calculate the id
122-
// We don't want to allocate a separate buffer for it, as this wouldn't be memory efficient
123-
// It's also (debatably) important that the bytes we hash are the full json object (minus id and closures obviously)
124-
// For this, we are manually writing the closing } bracket without calling Buffer.Advance
125-
// Such that, the buffer can continue to write the id, and closures later in this function.
126-
// var bytes = efficientJson.Buffer.GetSpan(1);
127-
// bytes[0] = (byte)'}';
128123
string id = IdGenerator.ComputeId(efficientJson.WrittenSpan);
129-
string str = Encoding.UTF8.GetString(efficientJson.WrittenSpan);
130124
#else
131-
132125
string id = IdGenerator.ComputeId(efficientJson.GetInternalBuffer(), 0, efficientJson.WrittenCount);
133-
string str = Encoding.UTF8.GetString(efficientJson.GetInternalBuffer());
134126
#endif
135127
jsonWriter.WriteString("id", id);
136128

0 commit comments

Comments
 (0)