Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Perf optimizations #58

Merged
merged 16 commits into from
Dec 25, 2021
Merged

Perf optimizations #58

merged 16 commits into from
Dec 25, 2021

Conversation

sebastienros
Copy link
Owner

Before


|                     Method |          Mean |         Error |     StdDev |   Gen 0 |  Gen 1 | Allocated |
|--------------------------- |--------------:|--------------:|-----------:|--------:|-------:|----------:|
|           CursorMatchHello |      45.83 ns |      9.488 ns |   0.520 ns |  0.0204 |      - |     128 B |
|         CursorMatchGoodbye |      59.47 ns |      2.266 ns |   0.124 ns |  0.0204 |      - |     128 B |
|            CursorMatchNone |      38.25 ns |     12.920 ns |   0.708 ns |  0.0204 |      - |     128 B |
|                            |               |               |            |         |        |           |
|             SkipWhiteSpace |      61.13 ns |      4.925 ns |   0.270 ns |  0.0204 |      - |     128 B |
|                            |               |               |            |         |        |           |
| DecodeStringWithoutEscapes |      11.93 ns |      2.786 ns |   0.153 ns |       - |      - |         - |
|    DecodeStringWithEscapes |     122.02 ns |     20.751 ns |   1.137 ns |  0.0191 |      - |     120 B |
|                            |               |               |            |         |        |           |
|         ExpressionRawSmall |     375.84 ns |      9.490 ns |   0.520 ns |  0.0482 |      - |     304 B |
|    ExpressionCompiledSmall |     619.13 ns |     15.042 ns |   0.824 ns |  0.1040 |      - |     656 B |
|      ExpressionFluentSmall |     930.06 ns |    152.683 ns |   8.369 ns |  0.1040 |      - |     656 B |
|                            |               |               |            |         |        |           |
|           ExpressionRawBig |   1,769.36 ns |     80.902 ns |   4.435 ns |  0.1907 |      - |   1,200 B |
|      ExpressionCompiledBig |   3,527.21 ns |    350.353 ns |  19.204 ns |  0.4578 | 0.0038 |   2,888 B |
|        ExpressionFluentBig |   4,936.93 ns |    396.767 ns |  21.748 ns |  0.4578 |      - |   2,888 B |
|                            |               |               |            |         |        |           |
|                    BigJson | 155,278.92 ns |  7,270.598 ns | 398.526 ns | 16.1133 | 4.3945 | 101,672 B |
|            BigJsonCompiled | 129,750.94 ns | 12,826.122 ns | 703.043 ns | 16.1133 | 4.6387 | 101,675 B |
|                            |               |               |            |         |        |           |
|                   DeepJson | 101,506.69 ns | 15,960.851 ns | 874.868 ns | 17.9443 | 4.3945 | 112,976 B |
|           DeepJsonCompiled |  73,291.34 ns | 11,373.355 ns | 623.412 ns | 17.9443 | 4.7607 | 112,976 B |
|                            |               |               |            |         |        |           |
|                   LongJson | 126,858.36 ns |  7,748.298 ns | 424.710 ns | 21.4844 | 7.0801 | 135,512 B |
|           LongJsonCompiled | 105,367.39 ns |  5,815.415 ns | 318.763 ns | 21.4844 | 7.0801 | 135,512 B |
|                            |               |               |            |         |        |           |
|                   WideJson |  71,432.41 ns |  5,591.488 ns | 306.488 ns |  6.5918 | 1.0986 |  41,584 B |
|           WideJsonCompiled |  61,578.80 ns |  4,373.520 ns | 239.727 ns |  6.5918 | 1.0986 |  41,584 B |

After


|                     Method |          Mean |         Error |     StdDev |   Gen 0 |  Gen 1 | Allocated |
|--------------------------- |--------------:|--------------:|-----------:|--------:|-------:|----------:|
|           CursorMatchHello |      43.62 ns |      2.116 ns |   0.116 ns |  0.0216 |      - |     136 B |
|         CursorMatchGoodbye |      44.72 ns |      2.184 ns |   0.120 ns |  0.0216 |      - |     136 B |
|            CursorMatchNone |      35.52 ns |      2.205 ns |   0.121 ns |  0.0216 |      - |     136 B |
|                            |               |               |            |         |        |           |
|             SkipWhiteSpace |      43.44 ns |      1.466 ns |   0.080 ns |  0.0216 |      - |     136 B |
|                            |               |               |            |         |        |           |
| DecodeStringWithoutEscapes |      10.29 ns |      1.322 ns |   0.072 ns |       - |      - |         - |
|    DecodeStringWithEscapes |     122.33 ns |     15.457 ns |   0.847 ns |  0.0191 |      - |     120 B |
|                            |               |               |            |         |        |           |
|         ExpressionRawSmall |     336.99 ns |     14.758 ns |   0.809 ns |  0.0496 |      - |     312 B |
|    ExpressionCompiledSmall |     579.00 ns |     38.098 ns |   2.088 ns |  0.1049 |      - |     664 B |
|      ExpressionFluentSmall |     858.92 ns |    144.214 ns |   7.905 ns |  0.1049 |      - |     664 B |
|                            |               |               |            |         |        |           |
|           ExpressionRawBig |   1,607.09 ns |     76.577 ns |   4.197 ns |  0.1907 |      - |   1,208 B |
|      ExpressionCompiledBig |   3,149.37 ns |    208.323 ns |  11.419 ns |  0.4616 | 0.0038 |   2,896 B |
|        ExpressionFluentBig |   4,536.28 ns |    306.708 ns |  16.812 ns |  0.4578 |      - |   2,896 B |
|                            |               |               |            |         |        |           |
|                    BigJson | 143,809.98 ns |  7,429.023 ns | 407.210 ns | 16.1133 | 4.1504 | 101,680 B |
|            BigJsonCompiled | 118,860.58 ns |  6,199.133 ns | 339.795 ns | 16.1133 | 4.8828 | 101,680 B |
|                            |               |               |            |         |        |           |
|                   DeepJson |  90,613.77 ns |  7,011.874 ns | 384.344 ns | 17.9443 | 4.2725 | 112,984 B |
|           DeepJsonCompiled |  65,661.43 ns |  5,726.605 ns | 313.895 ns | 17.9443 | 4.6387 | 112,984 B |
|                            |               |               |            |         |        |           |
|                   LongJson | 115,968.64 ns | 10,665.565 ns | 584.616 ns | 21.4844 | 7.0801 | 135,520 B |
|           LongJsonCompiled |  94,632.68 ns | 11,143.391 ns | 610.807 ns | 21.4844 | 7.0801 | 135,520 B |
|                            |               |               |            |         |        |           |
|                   WideJson |  66,854.04 ns |  3,849.785 ns | 211.020 ns |  6.5918 | 1.0986 |  41,592 B |
|           WideJsonCompiled |  54,751.33 ns |  2,700.468 ns | 148.022 ns |  6.5918 | 1.0986 |  41,592 B |

And benchmarks comparing JSON parsing, including Newtonsoft

|                  Method |        Mean |      Error |    StdDev | Ratio | RatioSD |    Gen 0 |    Gen 1 | Allocated |
|------------------------ |------------:|-----------:|----------:|------:|--------:|---------:|---------:|----------:|
|          BigJson_Parlot |   141.28 us |  27.220 us |  1.492 us |  1.00 |    0.00 |  16.1133 |   4.1504 |     99 KB |
|  BigJson_ParlotCompiled |   116.68 us |   5.311 us |  0.291 us |  0.83 |    0.01 |  16.1133 |   4.8828 |     99 KB |
|          BigJson_Pidgin |   268.07 us |  25.410 us |  1.393 us |  1.90 |    0.03 |  16.1133 |   3.9063 |     99 KB |
|         BigJson_Sprache | 2,072.73 us |  68.511 us |  3.755 us | 14.67 |    0.16 | 859.3750 | 214.8438 |  5,272 KB |
|      BigJson_Superpower | 1,445.79 us |  11.676 us |  0.640 us | 10.23 |    0.11 | 148.4375 |  39.0625 |    911 KB |
|      BigJson_Newtonsoft |   188.77 us |  31.459 us |  1.724 us |  1.34 |    0.02 |  32.9590 |  12.9395 |    203 KB |
|                         |             |            |           |       |         |          |          |           |
|         LongJson_Parlot |   115.18 us |  17.040 us |  0.934 us |  1.00 |    0.00 |  21.4844 |   7.0801 |    132 KB |
| LongJson_ParlotCompiled |    94.83 us |   7.648 us |  0.419 us |  0.82 |    0.01 |  21.4844 |   7.0801 |    132 KB |
|         LongJson_Pidgin |   248.97 us |   7.483 us |  0.410 us |  2.16 |    0.02 |  21.4844 |   6.8359 |    132 KB |
|        LongJson_Sprache | 1,818.10 us | 167.172 us |  9.163 us | 15.79 |    0.14 | 697.2656 | 197.2656 |  4,273 KB |
|     LongJson_Superpower | 1,213.95 us |  28.241 us |  1.548 us | 10.54 |    0.10 | 119.1406 |  39.0625 |    735 KB |
|     LongJson_Newtonsoft |   137.40 us |   5.082 us |  0.279 us |  1.19 |    0.01 |  32.9590 |  14.6484 |    203 KB |
|                         |             |            |           |       |         |          |          |           |
|         DeepJson_Parlot |    88.67 us |   2.616 us |  0.143 us |  1.00 |    0.00 |  17.9443 |   4.2725 |    110 KB |
| DeepJson_ParlotCompiled |    65.79 us |   2.291 us |  0.126 us |  0.74 |    0.00 |  17.9443 |   4.6387 |    110 KB |
|         DeepJson_Pidgin |   370.77 us |  18.345 us |  1.006 us |  4.18 |    0.01 |  36.6211 |  12.2070 |    225 KB |
|        DeepJson_Sprache | 1,563.18 us | 462.780 us | 25.367 us | 17.63 |    0.31 | 476.5625 | 193.3594 |  2,926 KB |
|     DeepJson_Newtonsoft |   112.45 us |   8.912 us |  0.488 us |  1.27 |    0.00 |  29.1748 |  11.5967 |    179 KB |
|                         |             |            |           |       |         |          |          |           |
|         WideJson_Parlot |    67.51 us |   4.374 us |  0.240 us |  1.00 |    0.00 |   6.5918 |   1.0986 |     41 KB |
| WideJson_ParlotCompiled |    54.39 us |   1.875 us |  0.103 us |  0.81 |    0.00 |   6.5918 |   1.0986 |     41 KB |
|         WideJson_Pidgin |   120.91 us |   8.235 us |  0.451 us |  1.79 |    0.01 |   6.5918 |   1.0986 |     41 KB |
|        WideJson_Sprache |   899.91 us |  46.837 us |  2.567 us | 13.33 |    0.05 | 451.1719 |  89.8438 |  2,767 KB |
|     WideJson_Superpower |   700.68 us |  29.067 us |  1.593 us | 10.38 |    0.03 |  73.2422 |  11.7188 |    452 KB |
|     WideJson_Newtonsoft |    93.49 us |   1.166 us |  0.064 us |  1.38 |    0.00 |  17.3340 |   5.7373 |    107 KB |

@@ -17,11 +18,19 @@ public Between(Parser<A> before, Parser<T> parser, Parser<B> after)
_after = after ?? throw new ArgumentNullException(nameof(after));
}

public bool CanSeek => _before is ISeekable seekable && seekable.CanSeek;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extension methods with aggressive inline to reduce duplication?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not concerned about perf here since this is called once per application (when the parser tree is built). How would an extension method remove duplication?

@sebastienros sebastienros merged commit 2ab5692 into main Dec 25, 2021
@sebastienros sebastienros deleted the sebros/whitespaceperf branch December 25, 2021 20:44
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.

2 participants