-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Intro precompiled functions for faster startup and runtime efficiency
- Loading branch information
Showing
28 changed files
with
24,498 additions
and
14 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains 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 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 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,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; | ||
} | ||
} |
This file contains 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 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,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); | ||
|
||
} |
Oops, something went wrong.