Skip to content

Conversation

@belav
Copy link
Owner

@belav belav commented Dec 29, 2025

No description provided.

TimothyMakkison and others added 9 commits December 25, 2025 10:21
…#1703)

Semi reverts #1572 which overwrote #1485, I assume this was accidental.
I noticed that you prefer explicit `Enumerable.Any` usage, should I
convert this PR to use `ListExtensions.Any`?


## Benchmarks
Saves around 15% of memory usage
### Before
| Method | Mean | Error | StdDev | Gen0 | Gen1 | Allocated |
|------------------------------
|---------:|--------:|--------:|----------:|----------:|----------:|
| Default_CodeFormatter_Tests | 128.0 ms | 2.44 ms | 3.43 ms | 3000.0000
| 1000.0000 | 34.57 MB |
| Default_CodeFormatter_Complex | 260.3 ms | 5.11 ms | 7.17 ms |
5000.0000 | 2000.0000 | 53.05 MB |


### After
| Method | Mean | Error | StdDev | Gen0 | Gen1 | Allocated |
|------------------------------
|---------:|--------:|---------:|----------:|----------:|----------:|
| Default_CodeFormatter_Tests | 126.0 ms | 2.27 ms | 5.45 ms | 2000.0000
| 1000.0000 | 28.02 MB |
| Default_CodeFormatter_Complex | 245.2 ms | 4.63 ms | 10.44 ms |
4000.0000 | 2000.0000 | 43.88 MB |

Co-authored-by: Bela VanderVoort <twobitbela@gmail.com>
)

Add check for if `modifiers` are `Doc.Null` to avoid allocating a
`Concat` and `Doc[]`


### Benchmarks (timing is inaccurate)
#### Before
| Method | Mean | Error | StdDev | Median | Gen0 | Gen1 | Allocated |
|------------------------------
|---------:|--------:|--------:|---------:|----------:|----------:|----------:|
| Default_CodeFormatter_Tests | 131.9 ms | 3.30 ms | 8.86 ms | 128.0 ms
| 3000.0000 | 1000.0000 | 34.57 MB |
| Default_CodeFormatter_Complex | 261.3 ms | 5.16 ms | 4.03 ms | 261.3
ms | 5000.0000 | 2000.0000 | 53.03 MB |


#### After
| Method | Mean | Error | StdDev | Median | Gen0 | Gen1 | Allocated |
|------------------------------
|---------:|---------:|---------:|---------:|----------:|----------:|----------:|
| Default_CodeFormatter_Tests | 189.5 ms | 10.25 ms | 28.05 ms | 195.6
ms | 3000.0000 | 1000.0000 | 34.31 MB |
| Default_CodeFormatter_Complex | 264.2 ms | 5.27 ms | 13.97 ms | 260.0
ms | 5000.0000 | 2000.0000 | 52.31 MB |

Co-authored-by: Bela VanderVoort <twobitbela@gmail.com>
Not sure if more changes might help here, perhaps calling
`docsStack.Push(TraverseDocOnExitStackMarker);` later and adding an
`else` clause might help, that or using a goto. Might be easier to
measure an improvement once other changes were made.

Did wonder if `ValueListBuilder` will help due to faster `Push` and
`Pop`.

Co-authored-by: Bela VanderVoort <twobitbela@gmail.com>
Most `result` Lists are always just under `3 * members.Count`, small
change but simple 🤷


### Benchmarks (timing is inaccurate should be the same)
#### Before
| Method | Mean | Error | StdDev | Gen0 | Gen1 | Allocated |
|------------------------------
|---------:|--------:|--------:|----------:|----------:|----------:|
| Default_CodeFormatter_Tests | 133.0 ms | 2.56 ms | 2.84 ms | 3000.0000
| 1000.0000 | 34.58 MB |
| Default_CodeFormatter_Complex | 271.5 ms | 5.40 ms | 7.92 ms |
5000.0000 | 2000.0000 | 52.95 MB |


#### After
| Method | Mean | Error | StdDev | Gen0 | Gen1 | Allocated |
|------------------------------
|---------:|--------:|--------:|----------:|----------:|----------:|
| Default_CodeFormatter_Tests | 132.4 ms | 2.64 ms | 3.87 ms | 3000.0000
| 1000.0000 | 34.56 MB |
| Default_CodeFormatter_Complex | 268.2 ms | 4.92 ms | 4.60 ms |
5000.0000 | 2000.0000 | 52.78 MB |

Co-authored-by: Bela VanderVoort <twobitbela@gmail.com>
It appears that `SyntaxNodeComparer` is run after a file is formated,
optimisation here may be impactful

- Prevent null `Func<T, T, Result>` allocations (still seeing some in
the profiler, no idea how to sotp this)
- Prevent boxing of `SyntaxTokenList`, `SyntaxNodeOrTokenList`
- Use `OrderBy.ToArray` instead of `OrderBy.ToList`
- Create `AllSeparatorsButLast` to prevent boxing
`SeparatedSyntaxList<SyntaxNode>` and `IEnumerable` creation

I have some other idea, but I'm focusing on the unhappy path right now.

### Benchmark (comparing `complexCode`)
#### Before
| Method | Mean | Error | StdDev | Gen0 | Gen1 | Gen2 | Allocated |
|---------------------------
|---------:|--------:|--------:|----------:|----------:|----------:|----------:|
| Default_SyntaxNodeComparer | 229.1 ms | 4.55 ms | 7.60 ms | 5000.0000
| 3000.0000 | 1000.0000 | 40.53 MB |


#### After


| Method | Mean | Error | StdDev | Gen0 | Gen1 | Allocated |
|---------------------------
|---------:|--------:|--------:|----------:|----------:|----------:|
| Default_SyntaxNodeComparer | 187.4 ms | 3.51 ms | 7.64 ms | 3000.0000
| 1000.0000 | 31.39 MB |

Co-authored-by: Bela VanderVoort <twobitbela@gmail.com>
- Compare the old and formatted code before using `SyntaxNodeComparer`
(ie new version of csharpier, no changes but hash is cache is
invalidated)
- Skip comparison if two `TypeDeclaration` aka (classes, struct,
interfaces etc) or `MethodDeclaration` are identical
- This could be done for all `SyntaxNodes` but may slow things down if
most nodes have changed

The CLI spends up to 30% of the time doing comparisons, with this, we
can skip cases with identical source code, and only compare
types/methods that have changed. The performance degradation for lots of
changes (i.e. first time a project is formatted) shouldn't be that great
as `CompareFullSpan` is basically a fast `SequenceEqual` and only used
for type declarations and methods.

Co-authored-by: Bela VanderVoort <twobitbela@gmail.com>
Repeatedly compiling regex for ignore files is a significant amount of
runtime, caches the `IgnoreWithBasePath` to prevent this. See also #1776


### Before
| Method | Mean | Error | StdDev | Gen0 | Gen1 | Allocated |
|----------
|---------:|---------:|---------:|----------:|----------:|----------:|
| FormatCli | 955.0 ms | 17.65 ms | 29.98 ms | 9000.0000 | 1000.0000 |
81.5 MB |


### After

| Method    | Mean     | Error    | StdDev   | Gen0      | Allocated |
|---------- |---------:|---------:|---------:|----------:|----------:|
| FormatCli | 683.5 ms | 13.61 ms | 30.17 ms | 7000.0000 |  69.33 MB |

Co-authored-by: Bela VanderVoort <twobitbela@gmail.com>
This is just adding the options that will eventually be used by the new
xml whitespace handling code.
@belav belav marked this pull request as draft December 29, 2025 21:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants