Skip to content

Commit 7df0439

Browse files
feat: cache common StringDoc types (#1494)
Add `StringDoc.Create` to reuse common `StringDoc` types where possible. Saving 1.7 MB, 2% of memory usage. ### Most common `StringDoc` types (total 97,000) ![Screenshot 2025-02-18 201936](https://github.com/user-attachments/assets/edff4d0b-f8ef-42d6-82a4-a3ec500faa87) - I did most of the common single character symbols, it might be worth adding other common symbols like `int`, `string`, `true`, `var`, `new` etc. The switch statements of this size are lowered into an efficient hash/binary tree so I doubt it makes a meaningful performance impact. - Is it worth exploring caching all new `StringDoc` types? There are a total of 674 unique `StringDoc` symbols in the example benchmark, I wonder how many unique symbols are in a real world 1M loc project. Perhaps a cache for each file or using a static `ConditionalWeakTable`. I can see this causing a memory leak if implemented incorrectly😅 - A future change could look into creating a `SpanDoc` type that contains `SyntaxToken.Span`, this is used to copy the relevant section of the original code into the new text without allocating. Co-authored-by: Bela VanderVoort <[email protected]>
1 parent 2f6094c commit 7df0439

File tree

3 files changed

+467
-5
lines changed

3 files changed

+467
-5
lines changed

Src/CSharpier/DocTypes/Doc.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@ public override string ToString()
77
return DocSerializer.Serialize(this);
88
}
99

10-
public static implicit operator Doc(string value)
11-
{
12-
return new StringDoc(value);
13-
}
10+
public static implicit operator Doc(string value) => StringDoc.Create(value);
1411

1512
public static NullDoc Null => NullDoc.Instance;
1613

0 commit comments

Comments
 (0)