-
-
Notifications
You must be signed in to change notification settings - Fork 202
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 #19 from Cysharp/utf8
UTF8 String serialization
- Loading branch information
Showing
20 changed files
with
490 additions
and
82 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
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,107 @@ | ||
using Benchmark.BenchmarkNetUtilities; | ||
using BinaryPack.Models.Helpers; | ||
using MemoryPack; | ||
using System.Net.Http; | ||
|
||
namespace Benchmark.Benchmarks; | ||
|
||
[PayloadColumn] | ||
public class Utf16VsUtf8 | ||
{ | ||
readonly string ascii; | ||
readonly string japanese; | ||
readonly string largeAscii; | ||
|
||
readonly byte[] utf16Jpn; | ||
readonly byte[] utf8Jpn; | ||
readonly byte[] utf16Ascii; | ||
readonly byte[] utf8Ascii; | ||
readonly byte[] utf16LargeAscii; | ||
readonly byte[] utf8LargeAscii; | ||
|
||
public Utf16VsUtf8() | ||
{ | ||
this.japanese = "あいうえおかきくけこさしすせそたちつてとなにぬねのはひふへほまみむめもやゆよらりるれろわをん"; | ||
this.ascii = "abcedfghijklmnopqrstuvwxyz0123456789"; | ||
this.utf16Jpn = MemoryPackSerializer.Serialize(japanese, MemoryPackSerializeOptions.Utf16); | ||
this.utf8Jpn = MemoryPackSerializer.Serialize(japanese, MemoryPackSerializeOptions.Utf8); | ||
this.utf16Ascii = MemoryPackSerializer.Serialize(ascii, MemoryPackSerializeOptions.Utf16); | ||
this.utf8Ascii = MemoryPackSerializer.Serialize(ascii, MemoryPackSerializeOptions.Utf8); | ||
|
||
this.largeAscii = RandomProvider.NextString(600); | ||
this.utf16LargeAscii = MemoryPackSerializer.Serialize(largeAscii, MemoryPackSerializeOptions.Utf16); | ||
this.utf8LargeAscii = MemoryPackSerializer.Serialize(largeAscii, MemoryPackSerializeOptions.Utf8); | ||
} | ||
|
||
[Benchmark] | ||
public byte[] SerializeUtf16Ascii() | ||
{ | ||
return MemoryPackSerializer.Serialize(ascii, MemoryPackSerializeOptions.Utf16); | ||
} | ||
|
||
[Benchmark] | ||
public byte[] SerializeUtf16Japanese() | ||
{ | ||
return MemoryPackSerializer.Serialize(japanese, MemoryPackSerializeOptions.Utf16); | ||
} | ||
|
||
[Benchmark] | ||
public byte[] SerializeUtf8Ascii() | ||
{ | ||
return MemoryPackSerializer.Serialize(ascii, MemoryPackSerializeOptions.Utf8); | ||
} | ||
|
||
[Benchmark] | ||
public byte[] SerializeUtf8Japanese() | ||
{ | ||
return MemoryPackSerializer.Serialize(japanese, MemoryPackSerializeOptions.Utf8); | ||
} | ||
|
||
[Benchmark] | ||
public byte[] SerializeUtf16LargeAscii() | ||
{ | ||
return MemoryPackSerializer.Serialize(largeAscii, MemoryPackSerializeOptions.Utf16); | ||
} | ||
|
||
[Benchmark] | ||
public byte[] SerializeUtf8LargeAscii() | ||
{ | ||
return MemoryPackSerializer.Serialize(largeAscii, MemoryPackSerializeOptions.Utf8); | ||
} | ||
|
||
[Benchmark] | ||
public void DeserializeUtf16Ascii() | ||
{ | ||
MemoryPackSerializer.Deserialize<string>(utf16Ascii); | ||
} | ||
|
||
[Benchmark] | ||
public void DeserializeUtf16Japanese() | ||
{ | ||
MemoryPackSerializer.Deserialize<string>(utf16Jpn); | ||
} | ||
|
||
[Benchmark] | ||
public void DeserializeUtf8Ascii() | ||
{ | ||
MemoryPackSerializer.Deserialize<string>(utf8Ascii); | ||
} | ||
|
||
[Benchmark] | ||
public void DeserializeUtf8Japanese() | ||
{ | ||
MemoryPackSerializer.Deserialize<string>(utf8Jpn); | ||
} | ||
|
||
[Benchmark] | ||
public void DeserializeUtf16LargeAscii() | ||
{ | ||
MemoryPackSerializer.Deserialize<string>(utf16LargeAscii); | ||
} | ||
|
||
[Benchmark] | ||
public void DeserializeUtf8LargeAscii() | ||
{ | ||
MemoryPackSerializer.Deserialize<string>(utf8LargeAscii); | ||
} | ||
} |
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,39 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Text.Unicode; | ||
using System.Threading.Tasks; | ||
|
||
namespace Benchmark.Micro; | ||
|
||
public class Utf8Decoding | ||
{ | ||
byte[] utf8bytes; | ||
int utf8length; | ||
int utf16length; | ||
|
||
public Utf8Decoding() | ||
{ | ||
// Japanese Hiragana | ||
var text = "あいうえおかきくけこさしすせそたちつてとなにぬねのはひふへほまみむめもやゆよらりるれろわをん"; | ||
utf8bytes = Encoding.UTF8.GetBytes(text); | ||
utf8length = utf8bytes.Length; | ||
utf16length = text.Length; | ||
} | ||
|
||
[Benchmark] | ||
public string UTF8GetString() | ||
{ | ||
return Encoding.UTF8.GetString(utf8bytes); | ||
} | ||
|
||
[Benchmark] | ||
public string Utf16LengthUtf8ToUtf16() | ||
{ | ||
return string.Create(utf16length, utf8bytes, static (dest, source) => | ||
{ | ||
Utf8.ToUtf16(source, dest, out var read, out var written); | ||
}); | ||
} | ||
} |
Oops, something went wrong.