Skip to content

Commit 06597e3

Browse files
authored
Merge pull request #4 from Cysharp/improve-perf
1.0.0 rewrite
2 parents 031b2b1 + 42f8e50 commit 06597e3

File tree

145 files changed

+28008
-13647
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

145 files changed

+28008
-13647
lines changed

.circleci/config.yml

+10-6
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ jobs:
4646
name: unity
4747
version: << parameters.unity_version >>
4848
steps:
49+
- run: apt update && apt install git -y
4950
- checkout
5051
- unity_activate:
5152
unity_version: << parameters.unity_version >>
@@ -96,12 +97,6 @@ workflows:
9697
version: 2
9798
default-pipeline:
9899
jobs:
99-
- build-unity:
100-
unity_version: 2019.1.2f1
101-
unity_license: ${UNITY_LICENSE_2019_1}
102-
filters:
103-
tags:
104-
only: /.*/
105100
- build-test:
106101
filters:
107102
tags:
@@ -110,6 +105,14 @@ workflows:
110105
# filters:
111106
# tags:
112107
# only: /.*/
108+
- build-unity:
109+
unity_version: 2019.1.2f1
110+
unity_license: ${UNITY_LICENSE_2019_1}
111+
filters:
112+
tags:
113+
only: /^\d\.\d\.\d.*/
114+
branches:
115+
ignore: /.*/
113116
- build-push:
114117
filters:
115118
tags:
@@ -119,6 +122,7 @@ workflows:
119122
- upload-github:
120123
requires:
121124
- build-unity
125+
- build-push
122126
filters:
123127
tags:
124128
only: /^\d\.\d\.\d.*/

README.md

+176-44
Original file line numberDiff line numberDiff line change
@@ -4,81 +4,213 @@ ZString
44

55
**Z**ero Allocation **String**Builder for .NET Core and Unity.
66

7-
Currently Preview Release, -0.1.0.
7+
* Struct StringBuilder to avoid allocation of builder itself
8+
* Rent write buffer from `ThreadStatic` or `ArrayPool`
9+
* All append methods are generics(`Append<T>(T value)`) and write to buffer directly instead of concatenate `value.ToString`
10+
* `T0`~`T15` AppendFormat(`AppendFormat<T0,...,T15>(string format, T0 arg0, ..., T15 arg15)` avoids boxing of stuct argument
11+
* Also `T0`~`T15` Concat(`Concat<T0,...,T15>(T0 arg0, ..., T15 arg15)`) avoid boxing and `value.ToString` allocation
12+
* Convinient `ZString.Format/Concat/Join` methods can replace instead of `String.Format/Concat/Join`
13+
* Can use inner buffer to avoid allocate final string
14+
* Can build both Utf16(`Span<char>`) and Utf8(`Span<byte>`) directly
815

9-
Getting Started(Unity, with TextMeshPro)
16+
![image](https://user-images.githubusercontent.com/46207/74473217-9061e200-4ee6-11ea-9a77-14d740886faa.png)
17+
18+
This graph compares following codes.
19+
20+
* `"x:" + x + " y:" + y + " z:" + z`
21+
* `ZString.Concat("x:", x, " y:", y, " z:", z)`
22+
* `string.Format("x:{0} y:{1} z:{2}", x, y, z)`
23+
* `ZString.Format("x:{0} y:{1} z:{2}", x, y, z)`
24+
* `new StringBuilder(), Append(), .ToString()`
25+
* `ZString.CreateStringBuilder(), Append(), .ToString()`
26+
27+
`"x:" + x + " y:" + y + " z:" + z` is converted to `String.Concat(new []{ "x:", x.ToString(), " y:", y.ToString(), " z:", z.ToString() })` by C# compiler. It has each `.ToString` allocation and params array allocation. `string.Format` calls `String.Format(string, object, object, object)` so each arguments causes int -> object boxing.
28+
29+
All `ZString` methods only allocate final string. Also, `ZString` has enabled to access inner buffer so if output target has stringless api, you can achieve completely zero allocation.
30+
31+
Getting Started
1032
---
11-
Check the [releases](https://github.com/Cysharp/ZString/releases) page, download `ZString.Unity.unitypackage`.
33+
For .NET Core, use NuGet.
1234

13-
```csharp
14-
using Cysharp.Text; // namespace
35+
> PM> Install-Package [ZString](https://www.nuget.org/packages/ZString)
1536
16-
TextMeshProUGUI text; // TMP_Text
17-
int count = 0;
37+
For Unity, check the [releases](https://github.com/Cysharp/ZString/releases) page, download `ZString.Unity.unitypackage`.
1838

19-
void Update()
39+
```csharp
40+
async void Example(int x, int y, int z)
2041
{
21-
text.SetTextFormat("Damage: {0}", count++);
22-
}
23-
```
42+
// same as x + y + z
43+
_ = ZString.Concat(x, y, z);
2444

25-
SetTextFormat is extension method of `TMP_Text`, there parameter is generics so can avoid boxing, and ZString writes to buffer directly without any ToString allocation. Finally inner buffer copy to `TextMeshPro` buffer so avoid all string allocations.
45+
// also can use numeric format strings
46+
_ = ZString.Format("x:{0}, y:{1:000}, z:{2:P}",x, y, z);
2647

27-
```csharp
28-
public static void SetTextFormat<T0>(this TMP_Text text, string format, T0 arg0)
29-
public static void SetTextFormat<T0, T1>(this TMP_Text text, string format, T0 arg0, T1 arg1)
30-
// ...
31-
public static void SetTextFormat<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15>(this TMP_Text text, string format, T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15)
48+
_ = ZString.Join(',', x, y, z);
49+
50+
// for Unity, direct write(avoid string allocation completely) to TextMeshPro
51+
tmp.SetTextFormat("Position: {0}, {1}, {2}", x, y, z);
52+
53+
// create StringBuilder
54+
using(var sb = ZString.CreateStringBuilder())
55+
{
56+
sb.Append("foo");
57+
sb.AppendLine(42);
58+
sb.AppendFormat("{0} {1:.###}", "bar", 123.456789);
59+
60+
// and build final string
61+
var str = sb.ToString();
62+
63+
// for Unity, direct write to TextMeshPro
64+
tmp.SetText(sb);
65+
66+
// write to destination buffer
67+
sb.TryCopyTo(dest, out var written);
68+
}
69+
70+
// C# 8.0, Using declarations
71+
// create Utf8 StringBuilder that build Utf8 directly to avoid encoding
72+
using var sb2 = ZString.CreateUtf8StringBuilder();
73+
74+
sb2.Concat("foo:", x, ", bar:", y);
75+
76+
// directly write to steam or dest to avoid allocation
77+
await sb2.WriteToAsync(stream);
78+
sb2.TryCopyTo(dest, out var written);
79+
}
3280
```
3381

34-
Raw API is start from `ZString.CreateStringBuilder();`.
82+
Reference
83+
---
84+
**static class ZString**
85+
86+
| method | returns | description |
87+
| -- | -- | -- |
88+
| CreateStringBuilder() | Utf16ValueStringBuilder | Create the Utf16 string StringBuilder. |
89+
| CreateStringBuilder(bool notNested) | Utf16ValueStringBuilder | Create the Utf16 string StringBuilder, when true uses thread-static buffer that is faster but must return immediately. |
90+
| CreateUtf8StringBuilder() | Utf8ValueStringBuilder | Create the Utf8(`Span<byte>`) StringBuilder. |
91+
| CreateUtf8StringBuilder(bool notNested) | Utf8ValueStringBuilder | Create the Utf8(`Span<byte>`) StringBuilder, when true uses thread-static buffer that is faster but must return immediately. |
92+
| `Join(char|string, T[]/IE<T>)` | string | Concatenates the elements of an array, using the specified seperator between each element. |
93+
| `Concat<T0,..,T15>(T0,..,T15)` | string | Concatenates the string representation of some specified values. |
94+
| `Format<T0,..,T15>(string, T0,..,T15)` | string | Replaces one or more format items in a string with the string representation of some specified values. |
95+
96+
**struct Utf16ValueStringBuilder : `IBufferWriter<char>`, IDisposable**
97+
98+
| method | returns | description |
99+
| -- | -- | -- |
100+
| Length | int | Length of written buffer. |
101+
| AsSpan() | `ReadOnlySpan<char>` | Get the written buffer data. |
102+
| AsMemory() | `ReadOnlyMemory<char>` | Get the written buffer data. |
103+
| AsArraySegment() | `ArraySegment<char>` | Get the written buffer data. |
104+
| Dispose() | void | Return the inner buffer to pool. |
105+
| `Append<T>(T value)` | void | Appends the string representation of a specified value to this instance. |
106+
| `Append<T>(T value, string format)` | void | Appends the string representation of a specified value to this instance with numeric format strings. |
107+
| `AppendLine()` | void | Appends the default line terminator to the end of this instance. |
108+
| `AppendLine<T>(T value)` | void | Appends the string representation of a specified value followed by the default line terminator to the end of this instance. |
109+
| `AppendLine<T>(T value, string format)` | void | Appends the string representation of a specified value with numeric format strings followed by the default line terminator to the end of this instance. |
110+
| `AppendFormat<T0,..,T15>(string, T0,..,T15)` | void | Appends the string returned by processing a composite format string, each format item is replaced by the string representation of arguments. |
111+
| `TryCopyTo(Span<char>, out int)` | bool | Copy inner buffer to the destination span. |
112+
| ToString() | string | Converts the value of this instance to a System.String. |
113+
| GetMemory(int sizeHint) | `Memory<char>` | IBufferWriter.GetMemory. |
114+
| GetSpan(int sizeHint) | `Span<char>` | IBufferWriter.GetSpan. |
115+
| Advance(int count) | void | IBufferWriter.Advance. |
116+
| static `RegisterTryFormat<T>(TryFormat<T>)` | void | Register custom formatter. |
117+
118+
**struct Utf8ValueStringBuilder : `IBufferWriter<byte>`, IDisposable**
119+
120+
| method | returns | description |
121+
| -- | -- | -- |
122+
| Length | int | Length of written buffer. |
123+
| AsSpan() | `ReadOnlySpan<char>` | Get the written buffer data. |
124+
| AsMemory() | `ReadOnlyMemory<char>` | Get the written buffer data. |
125+
| AsArraySegment() | `ArraySegment<char>` | Get the written buffer data. |
126+
| Dispose() | void | Return the inner buffer to pool. |
127+
| `Append<T>(T value)` | void | Appends the string representation of a specified value to this instance. |
128+
| `Append<T>(T value, StandardFormat format)` | void | Appends the string representation of a specified value to this instance with numeric format strings. |
129+
| `AppendLine()` | void | Appends the default line terminator to the end of this instance. |
130+
| `AppendLine<T>(T value)` | void | Appends the string representation of a specified value followed by the default line terminator to the end of this instance. |
131+
| `AppendLine<T>(T value, StandardFormat format)` | void | Appends the string representation of a specified value with numeric format strings followed by the default line terminator to the end of this instance. |
132+
| `AppendFormat<T0,..,T15>(string, T0,..,T15)` | void | Appends the string returned by processing a composite format string, each format item is replaced by the string representation of arguments. |
133+
| `TryCopyTo(Span<byte>, out int)` | bool | Copy inner buffer to the destination span. |
134+
| WriteToAsync(Stream stream) | Task | Write inner buffer to stream. |
135+
| ToString() | string | Encode the innner utf8 buffer to a System.String. |
136+
| GetMemory(int sizeHint) | `Memory<char>` | IBufferWriter.GetMemory. |
137+
| GetSpan(int sizeHint) | `Span<char>` | IBufferWriter.GetSpan. |
138+
| Advance(int count) | void | IBufferWriter.Advance. |
139+
| static `RegisterTryFormat<T>(TryFormat<T>)` | void | Register custom formatter. |
140+
141+
**static class TextMeshProExtensions**(Unity only)
142+
143+
| method | returns | description |
144+
| -- | -- | -- |
145+
| SetText(Utf16ValueStringBuilder) | void | Set inner buffer to text mesh pro directly to avoid string allocation. |
146+
| `SetTextFormat<T0,..,T15>(string, T0,..,T15)` | void | Set formatted string without string allocation. |
147+
148+
Advanced Tips
149+
---
150+
`ZString.CreateStringBuilder(notNested:true)` is a special optimized parameter that uses `ThreadStatic` buffer instead of rent from `ArrayPool`. It is slightly faster but can not use in nested.
35151

36152
```csharp
37-
using(var sb = ZString.CreateStringBuilder())
153+
using(var sb = ZString.CreateStringBuilder(true))
38154
{
39155
sb.Append("foo");
40-
sb.AppendLine(42);
41-
sb.AppendFormat("{0} {1}", "bar", 123.456);
42-
sb.AppendMany(1, "foo", 100, "bar");
43156

44-
Debug.Log(sb.ToString());
157+
using var sb2 = ZString.CreateStringBuilder(true); // NG, nested stringbuilder uses conflicted same buffer
158+
var str = ZString.Concat("x", 100); // NG, ZString.Concat/Join/Format uses threadstatic buffer
45159
}
160+
```
46161

47-
// If you want to use only format, use `ZString.Format` instead of `String.Format`.
48-
var str = ZString.Format("foo {0} bar {1}", 42, 123.456);
162+
```csharp
163+
// OK, return buffer immediately.
164+
using(var sb = ZString.CreateStringBuilder(true))
165+
{
166+
sb.Append("foo");
167+
return sb.ToString();
168+
}
49169
```
50170

51-
Getting Started(.NET Core)
171+
`ZString.CreateStringBuilder()` is same as `ZString.CreateStringBuilder(notNested:false)`.
172+
52173
---
53174

54-
> PM> Install-Package [ZString](https://www.nuget.org/packages/ZString)
175+
In default, `SByte`, `Int16`, `Int32`, `Int64`, `Byte`, `UInt16`, `UInt32`, `UInt64`, `Single`, `Double`, `TimeSpan`, `DateTime`, `DateTimeOffset`, `Decimal`, `Guid`, `String`, `Char` are used there own formatter to avoid `.ToString()` allocation, write directly to buffer. If not exists there list type, used `.ToString()` and copy string data.
176+
177+
If you want to avoid to convert string in custom type, you can register your own formatter.
55178

56179
```csharp
57-
using var sb = ZString.CreateStringBuilder();
180+
Utf16ValueStringBuilder.RegisterTryFormat((MyStruct value, Span<char> destination, out int charsWritten, ReadOnlySpan<char> format) =>
181+
{
182+
// write value to destionation and set size to charsWritten.
183+
charsWritten = 0;
184+
return true;
185+
});
58186

59-
sb.AppendLine("foo");
60-
sb.AppendLine(42);
61-
sb.AppendLine("bar");
62-
sb.AppendLine(123.456);
187+
Utf8ValueStringBuilder.RegisterTryFormat((MyStruct value, Span<byte> destination, out int written, StandardFormat format) =>
188+
{
189+
written = 0;
190+
return true;
191+
});
192+
```
63193

64-
Console.WriteLine(sb.ToString());
194+
---
65195

66-
// format, shortform
67-
var str = ZString.Format("foo {0} bar {1}", 42, 123.456);
68-
```
196+
`CreateStringBuilder` and `CreateUtf8StringBuilder` must use with `using`. Because their builder rent 64K buffer from `ArrayPool`. If not return buffer, allocate 64K buffer when string builder is created.
197+
198+
---
199+
200+
`Utf8ValueStringBuilder` and `Utf16ValueStringBuilder` implements `IBufferWriter` so you can pass serializer(such as `JsonSerializer` of `System.Text.Json`). But be careful to boxing copy, `ValueStringBuilder` is mutable struct. For example,
69201

70202
```csharp
71-
// write to Utf8 directly
72203
using var sb = ZString.CreateUtf8StringBuilder();
204+
IBufferWriter<byte> boxed = sb;
205+
var writer = new Utf8JsonWriter(boxed);
206+
JsonSerializer.Serialize(writer, ....);
73207

74-
sb.AppendLine("foo");
75-
sb.AppendLine(42);
76-
sb.AppendLine("bar");
77-
sb.AppendLine(123.456);
78-
79-
await sb.CopyToAsync(stream);
208+
using var unboxed = (Utf8ValueStringBuilder)boxed;
209+
var str = unboxed.ToString();
80210
```
81211

82212
License
83213
---
84-
This library is under the MIT License.
214+
This library is licensed under the the MIT License.
215+
216+
.NET Standard 2.0 and Unity version borrows [dotnet/runtime](https://github.com/dotnet/runtime) conversion methods, there exists under `ZString/Number` directory.

ZString.sln

+10-3
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,18 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "sandbox", "sandbox", "{A7D7
1717
EndProject
1818
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{0803618F-C4E8-4D37-831E-5D26C5574F49}"
1919
EndProject
20-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ZString", "src\ZString\ZString.csproj", "{7B09D422-D19A-457E-ADA0-4CDC2DC581BB}"
20+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ZString", "src\ZString\ZString.csproj", "{7B09D422-D19A-457E-ADA0-4CDC2DC581BB}"
2121
EndProject
22-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApp", "sandbox\ConsoleApp\ConsoleApp.csproj", "{9ADF67E1-1872-43D3-882E-607071726FE7}"
22+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleApp", "sandbox\ConsoleApp\ConsoleApp.csproj", "{9ADF67E1-1872-43D3-882E-607071726FE7}"
2323
EndProject
24-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ZString.Tests", "tests\ZString.Tests\ZString.Tests.csproj", "{62090C00-9727-4375-BE40-ABE2F4D41571}"
24+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ZString.Tests", "tests\ZString.Tests\ZString.Tests.csproj", "{62090C00-9727-4375-BE40-ABE2F4D41571}"
2525
EndProject
2626
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleAppNet472", "sandbox\ConsoleAppNet472\ConsoleAppNet472.csproj", "{BE8A17AA-504A-410D-B86D-92431B0F5594}"
2727
EndProject
2828
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ZString.NetCore2Tests", "tests\ZString.NetCore2Tests\ZString.NetCore2Tests.csproj", "{62C44156-F55C-4006-B9A2-108DAB340FAC}"
2929
EndProject
30+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PerfBenchmark", "sandbox\PerfBenchmark\PerfBenchmark.csproj", "{D766AEB3-3609-4F1D-8D81-5549F748F372}"
31+
EndProject
3032
Global
3133
GlobalSection(SolutionConfigurationPlatforms) = preSolution
3234
Debug|Any CPU = Debug|Any CPU
@@ -53,6 +55,10 @@ Global
5355
{62C44156-F55C-4006-B9A2-108DAB340FAC}.Debug|Any CPU.Build.0 = Debug|Any CPU
5456
{62C44156-F55C-4006-B9A2-108DAB340FAC}.Release|Any CPU.ActiveCfg = Release|Any CPU
5557
{62C44156-F55C-4006-B9A2-108DAB340FAC}.Release|Any CPU.Build.0 = Release|Any CPU
58+
{D766AEB3-3609-4F1D-8D81-5549F748F372}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
59+
{D766AEB3-3609-4F1D-8D81-5549F748F372}.Debug|Any CPU.Build.0 = Debug|Any CPU
60+
{D766AEB3-3609-4F1D-8D81-5549F748F372}.Release|Any CPU.ActiveCfg = Release|Any CPU
61+
{D766AEB3-3609-4F1D-8D81-5549F748F372}.Release|Any CPU.Build.0 = Release|Any CPU
5662
EndGlobalSection
5763
GlobalSection(SolutionProperties) = preSolution
5864
HideSolutionNode = FALSE
@@ -63,6 +69,7 @@ Global
6369
{62090C00-9727-4375-BE40-ABE2F4D41571} = {0803618F-C4E8-4D37-831E-5D26C5574F49}
6470
{BE8A17AA-504A-410D-B86D-92431B0F5594} = {A7D7AA7D-9A79-48A8-978D-0C98EBD81ED0}
6571
{62C44156-F55C-4006-B9A2-108DAB340FAC} = {0803618F-C4E8-4D37-831E-5D26C5574F49}
72+
{D766AEB3-3609-4F1D-8D81-5549F748F372} = {A7D7AA7D-9A79-48A8-978D-0C98EBD81ED0}
6673
EndGlobalSection
6774
GlobalSection(ExtensibilityGlobals) = postSolution
6875
SolutionGuid = {DF39BF43-3E0E-4F7D-9943-7E50D301234D}

docs/graph.xlsx

15.6 KB
Binary file not shown.

sandbox/ConsoleApp/ConsoleApp.csproj

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

3-
<PropertyGroup>
4-
<OutputType>Exe</OutputType>
5-
<TargetFramework>netcoreapp3.1</TargetFramework>
6-
</PropertyGroup>
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp3.1</TargetFramework>
6+
</PropertyGroup>
77

8-
<ItemGroup>
9-
<ProjectReference Include="..\..\src\ZString\ZString.csproj" />
10-
</ItemGroup>
8+
<ItemGroup>
9+
<PackageReference Include="StringFormatter" Version="1.0.0.13" />
10+
<PackageReference Include="System.Text.Json" Version="4.7.0" />
11+
<ProjectReference Include="..\..\src\ZString\ZString.csproj" />
12+
</ItemGroup>
1113

1214
</Project>

0 commit comments

Comments
 (0)