Coder Social home page Coder Social logo

linkdotnet / stringbuilder Goto Github PK

View Code? Open in Web Editor NEW
76.0 1.0 5.0 808 KB

A fast and low allocation StringBuilder for .NET.

Home Page: https://linkdotnet.github.io/StringBuilder

License: MIT License

C# 100.00%
csharp dotnet performance string stringbuilder string-builder valuestringbuilder

stringbuilder's Introduction

Header

Welcome

Hey lot, my name is Steven and I am a .NET developer based in Zurich. Welcome to my public page. I am most likely interested in new technologies. If you want to know more just check out the links down below.

I also have a blog here and if you are interested in the source code of it, here you go.

Social

Linkedin Badge Stackoverflow Badge Github Bade Blog Blade

Github

Metrics

stringbuilder's People

Contributors

akoken avatar dependabot[bot] avatar github-actions[bot] avatar linkdotnet avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

stringbuilder's Issues

Use rope-like structure with `ref` fields

In the current setup the ValueStringBuilder has one large array internally. Where this is fine for most cases, it can get tricky for either very big buffers (>85kb) or if we have to grow the buffer a lot.

A different approach would be to use a rope-like structure with a fixed size (kind of like the real StringBuilder that has always chunks of 8000 characters).

In contrast to System.Text.StringBuilder, one could try to create always new ropes when appending. So our ValueStringBuilder would be structured like a linked list. When appending a string we add another instance of ValueStringBuilder to the current one.

The internal structure could look like this:

public ref struct ValueStringBuilder
{
    public ref ValueStringBuilder next;

    public void Append(ReadOnlySpan<char> string)
    {
        ref var nextInstance = next;
        while(!Unsafe.IsNullRef(nextInstance.next)) { nextInstance = nextInstance.next; }

        nextInstance.next = new ValueStringBuilder(string);
    }
}

Currently this is not allowed with C# 11.

Add `AppendFormat` methods

The System.Text.StringBuilder offers some AppendFormat methods that make life easier if you want to format your values:

var sb = new StringBuilder();
sb.AppendFormat("{0} + {1} = {2}", 1, 2, 3);

It would be nice to have the same set of methods for the ValueStringBuilder. One major difference would be that the ValueStringBuilder would offer non-boxed versions:

public ref struct ValueStringBuilder
{
    public void AppendFormat<T>(ReadOnlySpan<char> format, T arg1);
    public void AppendFormat<T1, T2>(ReadOnlySpan<char> format, T1 arg1, T2 arg2);
    public void AppendFormat<T1, T2, T3>(ReadOnlySpan<char> format, T1 arg1, T2 arg2, T3 arg3);
    // ...
}

We want to avoid boxing and unboxing as much as possible. Still, there can be a convenient function that takes a params object[] as input (maybe it should have a different naming to indicate the boxing/unboxing nature).

Is it worth renaming private variables?

Hi! I like your project! But, I have a question, is it worth renaming private variables?
I ask this because in the C# convention they recommend using underscore to private variables.

Curious about struct layout and packing

Hey, man. I was reading up on .NET struct memory layout, and I remembered something about your struct that I was curious about. Why do you declare your struct to have sequential layout while at the same time ordering the fields in a way that, with packing, causes it to have 4 bytes of wasted space on x64, especially since it's not intended to be used in an array?

ZString benchmarks are wrong and misleading

You never dispose the ZString instance which means that the pooled buffer is never returned. This will dramatically impact performance.

As an aside, is this library based off of the dotnet/runtime ValueStringBuiilder? It seems to be nearly the same implementation, just split up.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.