• Stars
    star
    2,060
  • Rank 22,444 (Top 0.5 %)
  • Language
    C#
  • License
    MIT License
  • Created almost 5 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Zero Allocation StringBuilder for .NET and Unity.

ZString

GitHub Actions Releases

Zero Allocation StringBuilder for .NET Core and Unity.

  • Struct StringBuilder to avoid allocation of builder itself
  • Rent write buffer from ThreadStatic or ArrayPool
  • All append methods are generics(Append<T>(T value)) and write to buffer directly instead of concatenate value.ToString
  • T1~T16 AppendFormat(AppendFormat<T1,...,T16>(string format, T1 arg1, ..., T16 arg16) avoids boxing of struct argument
  • Also T1~T16 Concat(Concat<T1,...,T16>(T1 arg1, ..., T16 arg16)) avoid boxing and value.ToString allocation
  • Convinient ZString.Format/Concat/Join methods can replace instead of String.Format/Concat/Join
  • Can build both Utf16(Span<char>) and Utf8(Span<byte>) directly
  • Can use inner buffer to avoid allocate final string
  • Integrated with Unity TextMeshPro to avoid string allocation

image

This graph compares following codes.

  • "x:" + x + " y:" + y + " z:" + z
  • ZString.Concat("x:", x, " y:", y, " z:", z)
  • string.Format("x:{0} y:{1} z:{2}", x, y, z)
  • ZString.Format("x:{0} y:{1} z:{2}", x, y, z)
  • new StringBuilder(), Append(), .ToString()
  • ZString.CreateStringBuilder(), Append(), .ToString()

"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.

All ZString methods only allocate final string. Also, ZString has enabled to access inner buffer so if output target has stringless api(like Unity TextMeshPro's SetCharArray), you can achieve completely zero allocation.

The blog post of detailed explanation by author: medium@neuecc/ZString

Related project for loggin using with ZString, Cysharp/ZLogger - Zero Allocation Text/Strcutured Logger.

Table of Contents

Getting Started

For .NET Core, use NuGet.

PM> Install-Package ZString

For Unity, check the releases page, download ZString.Unity.unitypackage.

using Cysharp.Text; // namespace

async void Example(int x, int y, int z)
{
    // same as x + y + z
    _ = ZString.Concat(x, y, z);

    // also can use numeric format strings
    _ = ZString.Format("x:{0}, y:{1:000}, z:{2:P}",x, y, z);

    _ = ZString.Join(',', x, y, z);

    // for Unity, direct write(avoid string allocation completely) to TextMeshPro
    tmp.SetTextFormat("Position: {0}, {1}, {2}", x, y, z);

    // create StringBuilder
    using(var sb = ZString.CreateStringBuilder())
    {
        sb.Append("foo");
        sb.AppendLine(42);
        sb.AppendFormat("{0} {1:.###}", "bar", 123.456789);

        // and build final string
        var str = sb.ToString();

        // for Unity, direct write to TextMeshPro
        tmp.SetText(sb);

        // write to destination buffer
        sb.TryCopyTo(dest, out var written);
    }

    // prepare format, return value should store to field(like RegexOptions.Compile)
    var prepared = ZString.PrepareUtf16<int, int>("x:{0}, y:{1:000}");
    _ = prepared.Format(10, 20);

    // C# 8.0, Using declarations
    // create Utf8 StringBuilder that build Utf8 directly to avoid encoding
    using var sb2 = ZString.CreateUtf8StringBuilder();

    sb2.AppendFormat("foo:{0} bar:{1}", x, y);

    // directly write to steam or dest to avoid allocation
    await sb2.WriteToAsync(stream);
    sb2.CopyTo(bufferWritter);
    sb2.TryCopyTo(dest, out var written);
}

Reference

static class ZString

method returns description
CreateStringBuilder() Utf16ValueStringBuilder Create the Utf16 string StringBuilder.
CreateStringBuilder(bool notNested) Utf16ValueStringBuilder Create the Utf16 string StringBuilder, when true uses thread-static buffer that is faster but must return immediately.
CreateUtf8StringBuilder() Utf8ValueStringBuilder Create the Utf8(Span<byte>) StringBuilder.
CreateUtf8StringBuilder(bool notNested) Utf8ValueStringBuilder Create the Utf8(Span<byte>) StringBuilder, when true uses thread-static buffer that is faster but must return immediately.
Join(char/string, T[]/IE<T>) string Concatenates the elements of an array, using the specified seperator between each element.
PrepareUtf16<T1,..,T16>(string) Utf16PreparedFormat<T1,...,T16> Prepare string format to avoid parse template in each operation.
PrepareUtf8<T1,..,T16>(string) Utf8PreparedFormat<T1,...,T16> Prepare string format to avoid parse template in each operation.
Concat<T1,..,T16>(T1,..,T16) string Concatenates the string representation of some specified values.
Format<T1,..,T16>(string, T1,..,T16) string Replaces one or more format items in a string with the string representation of some specified values.
Utf8Format<T1,..,T16>(IBufferWriter<byte>, T1,..,T16) void Replaces one or more format items in a string with the string representation of some specified values.

struct Utf16ValueStringBuilder : IBufferWriter<char>, IDisposable

method returns description
Length int Length of written buffer.
AsSpan() ReadOnlySpan<char> Get the written buffer data.
AsMemory() ReadOnlyMemory<char> Get the written buffer data.
AsArraySegment() ArraySegment<char> Get the written buffer data.
Dispose() void Return the inner buffer to pool.
Append<T>(T value) void Appends the string representation of a specified value to this instance.
Append<T>(T value, string format) void Appends the string representation of a specified value to this instance with numeric format strings.
AppendJoin(char/string, T[]/IE<T>) void Concatenates and appends the elements of an array, using the specified seperator between each element.
AppendLine() void Appends the default line terminator to the end of this instance.
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.
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.
AppendFormat<T1,..,T16>(string, T1,..,T16) void Appends the string returned by processing a composite format string, each format item is replaced by the string representation of arguments.
TryCopyTo(Span<char>, out int) bool Copy inner buffer to the destination span.
ToString() string Converts the value of this instance to a System.String.
GetMemory(int sizeHint) Memory<char> IBufferWriter.GetMemory.
GetSpan(int sizeHint) Span<char> IBufferWriter.GetSpan.
Advance(int count) void IBufferWriter.Advance.
static RegisterTryFormat<T>(TryFormat<T>) void Register custom formatter.

struct Utf8ValueStringBuilder : IBufferWriter<byte>, IDisposable

method returns description
Length int Length of written buffer.
AsSpan() ReadOnlySpan<char> Get the written buffer data.
AsMemory() ReadOnlyMemory<char> Get the written buffer data.
AsArraySegment() ArraySegment<char> Get the written buffer data.
Dispose() void Return the inner buffer to pool.
Append<T>(T value) void Appends the string representation of a specified value to this instance.
Append<T>(T value, StandardFormat format) void Appends the string representation of a specified value to this instance with numeric format strings.
AppendJoin(char/string, T[]/IE<T>) void Concatenates and appends the elements of an array, using the specified seperator between each element.
AppendLine() void Appends the default line terminator to the end of this instance.
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.
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.
AppendFormat<T1,..,T16>(string, T1,..,T16) void Appends the string returned by processing a composite format string, each format item is replaced by the string representation of arguments.
CopyTo(IBufferWriter<byte>) void Copy inner buffer to the buffer writer.
TryCopyTo(Span<byte>, out int) bool Copy inner buffer to the destination span.
WriteToAsync(Stream stream) Task Write inner buffer to stream.
ToString() string Encode the innner utf8 buffer to a System.String.
GetMemory(int sizeHint) Memory<char> IBufferWriter.GetMemory.
GetSpan(int sizeHint) Span<char> IBufferWriter.GetSpan.
Advance(int count) void IBufferWriter.Advance.
static RegisterTryFormat<T>(TryFormat<T>) void Register custom formatter.

class Utf16PreparedFormat<T1,..,T16>

method returns description
Format string Replaces one or more format items in a string with the string representation of some specified values.
FormatTo<TBufferWriter>(ref TBufferWriter, T1,..,T16) void Replaces one or more format items in a string with the string representation of some specified values.

class Utf8PreparedFormat<T1,..,T16>

method returns description
Format string Replaces one or more format items in a string with the string representation of some specified values.
FormatTo<TBufferWriter>(ref TBufferWriter, T1,..,T16) void Replaces one or more format items in a string with the string representation of some specified values.

class ZStringWriter : TextWriter

A TextWriter implementation that is backed with Utf16ValueStringBuilder
It's important to make sure the writer is always properly disposed.

static class TextMeshProExtensions(Unity only)

method returns description
SetText(Utf16ValueStringBuilder) void Set inner buffer to text mesh pro directly to avoid string allocation.
SetTextFormat<T1,..,T16>(string, T1,..,T16) void Set formatted string without string allocation.

Unity

Install via UPM git URL package or asset package(ZString...*.unitypackage) available in ZString/releases page.

  • https://github.com/Cysharp/ZString.git?path=src/ZString.Unity/Assets/Scripts/ZString

If you want to set a target version, ZString uses the *.*.* release tag so you can specify a version like #2.4.0. For example https://github.com/Cysharp/ZString.git?path=src/ZString.Unity/Assets/Scripts/ZString#2.4.0.

Supporting minimum Unity version is 2021.3. The dependency managed DLL System.Runtime.CompilerServices.Unsafe/6.0.0 is included with unitypackage. For git references, you will need to add them in another way as they are not included to avoid unnecessary dependencies; either extract the dll from unitypackage or download it from the NuGet page.

Advanced Tips

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.

using(var sb = ZString.CreateStringBuilder(true))
{
    sb.Append("foo");

    using var sb2 = ZString.CreateStringBuilder(true); // NG, nested stringbuilder uses conflicted same buffer
    var str = ZString.Concat("x", 100); // NG, ZString.Concat/Join/Format uses threadstatic buffer
}
// OK, return buffer immediately.
using(var sb = ZString.CreateStringBuilder(true))
{
    sb.Append("foo");
    return sb.ToString();
}

ZString.CreateStringBuilder() is same as ZString.CreateStringBuilder(notNested:false).


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.

If you want to avoid to convert string in custom type, you can register your own formatter.

Utf16ValueStringBuilder.RegisterTryFormat((MyStruct value, Span<char> destination, out int charsWritten, ReadOnlySpan<char> format) =>
{
    // write value to destionation and set size to charsWritten.
    charsWritten = 0;
    return true;
});

Utf8ValueStringBuilder.RegisterTryFormat((MyStruct value, Span<byte> destination, out int written, StandardFormat format) =>
{
    written = 0;
    return true;
});

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.


Utf16ValueStringBuilder and Utf8ValueStringBuilder are mutable struct, be careful to copy by passing method. Use ref and try-finally.

void Build()
{
    var sb = ZString.CreateStringBuilder();
    try
    {
        BuildHeader(ref sb);
        BuildMessage(ref sb);
    }
    finally
    {
        // when use with `ref`, can not use `using`.
        sb.Dispose();
    }
}


void BuildHeader(ref Utf16ValueStringBuilder builder)
{
    //..
}

void BuildMessage(ref Utf16ValueStringBuilder builder)
{
    //..
}

Utf8Format, Utf8StringBuilder uses Utf8Formatter.TryFormat and there format string is not same as standard format. It uses StandardFormat, combinate of symbol char and precision. Supported format string symbol can find in Utf8Formatter.TryFormat document(For example Int32 supports G, D, N, X and Boolean supports G, I). Precision(zero padding) can pass after symbol like D2. For example sb.AppendFormat("{0:D2}:{1:D2}:{2:D2}", hour, minute, second).

TryFormat(DateTime) and TryFormat(TimeSpan) symbol is too restricted than standard string format. If you want to use custom format, deconstruct there Day, Hour, etc.


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,

using var sb = ZString.CreateUtf8StringBuilder();
IBufferWriter<byte> boxed = sb;
var writer = new Utf8JsonWriter(boxed);
JsonSerializer.Serialize(writer, ....);

using var unboxed = (Utf8ValueStringBuilder)boxed;
var str = unboxed.ToString();

License

This library is licensed under the MIT License.

.NET Standard 2.0 and Unity version borrows dotnet/runtime conversion methods, there exists under ZString/Number directory. This third-party license follows runtime/LICENSE.TXT.

More Repositories

1

UniTask

Provides an efficient allocation free async/await integration for Unity.
C#
8,201
star
2

MagicOnion

Unified Realtime/API framework for .NET platform and Unity.
C#
3,838
star
3

MemoryPack

Zero encoding extreme performance binary serializer for C# and Unity.
C#
3,288
star
4

R3

The new future of dotnet/reactive and UniRx.
C#
2,177
star
5

ConsoleAppFramework

Zero Dependency, Zero Overhead, Zero Reflection, Zero Allocation, AOT Safe CLI Framework powered by C# Source Generator.
C#
1,635
star
6

MasterMemory

Embedded Typed Readonly In-Memory Document Database for .NET and Unity.
C#
1,521
star
7

MessagePipe

High performance in-memory/distributed messaging pipeline for .NET and Unity.
C#
1,406
star
8

Ulid

Fast .NET C# Implementation of ULID for .NET and Unity.
C#
1,314
star
9

ZLogger

Zero Allocation Text/Structured Logger for .NET with StringInterpolation and Source Generator, built on top of a Microsoft.Extensions.Logging.
C#
1,262
star
10

SimdLinq

Drop-in replacement of LINQ aggregation operations extremely faster with SIMD.
C#
775
star
11

csbindgen

Generate C# FFI from Rust for automatically brings native code and C native library to .NET and Unity.
Rust
688
star
12

ObservableCollections

High performance observable collections and synchronized views, for WPF, Blazor, Unity.
C#
559
star
13

ProcessX

Simplify call an external process with the async streams in C# 8.0.
C#
453
star
14

YetAnotherHttpHandler

YetAnotherHttpHandler brings the power of HTTP/2 (and gRPC) to Unity and .NET Standard.
C#
354
star
15

UnitGenerator

C# Source Generator to create value-object, inspired by units of measure.
C#
330
star
16

RuntimeUnitTestToolkit

CLI/GUI Frontend of Unity Test Runner to test on any platform.
C#
300
star
17

AlterNats

An alternative high performance NATS client for .NET.
C#
284
star
18

NativeMemoryArray

Utilized native-memory backed array for .NET and Unity - over the 2GB limitation and support the modern API(IBufferWriter, ReadOnlySequence, scatter/gather I/O, etc...).
C#
276
star
19

StructureOfArraysGenerator

Structure of arrays source generator to make CPU Cache and SIMD friendly data structure for high-performance code in .NET and Unity.
C#
262
star
20

MagicPhysX

.NET PhysX 5 binding to all platforms(win, osx, linux) for 3D engine, deep learning, dedicated server of gaming.
Rust
258
star
21

PrivateProxy

Source Generator and .NET 8 UnsafeAccessor based high-performance strongly-typed private accessor for unit testing and runtime.
C#
239
star
22

KcpTransport

KcpTransport is a Pure C# implementation of RUDP for high-performance real-time network communication
C#
237
star
23

LogicLooper

A library for building server application using loop-action programming model on .NET.
C#
237
star
24

DFrame

Distributed load testing framework for .NET and Unity.
C#
223
star
25

Utf8StreamReader

Utf8 based StreamReader for high performance text processing.
C#
208
star
26

LitJWT

Lightweight, Fast JWT(JSON Web Token) implementation for .NET.
C#
199
star
27

Claudia

Unofficial Anthropic Claude API client for .NET.
C#
162
star
28

CsprojModifier

CsprojModifier performs additional processing when Unity Editor generates the .csproj.
C#
154
star
29

Utf8StringInterpolation

Successor of ZString; UTF8 based zero allocation high-peformance String Interpolation and StringBuilder.
C#
153
star
30

ValueTaskSupplement

Append supplemental methods(WhenAny, WhenAll, Lazy) to ValueTask.
C#
135
star
31

Kokuban

Simplifies styling strings in the terminal for .NET application.
C#
123
star
32

SlnMerge

SlnMerge merges the solution files when generating solution file by Unity Editor.
C#
114
star
33

GrpcWebSocketBridge

Yet Another gRPC over HTTP/1 using WebSocket implementation, primarily targets .NET platform.
C#
76
star
34

WebSerializer

Convert Object into QueryString/FormUrlEncodedContent for C# HttpClient REST Request.
C#
65
star
35

RandomFixtureKit

Fill random/edge-case value to target type for unit testing, supports both .NET Standard and Unity.
C#
46
star
36

Actions

41
star
37

DocfxTemplate

Patchworked DocFX v2 template for Cysharp
JavaScript
7
star
38

Multicaster

A framework for transparently invoking multiple instances or clients.
C#
5
star
39

com.unity.ide.visualstudio-backport

Backport of com.unity.ide.visualstudio to before Unity 2019.4.21
C#
1
star