Skip to content

Commit

Permalink
Tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
jamescourtney committed Nov 17, 2024
1 parent 4987cd7 commit 2ad57fb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/Benchmarks/Benchmark/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ public static void Main(string[] args)
#else
.WithRuntime(CoreRuntime.Core80)
#endif
;
;

// job = job.WithEnvironmentVariable(new EnvironmentVariable("DOTNET_TieredPGO", "0"));

var config = DefaultConfig.Instance
.AddColumn(new[] { StatisticColumn.P25, StatisticColumn.P95 })
.AddDiagnoser(MemoryDiagnoser.Default)
.AddJob(job);
.AddJob(job.DontEnforcePowerPlan());

summaries.Add(BenchmarkRunner.Run(typeof(FBBench.FBSerializeBench), config));
summaries.Add(BenchmarkRunner.Run(typeof(FBBench.FBDeserializeBench), config));
Expand Down
18 changes: 15 additions & 3 deletions src/FlatSharp.Runtime/IO/SerializationTarget/BigSpan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,8 @@ public long WriteAndProvisionString(
int maxItems = encoding.GetMaxByteCount(value.Length) + 1;
long stringStartOffset = context.AllocateVector(sizeof(byte), maxItems, sizeof(byte));

Span<byte> destination = this.ToSpan(stringStartOffset + sizeof(uint), maxItems);
BigSpan scopedThis = this.Slice(stringStartOffset, maxItems + sizeof(uint));
Span<byte> destination = scopedThis.ToSpan(sizeof(uint), maxItems);

#if NETSTANDARD2_0
int length = value.Length;
Expand All @@ -227,10 +228,10 @@ public long WriteAndProvisionString(
#endif

// null teriminator
this[stringStartOffset + bytesWritten + sizeof(uint)] = 0;
scopedThis.WriteUnalignedUnsafe<byte>(sizeof(uint) + bytesWritten, 0);

// write length
this.WriteInt(stringStartOffset, bytesWritten);
scopedThis.WriteUnalignedUnsafe<int>(0, bytesWritten);

// give back unused space. Account for null terminator.
context.Offset -= maxItems - (bytesWritten + 1);
Expand Down Expand Up @@ -262,8 +263,19 @@ private void WriteUnaligned<T>(long offset, T value)
where T : unmanaged
{
CheckAlignment(offset, Unsafe.SizeOf<T>());
var slice = this.ToSpan(offset, Unsafe.SizeOf<T>());
this.WriteUnalignedUnsafe(offset, value);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void WriteUnalignedUnsafe<T>(long offset, T value)
{
#if NET7_0_OR_GREATER
Unsafe.WriteUnaligned(ref Unsafe.Add(ref this.value, (IntPtr)offset), value);
#else
var slice = this.ToSpan(offset, Unsafe.SizeOf<T>());
Unsafe.WriteUnaligned(ref slice[0], value);
#endif
}

private static double ReverseEndianness(double value)
Expand Down

0 comments on commit 2ad57fb

Please sign in to comment.