Skip to content

Commit

Permalink
Merge pull request #6 from JasonXuDeveloper/main
Browse files Browse the repository at this point in the history
optimize read/write with empty string + shortName empty string replac…
  • Loading branch information
neuecc authored Sep 28, 2022
2 parents 34fe94d + dd94916 commit bc219a1
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/MemoryPack.Core/MemoryPackWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,15 @@ public void WriteString(string? value)
}

var copyByteCount = value.Length * 2;
ref var src = ref Unsafe.As<char, byte>(ref Unsafe.AsRef(value.GetPinnableReference()));

ref var dest = ref GetSpanReference(copyByteCount + 4);

Unsafe.WriteUnaligned(ref dest, value.Length);
Unsafe.CopyBlockUnaligned(ref Unsafe.Add(ref dest, 4), ref src, (uint)copyByteCount);

if(copyByteCount > 0)
{
ref var src = ref Unsafe.As<char, byte>(ref Unsafe.AsRef(value.GetPinnableReference()));
Unsafe.CopyBlockUnaligned(ref Unsafe.Add(ref dest, 4), ref src, (uint)copyByteCount);
}

Advance(copyByteCount + 4);
}
Expand Down

0 comments on commit bc219a1

Please sign in to comment.