{"id":18798632,"url":"https://github.com/liteobject/performance-with-span","last_synced_at":"2025-08-27T09:42:41.411Z","repository":{"id":184262806,"uuid":"671522147","full_name":"LiteObject/performance-with-span","owner":"LiteObject","description":"C# 7.2 introduced a new type called Span\u003cT\u003e that provides a safe and efficient way to work with contiguous regions of memory.","archived":false,"fork":false,"pushed_at":"2023-07-27T20:58:51.000Z","size":42,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-05-21T19:13:52.195Z","etag":null,"topics":["csharp","dotnet","examples","performance","span"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/LiteObject.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2023-07-27T14:05:36.000Z","updated_at":"2023-07-27T15:29:35.000Z","dependencies_parsed_at":null,"dependency_job_id":"fe8976b6-c772-4bb0-8427-2678e8c0d64b","html_url":"https://github.com/LiteObject/performance-with-span","commit_stats":null,"previous_names":["liteobject/performance-with-span"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/LiteObject/performance-with-span","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LiteObject%2Fperformance-with-span","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LiteObject%2Fperformance-with-span/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LiteObject%2Fperformance-with-span/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LiteObject%2Fperformance-with-span/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/LiteObject","download_url":"https://codeload.github.com/LiteObject/performance-with-span/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LiteObject%2Fperformance-with-span/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":272315616,"owners_count":24912609,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-27T02:00:09.397Z","response_time":76,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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","examples","performance","span"],"created_at":"2024-11-07T22:12:31.759Z","updated_at":"2025-08-27T09:42:36.367Z","avatar_url":"https://github.com/LiteObject.png","language":"C#","readme":"\n# Introduction to `Span\u003cT\u003e`\nProvides a safe and efficient way to work with contiguous regions of memory\n\n## What is `Span\u003cT\u003e`?\nA Span\u003cT\u003e is a lightweight view over a contiguous region of memory that \ncan be used to read from or write to that memory. It is similar to an \narray, but unlike an array, it does not own the memory it points to. \nInstead, it's just a reference to a contiguous region of memory that's \nalready been allocated, and it allows you to work with that memory directly.\n\nSpan\u003cT\u003e is part of a family of types in C# called \"memory types\", which also \nincludes Memory\u003cT\u003e, ReadOnlyMemory\u003cT\u003e, MemoryManager\u003cT\u003e, and others. These \ntypes are designed to provide efficient and safe ways to work with memory in C#.\n\n## How does `Span\u003cT\u003e` work?\nA `Span\u003cT\u003e` - which is a [ref struct](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/ref-struct) - contains a pointer to the start of the memory region, \nas well as a length that indicates the number of elements in the memory region.\n\nThe `Span\u003cT\u003e` struct also provides methods and operators that allow you to work with \nthe memory region.\n\nOne of the advantages of using `Span\u003cT\u003e` is that it avoids unnecessary copying of memory. \nWhen you create a `Span\u003cT\u003e` object, you're just creating a lightweight view over the memory \nregion - you're not creating a new copy of the memory. This makes `Span\u003cT\u003e` a great choice \nfor performance-critical scenarios where you need to avoid unnecessary memory allocations \nand copying.\n\n## Examples\nHere are some examples of how to use `Span\u003cT\u003e` in C#:\n\n### Example 1: Modifying an array in-place\n\n```csharp\nint[] numbers = { 1, 2, 3, 4, 5 };\n\nSpan\u003cint\u003e span = numbers.AsSpan();\n\nspan[2] = 42;\n\n// Output: \"1, 2, 42, 4, 5\"\nConsole.WriteLine(string.Join(\", \", numbers));\n```\n\nIn this example, we first define an array of integers numbers with the values `{ 1, 2, 3, 4, 5 }`. \nWe then create a `Span\u003cint\u003e` object span from the array using the AsSpan extension method.\n\nWe can then use the indexer syntax to modify the value of the third element (span[2]) to 42. \nThis modifies the underlying array numbers directly.\n\nFinally, we print the contents of the array to the console using the `string.Join` method.\n\nThe result is an array with the value { 1, 2, 42, 4, 5 }. Note that the `Span\u003cint\u003e` object span \nallows us to modify the contents of the array directly, without creating a new array or copying \nits contents.\n\n### Example 2: Copying a substring to a new array\n\n```csharp\nstring originalString = \"Hello, world!\";\n\nSpan\u003cchar\u003e span = originalString.AsSpan();\nSpan\u003cchar\u003e subSpan = span.Slice(0, 5);\n\nchar[] newArray = subSpan.ToArray();\n\nConsole.WriteLine(new string(newArray)); // Output: \"Hello\"\n```\n\nIn this example, we first define a string originalString with the value \"Hello, world!\". \nWe then create a `Span\u003cchar\u003e` object span from the string using the AsSpan extension method.\n\nWe can then create a new `Span\u003cchar\u003e` object subSpan that represents a substring of the original \nstring, starting at index 0 and with a length of 5. We can then create a new `char[]` array \nnewArray from the subSpan using the ToArray method.\n\nFinally, we create a new string from the newArray using the string constructor that takes \na `char[]` parameter, and print it to the console.\n\nThe result is a new string with the value \"Hello\". Note that the `Span\u003cchar\u003e` object subSpan \nallows us to work with a substring of the original string without creating a new string or \ncopying its contents.\n\n## Difference between `Span\u003cT\u003e` and `Memory\u003cT\u003e`\n\nThe main difference between `Span\u003cT\u003e` and `Memory\u003cT\u003e` is that `Span\u003cT\u003e` does not own the memory \nit points to, while `Memory\u003cT\u003e` does. This means that when you create a `Span\u003cT\u003e` object, you're \njust creating a lightweight view over an existing memory region that's already been allocated. \nYou're not creating a new copy of the memory, and you're not responsible for deallocating the \nmemory when you're done with it.\n\nIn contrast, when you create a `Memory\u003cT\u003e` object, you're creating a new memory region that's \nowned by the `Memory\u003cT\u003e` object. This means that you're responsible for deallocating the memory \nwhen you're done with it.\n\n## Conclusion\n\n`Span\u003cT\u003e` is a powerful and efficient tool for working with contiguous regions of memory in C#. \nIt allows you to work with memory directly, without unnecessary copying or memory allocations. \nBy using `Span\u003cT\u003e`, you can write more performant and efficient code in C#.\n\n---\n## Benchmark Summary\n\nThe `PerfTest.cs` file contains all the methods used in the benchmark test.\n\n![Benchmark](./Screenshot.jpg)\n---\n## Links\n- [Learn/.NET/API browser/System/`Span\u003cT\u003e` Struct](https://learn.microsoft.com/en-us/dotnet/api/system.span-1?view=net-7.0)\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fliteobject%2Fperformance-with-span","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fliteobject%2Fperformance-with-span","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fliteobject%2Fperformance-with-span/lists"}