Skip to content

Commit

Permalink
Fix bug in optimization to reuse Elm value instances for small blobs
Browse files Browse the repository at this point in the history
  • Loading branch information
Viir committed Oct 1, 2024
1 parent fb6ca7d commit e8fc830
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions implement/Pine.Core/ElmInteractive/ElmValueEncoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ public static Result<string, ElmValue> PineValueAsElmValue(
private static readonly ElmValue EmptyList = new ElmValue.ElmList([]);

private static readonly IReadOnlyList<Result<string, ElmValue>> ReusedBlobSingle =
[..Enumerable.Range(0, 0xff)
[..Enumerable.Range(0, 0x1_00)
.Select(b => PineBlobValueAsElmValue((PineValue.BlobValue)PineValue.Blob([(byte)b])))];

private static readonly IReadOnlyList<Result<string, ElmValue>> ReusedBlobTuple =
[..Enumerable.Range(0, 0xff)
.SelectMany(b1 => Enumerable.Range(0, 0xff)
.Select(b2 => PineBlobValueAsElmValue((PineValue.BlobValue)PineValue.Blob([(byte)b1, (byte)b2]))))];
[..Enumerable.Range(0, 0x1_00_00)
.Select(twoBytes =>
PineBlobValueAsElmValue((PineValue.BlobValue)PineValue.Blob([(byte)(twoBytes >> 8), (byte)twoBytes])))];

public static Result<string, ElmValue> PineBlobValueAsElmValue(
PineValue.BlobValue blobValue) =>
Expand All @@ -56,7 +56,7 @@ blobValue.Bytes.Length is 1 && ReusedBlobSingle is { } internedBlobSingle ?
internedBlobSingle[blobValue.Bytes.Span[0]]
:
blobValue.Bytes.Length is 2 && ReusedBlobTuple is { } internedBlobTuple ?
internedBlobTuple[blobValue.Bytes.Span[0] * 0xff + blobValue.Bytes.Span[1]]
internedBlobTuple[blobValue.Bytes.Span[0] * 0x100 + blobValue.Bytes.Span[1]]
:
(blobValue.Bytes.Span[0] switch
{
Expand Down

0 comments on commit e8fc830

Please sign in to comment.