Skip to content

Commit 5a7248a

Browse files
authored
Merge pull request #236 from Joy-less/improve-readme
Improve README.md
2 parents 55cb052 + 59aaac8 commit 5a7248a

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

README.md

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,38 +14,43 @@ Afterward, use the package as follow:
1414
```csharp
1515
using LinkDotNet.StringBuilder; // Namespace of the package
1616
17-
ValueStringBuilder stringBuilder = new ValueStringBuilder();
17+
using ValueStringBuilder stringBuilder = new();
1818
stringBuilder.AppendLine("Hello World");
1919

2020
string result = stringBuilder.ToString();
2121
```
2222

2323
There are also smaller helper functions, which enable you to use `ValueStringBuilder` without any instance:
2424
```csharp
25-
using LinkDotNet.StringBuilder;
25+
string result1 = ValueStringBuilder.Concat("Hello ", "World"); // "Hello World"
26+
string result2 = ValueStringBuilder.Concat("Hello", 1, 2, 3, "!"); // "Hello123!"
27+
```
2628

27-
_ = ValueStringBuilder.Concat("Hello ", "World"); // "Hello World"
28-
_ = ValueStringBuilder.Concat("Hello", 1, 2, 3, "!"); // "Hello123!"
29+
By default, `ValueStringBuilder` uses a rented buffer from `ArrayPool<char>.Shared`.
30+
You can avoid renting overhead with an initially stack-allocated buffer:
31+
```csharp
32+
using ValueStringBuilder stringBuilder = new(stackalloc char[128]);
2933
```
34+
Note that this will prevent you from returning `stringBuilder` or assigning it to an `out` parameter.
3035

3136
## What does it solve?
3237
The dotnet version of the `StringBuilder` is an all-purpose version that normally fits a wide variety of needs.
3338
But sometimes, low allocation is key. Therefore I created the `ValueStringBuilder`. It is not a class but a `ref struct` that tries to allocate as little as possible.
3439
If you want to know how the `ValueStringBuilder` works and why it uses allocations and is even faster, check out [this](https://steven-giesel.com/blogPost/4cada9a7-c462-4133-ad7f-e8b671987896) blog post.
3540
The blog goes into a bit more in detail about how it works with a simplistic version of the `ValueStringBuilder`.
3641

37-
## What it doesn't solve!
38-
The library is not meant as a general replacement for the `StringBuilder` shipped with the .net framework itself. You can head over to the documentation and read about the ["Known limitations"](https://linkdotnet.github.io/StringBuilder/articles/known_limitations.html).
39-
The library works best for a small to medium amount of strings (not multiple 100'000 characters, even though it can be still faster and uses fewer allocations). At any time, you can convert the `ValueStringBuilder` to a "normal" `StringBuilder` and vice versa.
42+
## What doesn't it solve?
43+
The library is not meant as a general replacement for the `StringBuilder` built into .NET. You can head over to the documentation and read about the ["Known limitations"](https://linkdotnet.github.io/StringBuilder/articles/known_limitations.html).
44+
The library works best for a small to medium length strings (not hundreds of thousands of characters, even though it can be still faster and performs fewer allocations). At any time, you can convert the `ValueStringBuilder` to a "normal" `StringBuilder` and vice versa.
4045

4146
The normal use case is to concatenate strings in a hot path where the goal is to put as minimal pressure on the GC as possible.
4247

4348
## Documentation
44-
More detailed documentation can be found [here](https://linkdotnet.github.io/StringBuilder/). It is really important to understand how the `ValueStringBuilder` works so that you did not run into weird situations where performance/allocations can even rise.
49+
More detailed documentation can be found [here](https://linkdotnet.github.io/StringBuilder). It is really important to understand how the `ValueStringBuilder` works so that you did not run into weird situations where performance/allocations can even rise.
4550

4651
## Benchmark
4752

48-
The following table gives you a small comparison between the `StringBuilder` which is part of .NET and the `ValueStringBuilder`:
53+
The following table compares the built-in `StringBuilder` and this library's `ValueStringBuilder`:
4954

5055
```no-class
5156
BenchmarkDotNet=v0.13.2, OS=macOS Monterey 12.6.1 (21G217) [Darwin 21.6.0]
@@ -62,9 +67,9 @@ Apple M1 Pro, 1 CPU, 10 logical and 10 physical cores
6267
| ValueStringBuilderPreAllocated | 113.9 ns | 0.67 ns | 0.60 ns | 0.50 | 0.00 | 0.2677 | 560 B | 0.38 |
6368
```
6469

65-
For more comparison check the documentation.
70+
For more comparisons, check the documentation.
6671

67-
Another benchmark shows that this `ValueStringBuilder` uses less memory when it comes to appending `ValueTypes` such as `int`, `double`, ...
72+
Another benchmark shows that `ValueStringBuilder` allocates less memory when appending value types (such as `int` and `double`):
6873

6974
```no-class
7075
| Method | Mean | Error | StdDev | Gen 0 | Allocated |
@@ -73,7 +78,7 @@ Another benchmark shows that this `ValueStringBuilder` uses less memory when it
7378
| ValueStringBuilder | 16.24 us | 0.496 us | 1.462 us | 0.3357 | 1 KB |
7479
```
7580

76-
Checkout the [Benchmark](tests/LinkDotNet.StringBuilder.Benchmarks) for a more detailed comparison and setup.
81+
Check out the [Benchmark](tests/LinkDotNet.StringBuilder.Benchmarks) for a more detailed comparison and setup.
7782

7883
## Support & Contributing
7984

0 commit comments

Comments
 (0)