From efcd5ac8f47bf520bf3741ccc1e7901a5171ea71 Mon Sep 17 00:00:00 2001 From: Daniel Cazzulino Date: Mon, 5 Sep 2022 02:39:22 -0300 Subject: [PATCH] Remove dependency on Newtonsoft.Json A source generator takes care of converting the JSON data into the same run-time C# dictionary structure we had before, but initialized statically via generated code instead. This should be quite a bit more performant too. Fixes #28 --- ISBN.sln | 6 + src/GroupsGenerator/GroupsGenerator.cs | 92 +++++++ src/GroupsGenerator/GroupsGenerator.csproj | 22 ++ src/GroupsGenerator/Template.sbntxt | 44 ++++ src/ISBN/EmbeddedResource.cs | 37 --- src/ISBN/ISBN.cs | 45 +--- src/ISBN/ISBN.csproj | 13 +- src/ISBN/Range.cs | 278 --------------------- src/Tests/Tests.csproj | 1 + 9 files changed, 175 insertions(+), 363 deletions(-) create mode 100644 src/GroupsGenerator/GroupsGenerator.cs create mode 100644 src/GroupsGenerator/GroupsGenerator.csproj create mode 100644 src/GroupsGenerator/Template.sbntxt delete mode 100644 src/ISBN/EmbeddedResource.cs delete mode 100644 src/ISBN/Range.cs diff --git a/ISBN.sln b/ISBN.sln index 8be20fb..76b085b 100644 --- a/ISBN.sln +++ b/ISBN.sln @@ -14,6 +14,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ISBN", "src\ISBN\ISBN.cspro EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tests", "src\Tests\Tests.csproj", "{981FC772-B5F4-4A38-9EA6-50904FBD0C9A}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GroupsGenerator", "src\GroupsGenerator\GroupsGenerator.csproj", "{1A0A023F-2E23-44AC-8ABA-4BE5FAFF7F61}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -28,6 +30,10 @@ Global {981FC772-B5F4-4A38-9EA6-50904FBD0C9A}.Debug|Any CPU.Build.0 = Debug|Any CPU {981FC772-B5F4-4A38-9EA6-50904FBD0C9A}.Release|Any CPU.ActiveCfg = Release|Any CPU {981FC772-B5F4-4A38-9EA6-50904FBD0C9A}.Release|Any CPU.Build.0 = Release|Any CPU + {1A0A023F-2E23-44AC-8ABA-4BE5FAFF7F61}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1A0A023F-2E23-44AC-8ABA-4BE5FAFF7F61}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1A0A023F-2E23-44AC-8ABA-4BE5FAFF7F61}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1A0A023F-2E23-44AC-8ABA-4BE5FAFF7F61}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/src/GroupsGenerator/GroupsGenerator.cs b/src/GroupsGenerator/GroupsGenerator.cs new file mode 100644 index 0000000..ea1fd4b --- /dev/null +++ b/src/GroupsGenerator/GroupsGenerator.cs @@ -0,0 +1,92 @@ +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Text; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.Text; +using Newtonsoft.Json.Linq; +using Scriban; +using static System.Net.Mime.MediaTypeNames; + +public record Range(string Min, string Max); +public record GroupDef(string Key, string Name, Range[] Ranges); + +[Generator(LanguageNames.CSharp)] +public class GroupsGenerator : ISourceGenerator +{ + public void Initialize(GeneratorInitializationContext context) { } + + public void Execute(GeneratorExecutionContext context) + { + var jsFile = context.AdditionalFiles.FirstOrDefault(f => Path.GetFileName(f.Path) == "groups.js"); + + if (jsFile == null) + { + context.ReportDiagnostic(Diagnostic.Create("ISBN001", "Compiler", "groups.js not found", + DiagnosticSeverity.Error, DiagnosticSeverity.Error, true, 1)); + return; + } + + var text = File.ReadAllText(jsFile.Path); + text = text[text.IndexOf('{')..]; + + var source = RenderTemplate(text); + + context.AddSource("ISBN.Groups.g", SourceText.From(source, Encoding.UTF8)); + } + + public static string RenderTemplate(string json) + { + var data = JObject.Parse(json); + var groups = new ConcurrentDictionary>>(); + + foreach (var prop in data.Properties()) + { + if (prop?.Value is JObject obj && + obj.Value("name") is string name && + obj.Property("ranges") is JProperty ranges && + ranges?.Value is JArray rangeValues) + { + var result = new List(); + foreach (var range in rangeValues) + { + var values = range.Values().ToArray(); + if (values.Length != 2 || + values[0] == null || values[1] == null) + continue; + + result.Add(new Range(values[0]!, values[1]!)); + } + + var parts = prop.Name.Split('-'); + var prefix = parts[0]; + var group = parts[1]; + var groupFirstDigit = group[0]; + + groups.GetOrAdd(prefix, _ => new()) + .GetOrAdd(groupFirstDigit, _ => new()) + [group] = new GroupDef(group, name, result.ToArray()); + } + } + + var path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().ManifestModule.FullyQualifiedName), "Template.sbntxt"); + Template template; + + if (File.Exists(path)) + { + template = Template.Parse(File.ReadAllText(path)); + } + else + { + using var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("GroupsGenerator.Template.sbntxt"); + using var reader = new StreamReader(stream); + template = Template.Parse(reader.ReadToEnd()); + } + + var output = template.Render(new { map = groups }, member => member.Name); + + return output; + } +} diff --git a/src/GroupsGenerator/GroupsGenerator.csproj b/src/GroupsGenerator/GroupsGenerator.csproj new file mode 100644 index 0000000..166a39a --- /dev/null +++ b/src/GroupsGenerator/GroupsGenerator.csproj @@ -0,0 +1,22 @@ + + + + netstandard2.0 + false + Preview + false + + + + + + + + + + + + + + + diff --git a/src/GroupsGenerator/Template.sbntxt b/src/GroupsGenerator/Template.sbntxt new file mode 100644 index 0000000..87572eb --- /dev/null +++ b/src/GroupsGenerator/Template.sbntxt @@ -0,0 +1,44 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +using System.Collections.Generic; + +partial record ISBN +{ + static readonly Dictionary>> groups = new() + { + {{~ for pair in map ~}} + { + "{{ pair.Key }}", + new() + { + {{~ for cp in pair.Value ~}} + { + '{{ cp.Key }}', + new() + { + {{~ for gp in cp.Value ~}} + { + "{{~ gp.Key ~}}", + new("{{~ gp.Value.Key ~}}", "{{~ gp.Value.Name ~}}", new Range[] + { + {{~ for r in gp.Value.Ranges ~}} + new Range("{{~ r.Min ~}}", "{{~ r.Max ~}}"), + {{~ end ~}} + }) + }, + {{~ end ~}} + } + }, + {{~ end ~}} + } + }, + {{~ end ~}} + }; +} \ No newline at end of file diff --git a/src/ISBN/EmbeddedResource.cs b/src/ISBN/EmbeddedResource.cs deleted file mode 100644 index 1ec4b70..0000000 --- a/src/ISBN/EmbeddedResource.cs +++ /dev/null @@ -1,37 +0,0 @@ -using System; -using System.IO; -using System.Linq; -using System.Reflection; - -static class EmbeddedResource -{ - static readonly string? baseDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); - - public static string GetContent(string relativePath) - { - var filePath = Path.Combine(baseDir ?? "", Path.GetFileName(relativePath)); - if (File.Exists(filePath)) - return File.ReadAllText(filePath); - - var baseName = Assembly.GetExecutingAssembly().GetName().Name; - var resourceName = relativePath - .TrimStart('.') - .Replace(Path.DirectorySeparatorChar, '.') - .Replace(Path.AltDirectorySeparatorChar, '.'); - - var manifestResourceName = Assembly.GetExecutingAssembly() - .GetManifestResourceNames().FirstOrDefault(x => x.EndsWith(resourceName)); - - if (string.IsNullOrEmpty(manifestResourceName)) - throw new InvalidOperationException($"Did not find required resource ending in '{resourceName}' in assembly '{baseName}'."); - - using var stream = Assembly.GetExecutingAssembly() - .GetManifestResourceStream(manifestResourceName); - - if (stream == null) - throw new InvalidOperationException($"Did not find required resource '{manifestResourceName}' in assembly '{baseName}'."); - - using var reader = new StreamReader(stream); - return reader.ReadToEnd(); - } -} \ No newline at end of file diff --git a/src/ISBN/ISBN.cs b/src/ISBN/ISBN.cs index aa5098e..e1b4608 100644 --- a/src/ISBN/ISBN.cs +++ b/src/ISBN/ISBN.cs @@ -1,9 +1,6 @@ -using System.Collections.Concurrent; -using System.Collections.Generic; -using System.Diagnostics.CodeAnalysis; +using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Text; -using Newtonsoft.Json.Linq; /// /// Adapted to C# from https://github.com/inventaire/isbn3 @@ -13,44 +10,6 @@ public partial record ISBN record Range(string Min, string Max); record GroupDef(string Key, string Name, Range[] Ranges); - static readonly ConcurrentDictionary>> groupsMap = new(); - - static ISBN() - { - var raw = EmbeddedResource.GetContent("groups.js"); - var json = raw[raw.IndexOf('{')..]; - var data = JObject.Parse(json); - - foreach (var prop in data.Properties()) - { - if (prop?.Value is JObject obj && - obj.Value("name") is string name && - obj.Property("ranges") is JProperty ranges && - ranges?.Value is JArray rangeValues) - { - var result = new List(); - foreach (var range in rangeValues) - { - var values = range.Values().ToArray(); - if (values.Length != 2 || - values[0] == null || values[1] == null) - continue; - - result.Add(new Range(values[0]!, values[1]!)); - } - - var parts = prop.Name.Split('-'); - var prefix = parts[0]; - var group = parts[1]; - var groupFirstDigit = group[0]; - - groupsMap.GetOrAdd(prefix, _ => new()) - .GetOrAdd(groupFirstDigit, _ => new()) - [group] = new GroupDef(group, name, result.ToArray()); - } - } - } - /// /// Tries parsing the given as a structure. /// @@ -175,7 +134,7 @@ static bool VerifyChecksum10(string isbn) static (GroupDef group, string restAfterGroup)? GetGroup(string isbn) { var prefix = isbn[..3]; - if (!groupsMap.TryGetValue(prefix, out var groupMap)) + if (!groups.TryGetValue(prefix, out var groupMap)) return null; var restAfterPrefix = isbn[3..]; diff --git a/src/ISBN/ISBN.csproj b/src/ISBN/ISBN.csproj index 4cbdb28..4278610 100644 --- a/src/ISBN/ISBN.csproj +++ b/src/ISBN/ISBN.csproj @@ -10,19 +10,22 @@ - - - + + - + - + + + + + diff --git a/src/ISBN/Range.cs b/src/ISBN/Range.cs deleted file mode 100644 index 0efbadd..0000000 --- a/src/ISBN/Range.cs +++ /dev/null @@ -1,278 +0,0 @@ -// https://github.com/dotnet/corefx/blob/1597b894a2e9cac668ce6e484506eca778a85197/src/Common/src/CoreLib/System/Index.cs -// https://github.com/dotnet/corefx/blob/1597b894a2e9cac668ce6e484506eca778a85197/src/Common/src/CoreLib/System/Range.cs -#pragma warning disable CS0436 // Type conflicts with imported type - -using System.Runtime.CompilerServices; - -namespace System -{ - /// Represent a type can be used to index a collection either from the start or the end. - /// - /// Index is used by the C# compiler to support the new index syntax - /// - /// int[] someArray = new int[5] { 1, 2, 3, 4, 5 } ; - /// int lastElement = someArray[^1]; // lastElement = 5 - /// - /// - readonly struct Index : IEquatable - { - readonly int _value; - - /// Construct an Index using a value and indicating if the index is from the start or from the end. - /// The index value. it has to be zero or positive number. - /// Indicating if the index is from the start or from the end. - /// - /// If the Index constructed from the end, index value 1 means pointing at the last element and index value 0 means pointing at beyond last element. - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public Index(int value, bool fromEnd = false) - { - if (value < 0) - { - throw new ArgumentOutOfRangeException(nameof(value), "value must be non-negative"); - } - - if (fromEnd) - _value = ~value; - else - _value = value; - } - - // The following private constructors mainly created for perf reason to avoid the checks - Index(int value) - { - _value = value; - } - - /// Create an Index pointing at first element. - public static Index Start => new Index(0); - - /// Create an Index pointing at beyond last element. - public static Index End => new Index(~0); - - /// Create an Index from the start at the position indicated by the value. - /// The index value from the start. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Index FromStart(int value) - { - if (value < 0) - { - throw new ArgumentOutOfRangeException(nameof(value), "value must be non-negative"); - } - - return new Index(value); - } - - /// Create an Index from the end at the position indicated by the value. - /// The index value from the end. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Index FromEnd(int value) - { - if (value < 0) - { - throw new ArgumentOutOfRangeException(nameof(value), "value must be non-negative"); - } - - return new Index(~value); - } - - /// Returns the index value. - public int Value - { - get - { - if (_value < 0) - { - return ~_value; - } - else - { - return _value; - } - } - } - - /// Indicates whether the index is from the start or the end. - public bool IsFromEnd => _value < 0; - - /// Calculate the offset from the start using the giving collection length. - /// The length of the collection that the Index will be used with. length has to be a positive value - /// - /// For performance reason, we don't validate the input length parameter and the returned offset value against negative values. - /// we don't validate either the returned offset is greater than the input length. - /// It is expected Index will be used with collections which always have non negative length/count. If the returned offset is negative and - /// then used to index a collection will get out of range exception which will be same affect as the validation. - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public int GetOffset(int length) - { - var offset = _value; - if (IsFromEnd) - { - // offset = length - (~value) - // offset = length + (~(~value) + 1) - // offset = length + value + 1 - - offset += length + 1; - } - return offset; - } - - /// Indicates whether the current Index object is equal to another object of the same type. - /// An object to compare with this object - public override bool Equals(object? value) => value is Index index && _value == index._value; - - /// Indicates whether the current Index object is equal to another Index object. - /// An object to compare with this object - public bool Equals(Index other) => _value == other._value; - - /// Returns the hash code for this instance. - public override int GetHashCode() => _value; - - /// Converts integer number to an Index. - public static implicit operator Index(int value) => FromStart(value); - - /// Converts the value of the current Index object to its equivalent string representation. - public override string ToString() - { - if (IsFromEnd) - return "^" + ((uint)Value).ToString(); - - return ((uint)Value).ToString(); - } - } - - /// Represent a range has start and end indexes. - /// - /// Range is used by the C# compiler to support the range syntax. - /// - /// int[] someArray = new int[5] { 1, 2, 3, 4, 5 }; - /// int[] subArray1 = someArray[0..2]; // { 1, 2 } - /// int[] subArray2 = someArray[1..^0]; // { 2, 3, 4, 5 } - /// - /// - readonly struct Range : IEquatable - { - /// Represent the inclusive start index of the Range. - public Index Start { get; } - - /// Represent the exclusive end index of the Range. - public Index End { get; } - - /// Construct a Range object using the start and end indexes. - /// Represent the inclusive start index of the range. - /// Represent the exclusive end index of the range. - public Range(Index start, Index end) - { - Start = start; - End = end; - } - - /// Indicates whether the current Range object is equal to another object of the same type. - /// An object to compare with this object - public override bool Equals(object? value) => - value is Range r && - r.Start.Equals(Start) && - r.End.Equals(End); - - /// Indicates whether the current Range object is equal to another Range object. - /// An object to compare with this object - public bool Equals(Range other) => other.Start.Equals(Start) && other.End.Equals(End); - - /// Returns the hash code for this instance. - public override int GetHashCode() - { - return Start.GetHashCode() * 31 + End.GetHashCode(); - } - - /// Converts the value of the current Range object to its equivalent string representation. - public override string ToString() - { - return Start + ".." + End; - } - - /// Create a Range object starting from start index to the end of the collection. - public static Range StartAt(Index start) => new Range(start, Index.End); - - /// Create a Range object starting from first element in the collection to the end Index. - public static Range EndAt(Index end) => new Range(Index.Start, end); - - /// Create a Range object starting from first element to the end. - public static Range All => new Range(Index.Start, Index.End); - - /// Calculate the start offset and length of range object using a collection length. - /// The length of the collection that the range will be used with. length has to be a positive value. - /// - /// For performance reason, we don't validate the input length parameter against negative values. - /// It is expected Range will be used with collections which always have non negative length/count. - /// We validate the range is inside the length scope though. - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public (int Offset, int Length) GetOffsetAndLength(int length) - { - int start; - var startIndex = Start; - if (startIndex.IsFromEnd) - start = length - startIndex.Value; - else - start = startIndex.Value; - - int end; - var endIndex = End; - if (endIndex.IsFromEnd) - end = length - endIndex.Value; - else - end = endIndex.Value; - - if ((uint)end > (uint)length || (uint)start > (uint)end) - { - throw new ArgumentOutOfRangeException(nameof(length)); - } - - return (start, end - start); - } - } -} - -namespace System.Runtime.CompilerServices -{ - static class RuntimeHelpers - { - /// - /// Slices the specified array using the specified range. - /// - public static T[] GetSubArray(T[] array, Range range) - { - if (array == null) - { - throw new ArgumentNullException(nameof(array)); - } - - (var offset, var length) = range.GetOffsetAndLength(array.Length); - - if (default(T) != null || typeof(T[]) == array.GetType()) - { - // We know the type of the array to be exactly T[]. - - if (length == 0) - { - return Array.Empty(); - } - - var dest = new T[length]; - Array.Copy(array, offset, dest, 0, length); - return dest; - } - else - { - // The array is actually a U[] where U:T. -#pragma warning disable CS8604 // Possible null reference argument. - var dest = (T[])Array.CreateInstance(array.GetType().GetElementType(), length); -#pragma warning restore CS8604 // Possible null reference argument. - Array.Copy(array, offset, dest, 0, length); - return dest; - } - } - } -} -#pragma warning restore CS0436 // Type conflicts with imported type diff --git a/src/Tests/Tests.csproj b/src/Tests/Tests.csproj index a135238..42ba2f2 100644 --- a/src/Tests/Tests.csproj +++ b/src/Tests/Tests.csproj @@ -3,6 +3,7 @@ net6.0 false + false