Skip to content

Commit

Permalink
Intro precompiled functions for faster startup and runtime efficiency
Browse files Browse the repository at this point in the history
  • Loading branch information
Viir committed Aug 6, 2024
1 parent 8b7fb8d commit be351fe
Show file tree
Hide file tree
Showing 28 changed files with 24,498 additions and 14 deletions.
File renamed without changes.
15 changes: 15 additions & 0 deletions implement/Pine.Core/ElmInteractive/ElmValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,27 @@ override public string ToString() =>

public const string ElmStringTypeTagName = "String";

public const string ElmSetTypeTagName = "Set_elm_builtin";

public const string ElmDictEmptyTagName = "RBEmpty_elm_builtin";

public const string ElmDictNotEmptyTagName = "RBNode_elm_builtin";

public static readonly PineValue ElmRecordTypeTagNameAsValue =
PineValueAsString.ValueFromString(ElmRecordTypeTagName);

public static readonly PineValue ElmStringTypeTagNameAsValue =
PineValueAsString.ValueFromString(ElmStringTypeTagName);

public static readonly PineValue ElmSetTypeTagNameAsValue =
PineValueAsString.ValueFromString(ElmSetTypeTagName);

public static readonly PineValue ElmDictEmptyTagNameAsValue =
PineValueAsString.ValueFromString(ElmDictEmptyTagName);

public static readonly PineValue ElmDictNotEmptyTagNameAsValue =
PineValueAsString.ValueFromString(ElmDictNotEmptyTagName);

public static ElmValue Integer(System.Numerics.BigInteger Value) =>
ReusedIntegerInstances?.TryGetValue(Value, out var reusedInstance) ?? false && reusedInstance is not null ?
reusedInstance
Expand Down
19 changes: 16 additions & 3 deletions implement/Pine.Core/Pine.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<Nullable>enable</Nullable>
<AssemblyVersion>0.3.15</AssemblyVersion>
<FileVersion>0.3.15</FileVersion>
<GenerateEmbeddedFilesManifest>true</GenerateEmbeddedFilesManifest>
</PropertyGroup>

<PropertyGroup>
Expand All @@ -22,9 +23,21 @@
</PropertyGroup>

<ItemGroup>
<None Include="README.md" Pack="true" PackagePath="\"/>
<None Include="./../../License.txt" Pack="true" PackagePath="\"/>
<None Include="package-icon.png" Pack="true" PackagePath=""/>
<None Include="README.md" Pack="true" PackagePath="\" />
<None Include="./../../License.txt" Pack="true" PackagePath="\" />
<None Include="package-icon.png" Pack="true" PackagePath="" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="PineVM\PopularExpression\**" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="PineVM\PopularValue\**" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.FileProviders.Embedded" Version="8.0.7" />
</ItemGroup>

</Project>
42 changes: 42 additions & 0 deletions implement/Pine.Core/PineVM/EncodePineExpressionAsJson.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Pine.Json;
using System;
using System.Collections.Generic;
using System.Text.Json;

namespace Pine.PineVM;

public class EncodePineExpressionAsJson
{
public static string ToJsonString(Expression expression) =>
JsonSerializer.Serialize(expression, jsonSerializerOptions);

public static string ToJsonString(IReadOnlyList<Expression> expression) =>
JsonSerializer.Serialize(expression, jsonSerializerOptions);

public static Expression SingleFromJsonString(string json) =>
JsonSerializer.Deserialize<Expression>(json, jsonSerializerOptions) ?? throw new Exception();

public static IReadOnlyList<Expression> ListFromJsonString(string json) =>
JsonSerializer.Deserialize<IReadOnlyList<Expression>>(json, jsonSerializerOptions) ?? throw new Exception();

public static object SingleOrListFromJsonString(string json)
{
if (json.TrimStart().StartsWith('['))
{
return ListFromJsonString(json);
}

return SingleFromJsonString(json);
}

readonly static JsonSerializerOptions jsonSerializerOptions = BuildJsonSerializerOptions();

public static JsonSerializerOptions BuildJsonSerializerOptions()
{
var options = new JsonSerializerOptions { MaxDepth = 1000 };

options.Converters.Add(new JsonConverterForPineValue());

return options;
}
}
5 changes: 5 additions & 0 deletions implement/Pine.Core/PineVM/ExpressionEncoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -581,5 +581,10 @@ Expression.KernelApplicationExpression kernelListHeadExpr(

yield return kernelListHeadExpr(justSkip);
}

foreach (var namedExpr in PopularExpression.BuildPopularExpressionDictionary())
{
yield return namedExpr.Value;
}
}
}
84 changes: 84 additions & 0 deletions implement/Pine.Core/PineVM/PopularExpression.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Text;
using System.Text.Json;

namespace Pine.PineVM;

public class PopularExpression
{
static readonly JsonSerializerOptions jsonSerializerOptions = EncodePineExpressionAsJson.BuildJsonSerializerOptions();

public static IImmutableDictionary<string, Expression> BuildPopularExpressionDictionary()
{
var exprFiles =
LoadPopularExpressionFiles()
.Extract(err => throw new Exception(err));

return
exprFiles
.Aggregate(
func: (aggregate, nextFile) =>
{
if (nextFile.Key.Count is not 1)
return aggregate;

if (nextFile.Key[0].EndsWith(".json", StringComparison.OrdinalIgnoreCase))
{
var json = Encoding.UTF8.GetString(nextFile.Value.Span);

var expression = EncodePineExpressionAsJson.SingleFromJsonString(json);

var exprName = nextFile.Key[0][..^5];

return aggregate.SetItem(exprName, expression);
}

return aggregate;
},
seed: ImmutableDictionary<string, Expression>.Empty);
}

public static IImmutableDictionary<string, PineValue> BuildPopularValueDictionary()
{
var exprFiles =
LoadPopularValueFiles()
.Extract(err => throw new Exception(err));

return
exprFiles
.Aggregate(
func: (aggregate, nextFile) =>
{
if (nextFile.Key.Count is not 1)
return aggregate;

if (nextFile.Key[0].EndsWith(".json", StringComparison.OrdinalIgnoreCase))
{
var json = Encoding.UTF8.GetString(nextFile.Value.Span);

var pineValue = JsonSerializer.Deserialize<PineValue>(json, jsonSerializerOptions)!;

var pineValueName = nextFile.Key[0][..^5];

return aggregate.SetItem(pineValueName, pineValue);
}

return aggregate;
},
seed: ImmutableDictionary<string, PineValue>.Empty);
}

public static Result<string, IImmutableDictionary<IReadOnlyList<string>, ReadOnlyMemory<byte>>> LoadPopularExpressionFiles() =>
DotNetAssembly.LoadDirectoryFilesFromManifestEmbeddedFileProviderAsDictionary(
directoryPath: ["PineVM", "PopularExpression"],
assembly: typeof(PopularExpression).Assembly);

public static Result<string, IImmutableDictionary<IReadOnlyList<string>, ReadOnlyMemory<byte>>> LoadPopularValueFiles() =>
DotNetAssembly.LoadDirectoryFilesFromManifestEmbeddedFileProviderAsDictionary(
directoryPath: ["PineVM", "PopularValue"],
assembly: typeof(PopularExpression).Assembly);

}
Loading

0 comments on commit be351fe

Please sign in to comment.