Khalid ⚡ on Nostr: I was recently playing around with the Span APIs and was curious to compare the ...
I was recently playing around with the Span APIs and was curious to compare the memory usage between the traditional String APIs and using ReadOnlySpan<char>. This is a simple example of a tight loop.
Most of the memory usage comes from, unsurprisingly, string.Split which creates new string instances.
You can significantly reduce memory pressure in places where you need to inspect values rather than create new instances of them.
#dotnet #csharp
Published at
2024-12-23 19:27:44 UTCEvent JSON
{
"id": "17c503f76db47cc2d86b41167b38665210c9cbc6f27f2ff46b9bc7b2a6ceae60",
"pubkey": "cfa8d6309605e4fe3abdb48e1eae10a004d5a0183f5b227eef2e8ff92be05638",
"created_at": 1734982064,
"kind": 1,
"tags": [
[
"t",
"csharp"
],
[
"proxy",
"https://mastodon.social/@khalidabuhakmeh/113703784576191733",
"web"
],
[
"t",
"dotnet"
],
[
"imeta",
"url https://files.mastodon.social/media_attachments/files/113/703/784/531/594/032/original/8947628aea3e9d83.png",
"m image/png"
],
[
"proxy",
"https://mastodon.social/users/khalidabuhakmeh/statuses/113703784576191733",
"activitypub"
],
[
"L",
"pink.momostr"
],
[
"l",
"pink.momostr.activitypub:https://mastodon.social/users/khalidabuhakmeh/statuses/113703784576191733",
"pink.momostr"
],
[
"-"
]
],
"content": "I was recently playing around with the Span APIs and was curious to compare the memory usage between the traditional String APIs and using ReadOnlySpan\u003cchar\u003e. This is a simple example of a tight loop.\n\nMost of the memory usage comes from, unsurprisingly, string.Split which creates new string instances.\n\nYou can significantly reduce memory pressure in places where you need to inspect values rather than create new instances of them.\n\n#dotnet #csharp\nhttps://files.mastodon.social/media_attachments/files/113/703/784/531/594/032/original/8947628aea3e9d83.png\n",
"sig": "1bd7992cbda5b48c46f217810fc481754c158104767bbf2808ec760c23ac1792c79aff1ab9f681e3a6b944ebd7da9e44efb57c7da849e23b615b9553696ea918"
}