rjmurillo / moq.analyzers

Set of analyzers for Moq mocking library
BSD 3-Clause "New" or "Revised" License
22 stars 2 forks source link

Refactor Moq1300 to use IOperation-based analysis #125

Closed MattKotsenas closed 1 week ago

MattKotsenas commented 1 week ago

Refactor Moq1300 to use IOperation-based analysis instead of syntax analysis. This method results in a slightly less specific diagnostic (it uses the location of the entire .As<T>() method instead of only the type parameter, however it has several benefits:

  1. About double the performance / speed
  2. Cuts the memory allocations in half
  3. I believe a bit more robust to "interesting" syntax trees

Here's a BenchmarkDotNet run comparing the old and new analyzers:

| Method                    | Mean     | Error   | StdDev   | Ratio | RatioSD | Gen0      | Gen1      | Gen2      | Allocated | Alloc Ratio |
|-------------------------- |---------:|--------:|---------:|------:|--------:|----------:|----------:|----------:|----------:|------------:|
| OldMoq1300WithDiagnostics | 204.2 ms | 5.01 ms | 14.44 ms |  1.72 |    0.13 | 8000.0000 | 4000.0000 | 1000.0000 |  69.35 MB |        1.36 |
| NewMoq1300WithDiagnostics | 162.3 ms | 3.21 ms |  3.15 ms |  1.38 |    0.06 | 6000.0000 | 2000.0000 |         - |  62.42 MB |        1.22 |
| Moq1300Baseline           | 118.0 ms | 2.35 ms |  5.12 ms |  1.00 |    0.00 | 5000.0000 | 2000.0000 |         - |  50.97 MB |        1.00 |