-
-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #59 from Cysharp/improve-custom-formatter
Improve CustomFormatter performance
- Loading branch information
Showing
19 changed files
with
504 additions
and
28 deletions.
There are no files selected for viewing
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,110 @@ | ||
using MemoryPack.Internal; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace MemoryPack.Compression; | ||
|
||
[Preserve] | ||
public sealed class BitPackFormatter : MemoryPackFormatter<bool[]> | ||
{ | ||
public static readonly BitPackFormatter Default = new BitPackFormatter(); | ||
|
||
[Preserve] | ||
public override void Serialize<TBufferWriter>(ref MemoryPackWriter<TBufferWriter> writer, scoped ref bool[]? value) | ||
{ | ||
if (value == null) | ||
{ | ||
writer.WriteNullCollectionHeader(); | ||
return; | ||
} | ||
writer.WriteCollectionHeader(value.Length); | ||
|
||
var data = 0; | ||
var bit = 0; | ||
foreach (var item in value) | ||
{ | ||
Set(ref data, bit, item); | ||
|
||
bit += 1; | ||
|
||
if (bit == 32) | ||
{ | ||
writer.WriteUnmanaged(data); | ||
data = 0; | ||
bit = 0; | ||
} | ||
} | ||
|
||
if (bit != 0) | ||
{ | ||
writer.WriteUnmanaged(data); | ||
} | ||
} | ||
|
||
[Preserve] | ||
public override void Deserialize(ref MemoryPackReader reader, scoped ref bool[]? value) | ||
{ | ||
if (!reader.DangerousTryReadCollectionHeader(out var length)) | ||
{ | ||
value = null; | ||
return; | ||
} | ||
|
||
if (length == 0) | ||
{ | ||
value = Array.Empty<bool>(); | ||
return; | ||
} | ||
|
||
var readCount = ((length - 1) / 32) + 1; | ||
var requireSize = readCount * 4; | ||
if (reader.Remaining < requireSize) | ||
{ | ||
MemoryPackSerializationException.ThrowInsufficientBufferUnless(length); | ||
} | ||
|
||
if (value == null || value.Length != length) | ||
{ | ||
value = new bool[length]; | ||
} | ||
|
||
var bit = 0; | ||
var data = 0; | ||
for (int i = 0; i < value.Length; i++) | ||
{ | ||
if (bit == 0) | ||
{ | ||
reader.ReadUnmanaged(out data); | ||
} | ||
|
||
value[i] = Get(data, bit); | ||
|
||
bit += 1; | ||
|
||
if (bit == 32) | ||
{ | ||
data = 0; | ||
bit = 0; | ||
} | ||
} | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public bool Get(int data, int index) | ||
{ | ||
return (data & (1 << index)) != 0; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public void Set(ref int data, int index, bool value) | ||
{ | ||
int bitMask = 1 << index; | ||
if (value) | ||
{ | ||
data |= bitMask; | ||
} | ||
else | ||
{ | ||
data &= ~bitMask; | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,33 +1,50 @@ | ||
using MemoryPack.Formatters; | ||
using MemoryPack.Compression; | ||
using MemoryPack.Formatters; | ||
|
||
namespace MemoryPack; | ||
|
||
#if !UNITY_2021_2_OR_NEWER | ||
|
||
public sealed class Utf8StringFormatterAttribute : MemoryPackCustomFormatterAttribute<string> | ||
public sealed class Utf8StringFormatterAttribute : MemoryPackCustomFormatterAttribute<Utf8StringFormatter, string> | ||
{ | ||
public override IMemoryPackFormatter<string> GetFormatter() | ||
public override Utf8StringFormatter GetFormatter() | ||
{ | ||
return Utf8StringFormatter.Default; | ||
} | ||
} | ||
|
||
public sealed class Utf16StringFormatterAttribute : MemoryPackCustomFormatterAttribute<string> | ||
public sealed class Utf16StringFormatterAttribute : MemoryPackCustomFormatterAttribute<Utf16StringFormatter, string> | ||
{ | ||
public override IMemoryPackFormatter<string> GetFormatter() | ||
public override Utf16StringFormatter GetFormatter() | ||
{ | ||
return Utf16StringFormatter.Default; | ||
} | ||
} | ||
|
||
public sealed class OrdinalIgnoreCaseStringDictionaryFormatter<TValue> : MemoryPackCustomFormatterAttribute<Dictionary<string, TValue?>> | ||
public sealed class OrdinalIgnoreCaseStringDictionaryFormatter<TValue> : MemoryPackCustomFormatterAttribute<DictionaryFormatter<string, TValue?>, Dictionary<string, TValue?>> | ||
{ | ||
static readonly DictionaryFormatter<string, TValue?> formatter = new DictionaryFormatter<string, TValue?>(StringComparer.OrdinalIgnoreCase); | ||
|
||
public override IMemoryPackFormatter<Dictionary<string, TValue?>> GetFormatter() | ||
public override DictionaryFormatter<string, TValue?> GetFormatter() | ||
{ | ||
return formatter; | ||
} | ||
} | ||
|
||
public sealed class InternStringFormatterAttribute : MemoryPackCustomFormatterAttribute<InternStringFormatter, string> | ||
{ | ||
public override InternStringFormatter GetFormatter() | ||
{ | ||
return InternStringFormatter.Default; | ||
} | ||
} | ||
|
||
public sealed class BitPackFormatterAttribute : MemoryPackCustomFormatterAttribute<BitPackFormatter, bool[]> | ||
{ | ||
public override BitPackFormatter GetFormatter() | ||
{ | ||
return BitPackFormatter.Default; | ||
} | ||
} | ||
|
||
#endif |
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
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
Oops, something went wrong.