{"id":18339017,"url":"https://github.com/xoofx/fixedstrings","last_synced_at":"2026-03-06T04:36:36.112Z","repository":{"id":198258567,"uuid":"700203355","full_name":"xoofx/FixedStrings","owner":"xoofx","description":"Zero allocation valuetype based fixed string implementation (8/16/32/64) for .NET 7+","archived":false,"fork":false,"pushed_at":"2024-03-17T14:05:39.000Z","size":63,"stargazers_count":86,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-11-27T12:57:33.550Z","etag":null,"topics":["csharp","dotnet"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-2-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xoofx.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null},"funding":{"github":["xoofx"]}},"created_at":"2023-10-04T06:34:04.000Z","updated_at":"2025-10-23T05:57:32.000Z","dependencies_parsed_at":"2024-02-16T18:44:26.544Z","dependency_job_id":null,"html_url":"https://github.com/xoofx/FixedStrings","commit_stats":null,"previous_names":["xoofx/fixedstrings"],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/xoofx/FixedStrings","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xoofx%2FFixedStrings","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xoofx%2FFixedStrings/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xoofx%2FFixedStrings/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xoofx%2FFixedStrings/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xoofx","download_url":"https://codeload.github.com/xoofx/FixedStrings/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xoofx%2FFixedStrings/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30161894,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-06T04:22:03.816Z","status":"ssl_error","status_checked_at":"2026-03-06T04:22:00.183Z","response_time":250,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["csharp","dotnet"],"created_at":"2024-11-05T20:16:13.889Z","updated_at":"2026-03-06T04:36:36.095Z","avatar_url":"https://github.com/xoofx.png","language":"C#","funding_links":["https://github.com/sponsors/xoofx"],"categories":[],"sub_categories":[],"readme":"# FixedStrings [![ci](https://github.com/xoofx/FixedStrings/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/xoofx/FixedStrings/actions/workflows/ci.yml) [![NuGet](https://img.shields.io/nuget/v/FixedStrings.svg)](https://www.nuget.org/packages/FixedStrings/)\n\n\u003cimg align=\"right\" width=\"160px\" height=\"160px\" src=\"https://raw.githubusercontent.com/xoofx/FixedStrings/main/img/FixedStrings.png\"\u003e\n\nFixedStrings provides a zero allocation valuetype based fixed string implementation with the following size 8/16/32/64.\n\n```c#\n// Zero allocation!\nFixedString16 str = $\"HelloWorld {DateTime.Now.Year}\";\n// Prints \"HelloWorld 2023\"\nConsole.Out.WriteLine(str.AsSpan());\n```\n\n## Features\n\n- Zero allocation via `FixedString8`, `FixedString16`, `FixedString32` and `FixedString64` structs.     \n- Compatible with `net7.0`+\n\n## User Guide\n\n### Why FixedStrings?\n\nMany modern applications are suffering from lots of allocations on the heap, and it is very frequent to see dozen of thousands of strings allocated on the managed heap. Having all these managed objects around is creating a lot more pressure on the GC, they can be more scattered in memory.\n\nIn many scenarios, you might be able to co-locate such strings closer to the class that are using them, but you might be able also to refine the memory requirement/usage for these strings.\n\nFor instance, on a 64 bit system, **a single allocation of an empty managed string on the heap will take 24 bytes + 8 bytes for the reference to it. That's 32 bytes for an empty string!** See [details on sharplab.io](https://sharplab.io/#v2:EYLgxg9gTgpgtADwGwBYA0AXEUCuA7AHwAEAmABgFgAoUgRmoEk8BnABxjAwDoAJGAQ1YAKAEQiAlAG5GLdp14DhInhOlUmbDtz6DRfADb6IAdQiqgA=).\n\nWith a fixed string, like `FixedString8`, you get 7 characters and it takes only 16 bytes or a `FixedString16` would give similarly 15 characters while taking only 32 bytes!\n\nAs you can see in the benchmarks below, by avoiding an allocation to the heap, **FixedStrings can be 2x as fast as creating them on the heap**.\n\n### How does it work?\n\nFixedStrings is relying on the C# 10 [interpolated string handlers](https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/tutorials/interpolated-string-handler) feature.\n\nBut unlike classical usage of interpolated string handlers, a FixedString is in fact itself **an interpolation handler**, using a fixed size buffer to store the string.\n\n| Fixed String Type | Maximum Number of Chars | Total Size\n|-------------------|-------------------------|---------------------\n| `FixedString8`    |        7                | 16 bytes\n| `FixedString16`   |        15               | 32 bytes\n| `FixedString32`   |        31               | 64 bytes\n| `FixedString64`   |        63               | 128 bytes\n\nEach fixed string contains a `short` value for the length.\n\nFor example `FixedString8` is declared like this:\n\n```c#\n[InterpolatedStringHandler]\npublic struct FixedString8 : IFixedString\u003cFixedString8\u003e\n{\n    private short _length;\n    private short _c0;\n    private short _c1;\n    private short _c2;\n    private short _c3;\n    private short _c4;\n    private short _c5;\n    private short _c6;\n\n    // ...\n}\n```\n\nIt is using a `short` for the internal representation of each characters to avoid marshalling issues - so that each character is passed as a plain `short` (UTF-16) value to the native side.\n\nYou can then copy around by value this string very easily and you can have a better control of the layout in memory.\n\n### Usage and restrictions\n\nFixedStrings are not meant to replace all strings in your application. They are meant to be used for strings that are known to be short and that are used in a very hot path of your application.\n\n- If you try to add more characters than the size of the fixed string, **it will be truncated**. For practical reasons, It won't throw an exception!\n- FixedStrings support interpolation:\n  ```c#\n  FixedString8 str = $\"Hello {name}\";\n  ```\n  For performance reasons, supported types for values are: `string` and any types implementing `ISpanFormattable` (e.g `int`, `float`...etc.).\n  A fixed string is itself `ISpanFormattable` and can be used as a value - and nested in string interpolations.\n\n  \u003e Note that if a non string interpolated value cannot fit within the fixed string, it won't be emitted and the string will be truncated (in case of using a string). For example \n  \u003e ```c#\n  \u003e FixedString8 str = $\"{\"Hello\"}{\"World\"}\";\n  \u003e ```\n  \u003e will result in ` str` containing `\"HelloWo\"` (7 characters).\n  \u003e but the following interpolation:\n  \u003e ```c#\n  \u003e FixedString8 str = $\"Hello{int.MaxValue}\";\n  \u003e ```\n  \u003e will result in ` str` containing `\"Hello\"`, with the int value being discarded as it cannot fit within the fixed string.\n- Supporting alignment and format for interpolated values\n  ```c#\n  byte value = 10; \n  FixedString16 str = $\"Test 0x{value:X2}\";\n  ```\n- FixedStrings support assigning from a regular string:\n  ```c#\n  FixedString8 str = \"Hello\";\n  ```\n- You can get a `ReadOnlySpan\u003cchar\u003e` directly from a FixedString:\n  ```c#\n  FixedString8 str = \"Hello\";\n  ReadOnlySpan\u003cchar\u003e span = str.AsSpan();\n  ```\n- FixedStrings can be copied by value:\n  ```c#\n  FixedString8 str = \"Hello\";\n  FixedString8 str2 = str;\n  ```\n  `str2` will be a copy of `str`.\n\n- FixedStrings implement [`IFixedString\u003cT\u003e`](https://github.com/xoofx/FixedStrings/blob/main/src/FixedStrings/IFixedString.cs) and can be used as a generic constraint:\n  ```c#\n  public int Foo\u003cT\u003e(T value) where T : IFixedString\u003cT\u003e\n  {\n      // ...\n      return value.Length;\n  }\n  ```\n\n## Benchmarks\n\n`TestFixed` is using a `FixedString` and `TestDynamic` is using a regular `string`.\n\n- For `FixedString8`, the performance gain is 2.5x faster than using a regular string.\n- For `FixedString16`, the performance gain is 2x faster than using a regular string.\n- For `FixedString32` / `FixedString64`, the performance gain is 30% faster than using a regular string.\n\n| Method        | Runtime  | Mean     | Allocated |\n|-------------- |--------- |---------:|----------:|\n| TestFixed8    | .NET 7.0 | 13.70 ns |         - |\n| TestDynamic8  | .NET 7.0 | 31.33 ns |      40 B |\n|               |          |          |           |\n| TestFixed8    | .NET 8.0 | 10.05 ns |         - |\n| TestDynamic8  | .NET 8.0 | 26.47 ns |      40 B |\n|               |          |          |           |\n| TestFixed16   | .NET 7.0 | 17.56 ns |         - |\n| TestDynamic16 | .NET 7.0 | 32.52 ns |      56 B |\n|               |          |          |           |\n| TestFixed16   | .NET 8.0 | 14.25 ns |         - |\n| TestDynamic16 | .NET 8.0 | 27.80 ns |      56 B |\n|               |          |          |           |\n| TestFixed32   | .NET 7.0 | 38.27 ns |         - |\n| TestDynamic32 | .NET 7.0 | 49.70 ns |      86 B |\n|               |          |          |           |\n| TestFixed32   | .NET 8.0 | 30.12 ns |         - |\n| TestDynamic32 | .NET 8.0 | 38.15 ns |      86 B |\n|               |          |          |           |\n| TestFixed64   | .NET 7.0 | 61.37 ns |         - |\n| TestDynamic64 | .NET 7.0 | 80.15 ns |     144 B |\n|               |          |          |           |\n| TestFixed64   | .NET 8.0 | 43.96 ns |         - |\n| TestDynamic64 | .NET 8.0 | 59.32 ns |     144 B |\n\n## Building FixedStrings\n\nIn order to build FixedStrings, you need:\n- Install the [.NET 8 SDK](https://dotnet.microsoft.com/en-us/download/dotnet/8.0)\n- Install the [.NET 7 SDK](https://dotnet.microsoft.com/en-us/download/dotnet/7.0) (For the benchmarks)\n\n```\ndotnet build -c Release src/FixedStrings.sln\n```\n\n## License\n\nThis software is released under the [BSD-2-Clause license](https://opensource.org/licenses/BSD-2-Clause). \n\n## Author\n\nAlexandre Mutel aka [xoofx](https://xoofx.github.io).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxoofx%2Ffixedstrings","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxoofx%2Ffixedstrings","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxoofx%2Ffixedstrings/lists"}