Skip to content
This repository has been archived by the owner on Nov 6, 2019. It is now read-only.

Commit

Permalink
Combining string instructions and optimizing its workflow. #15
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike-E-angelo committed Jun 22, 2019
1 parent 51a7e09 commit 88b797b
Show file tree
Hide file tree
Showing 11 changed files with 222 additions and 169 deletions.
23 changes: 13 additions & 10 deletions Super.Platform.NetCore/Utf8.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using JetBrains.Annotations;
using Super.Text;
using Super.Text;
using System;
using System.Buffers;
using System.Runtime.CompilerServices;
Expand All @@ -13,23 +12,27 @@ public sealed class Utf8 : IUtf8
HighSurrogateEnd = '\udbff',
LowSurrogateStart = '\udc00',
LowSurrogateEnd = '\udfff';

[UsedImplicitly]
public static Utf8 Default { get; } = new Utf8();

Utf8() {}

public uint Get(Utf8Input parameter)
=> (uint)Get(parameter.Characters.Span, parameter.Destination.AsSpan().Slice((int)parameter.Start));

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int Get(ReadOnlySpan<char> source, Span<byte> destination)
{
var status = Get(MemoryMarshal.AsBytes(parameter.Characters.Span),
parameter.Destination.AsSpan().Slice((int)parameter.Start),
out var written);
var status = Get(source, destination, out var result);
return status == OperationStatus.Done
? (uint)written
? result
: throw new
InvalidOperationException($"[{status}] Could not successfully convert value to Utf-8 data: {parameter.Characters}");
InvalidOperationException($"[{status}] Could not successfully convert value to Utf-8 data: {source.ToString()}");
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static OperationStatus Get(ReadOnlySpan<char> source, Span<byte> destination, out int written)
=> Get(MemoryMarshal.AsBytes(source), destination, out written);

// ReSharper disable once CyclomaticComplexity
// ReSharper disable once MethodTooLong
/// <summary>
Expand All @@ -39,7 +42,7 @@ public uint Get(Utf8Input parameter)
/// <param name="destination"></param>
/// <param name="written"></param>
/// <returns></returns>
static unsafe OperationStatus Get(ReadOnlySpan<byte> source, Span<byte> destination, out int written)
public static unsafe OperationStatus Get(ReadOnlySpan<byte> source, Span<byte> destination, out int written)
{
fixed (byte* chars = &MemoryMarshal.GetReference(source))
fixed (byte* bytes = &MemoryMarshal.GetReference(destination))
Expand Down
2 changes: 1 addition & 1 deletion Super.Serialization.Testing.Application/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class Program
static void Main(params string[] arguments)
{
Configuration.Default.Get(arguments)
.To(Run.A<DefaultStringInstructionTests.Benchmarks>);
.To(Run.A<StringValueInstructionTests.Benchmarks>);
}
}
}
29 changes: 29 additions & 0 deletions Super.Serialization.Testing.Application/RangesTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Text.Json;
using Xunit;
using Xunit.Abstractions;

namespace Super.Serialization.Testing.Application
{
public sealed class RangesTests
{
readonly ITestOutputHelper _output;

public RangesTests(ITestOutputHelper output) => _output = output;

[Fact]
void Verify()
{
/*_output.WriteLine($"Low: {Ranges.Default.Low.Start.Value} - {Ranges.Default.Low.End.Value}");
//_output.WriteLine($"{Ranges.Default.Normal.Start.Value} - {Ranges.Default.Normal.End.Value}");
_output.WriteLine($"High: {Ranges.Default.High.Start.Value} - {Ranges.Default.High.End.Value}");
for (var i = Ranges.Default.Normal.Start.Value; i < Ranges.Default.Normal.End.Value; i++)
{
_output.WriteLine($"{i}: {(char)i}");
}*/

var valid = new string(new[] {'a', 'b', 'c', (char)0xD800, (char)0xDC00});
var text = JsonEncodedText.Encode(valid);
_output.WriteLine(text.ToString());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using BenchmarkDotNet.Attributes;
using FluentAssertions;
using Super.Serialization.Writing.Instructions;
using System;
using System.Linq;
using System.Text.Json.Serialization;
using Xunit;
Expand All @@ -13,8 +12,8 @@ public sealed class DefaultStringInstructionTests
[Fact]
void Verify()
{
var content = string.Join('\n', Enumerable.Repeat("Hello World!", 10));
Writer.Default.Get(content.AsMemory())
var content = string.Join('\n', Enumerable.Repeat("Hello World!", 3));
Writer.Default.Get(content)
.Open()
.Should()
.Equal(JsonSerializer.ToUtf8Bytes(content));
Expand All @@ -24,26 +23,26 @@ void Verify()
void VerifyNormal()
{
var content = string.Join('-', Enumerable.Repeat("Hello World!", 10));
Writer.Default.Get(content.AsMemory())
Writer.Default.Get(content)
.Open()
.Should()
.Equal(JsonSerializer.ToUtf8Bytes(content));
}

sealed class Writer : SingleInstructionWriter<ReadOnlyMemory<char>>
sealed class Writer : SingleInstructionWriter<string>
{
public static Writer Default { get; } = new Writer();

Writer() : base(DefaultStringInstruction.Default.Quoted()) {}
Writer() : base(StringInstruction.Default.Quoted()) {}
}

public class Benchmarks : Benchmark<ReadOnlyMemory<char>>
public class Benchmarks : Benchmark<string>
{
readonly string _instance;

public Benchmarks() : this(string.Join('\n', Enumerable.Repeat("Hello World!", 2))) {}

public Benchmarks(string instance) : base(Writer.Default, instance.AsMemory()) => _instance = instance;
public Benchmarks(string instance) : base(Writer.Default, instance) => _instance = instance;

[Benchmark]
public byte[] Native() => JsonSerializer.ToUtf8Bytes(_instance);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Super.Serialization.Testing.Application.Writing.Instructions
{
public sealed class DoubleInstructionTests
public sealed class DoubleInstructionTests
{
[Fact]
void Verify()
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using FluentAssertions;
using Super.Serialization.Writing.Instructions;
using System;
using System.Text.Json.Serialization;
using Xunit;

Expand All @@ -12,19 +11,19 @@ public sealed class StringValueInstructionTests
void Verify()
{
const string data = "Hello World!";
QuotedWriter.Default.Get(data.AsMemory()).Open().Should().Equal(JsonSerializer.ToUtf8Bytes(data));
QuotedWriter.Default.Get(data).Open().Should().Equal(JsonSerializer.ToUtf8Bytes(data));
}

sealed class QuotedWriter : SingleInstructionWriter<ReadOnlyMemory<char>>
sealed class QuotedWriter : SingleInstructionWriter<string>
{
public static QuotedWriter Default { get; } = new QuotedWriter();

QuotedWriter() : base(StringInstruction.Default.Quoted()) {}
}

public class Benchmarks : Benchmark<ReadOnlyMemory<char>>
public class Benchmarks : Benchmark<string>
{
public Benchmarks() : base(QuotedWriter.Default, "Hello World!".AsMemory()) {}
public Benchmarks() : base(QuotedWriter.Default, "Hello World!") {}
}
}
}
10 changes: 8 additions & 2 deletions Super.Serialization/Super.Serialization.csproj
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<TargetFramework>netcoreapp3.0</TargetFramework>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Super.Platform.NetCore\Super.Platform.NetCore.csproj" />
<ProjectReference Include="..\Super\Super.csproj" />
</ItemGroup>

<Target Name="WorkaroundNetStandard" AfterTargets="ResolvePackageAssets">
<ItemGroup>
<TransitiveFrameworkReference Remove="NETStandard.Library" />
</ItemGroup>
</Target>
</Project>
2 changes: 2 additions & 0 deletions Super.Serialization/Super.Serialization.csproj.DotSettings
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeInspection/CSharpLanguageProject/LanguageLevel/@EntryValue">Default</s:String></wpf:ResourceDictionary>
34 changes: 16 additions & 18 deletions Super.Serialization/Writer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@

namespace Super.Serialization
{
public static class RangeExtension
public static class RangeExtension
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsOrBetween(this Range @this, int point)
public static bool IsOrContains(this Range @this, int point)
=> point >= @this.Start.Value && point <= @this.End.Value;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool Between(this Range @this, int point) => point > @this.Start.Value && point < @this.End.Value;
public static bool Contains(this Range @this, int point) => point > @this.Start.Value && point < @this.End.Value;

public static Range Until(this Range @this, Range other) => new Range(@this.End, other.Start);
}
Expand All @@ -25,22 +25,20 @@ public sealed class Ranges
{
public static Ranges Default { get; } = new Ranges();

Ranges() : this(new Range('\uDC00', '\uDFFF'), new Range('\uD800', '\uDBFF')) {}
Ranges() : this(new Range('\uD800', '\uDBFF'), new Range('\uDC00', '\uDFFF')) {}

public Ranges(Range low, Range high) : this(low, low.Until(high), high) {}
public Ranges(Range high, Range low) : this(high, low, new Range(high.Start, low.End)) {}

public Ranges(Range low, Range normal, Range high)
public Ranges(Range high, Range low, Range all)
{
Low = low;
Normal = normal;
High = high;
High = high;
Low = low;
All = all;
}

public Range Low { get; }

public Range Normal { get; }

public Range High { get; }
public Range Low { get; }
public Range All { get; }
}

public sealed class DefaultEncoding : Instance<Encoding>
Expand Down Expand Up @@ -142,12 +140,12 @@ public Array<byte> Get(T parameter)
var instruction = _instructions[i];
var size = instruction.Get(parameter);
composition = composition.Index + size >= composition.Output.Length
? new Composition<T>(composition.Output.Copy(in size), parameter,
composition.Index)
: composition;
? new Composition<T>(composition.Output.Copy(in size), parameter,
composition.Index)
: composition;

composition = new Composition<T>(composition.Output, parameter,
composition.Index + instruction.Get(composition));
composition.Index + instruction.Get(composition));
}

var result = composition.Output.CopyInto(new byte[composition.Index], 0, composition.Index);
Expand All @@ -170,7 +168,7 @@ public SingleInstructionWriter(IInstruction<T> instruction, ArrayPool<byte> pool
: this(instruction, pool.Rent, pool.Return) {}

public SingleInstructionWriter(IInstruction<T> instruction, Func<int, byte[]> lease,
Action<byte[], bool> @return)
Action<byte[], bool> @return)
{
_instruction = instruction;
_lease = lease;
Expand Down
Loading

0 comments on commit 88b797b

Please sign in to comment.