You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
area-System.Text.JsonenhancementProduct code improvement that does NOT require public API changes/additionsin-prThere is an active PR which will close this issue when it is mergedtenet-performancePerformance related issue
Is your feature request related to a problem? Please describe.
Feature request. Enable SIMD for the new Json APIs in ASP.NET Core 3.
Describe the solution you'd like
See SimdJsonSharp
With SIMD support, SimdJsonSharp is faster than any existing Json library on .NET.
I think the new Json APIs in ASP.NET Core 3 should add SIMD support to improve its performance.
Reacted by Haytham ElFadeel, Dzmitry Safarau, steskalja, Raphael DUCOM, epicabsol, JanEggers, leis1015, Bradley Grainger, Yann Crumeyrolle, kronic and 18 more
Reacted by Rodrigo Silva de Andrade, Egor Bogatov, Raphael DUCOM, JanEggers, leis1015, Artemov Ivan, Ryan Elian, Ahson Khan, Soheil Alizadeh and Andrew Harper
My personal take on this:
If you take a look at @EgorBo's SimdJsonSharp and the available benchmarks the structural symbol tape concept really shines the larger the data gets, especially if it is pretty-printed (i.e. much whitespace between relevant symbols). If that's not the case, e.g. no pretty-printing, most of the time is spent in parsing of the more complex data types. That's visibile in the benchmark, a simpler, "not-as-valid" double parser is easily ten times faster than the corefx version.
Still I expect SimdJsonSharp (especially if it's further optimized) to be twice as fast (or more) for data sizes of 1kb and larger, compared to the "normal" approach of a byte-per-byte parser.
I think we'll never have the option to be forgiving in parsing in the name of perf, due to security concerns. However, I don't see a reason why we shouldn't be speeding up the implementation using SIMD.
SimdJsonSharp does full validation, including checking for valid UTF-8. However, it is designed around parsing large chunks and may need to be adjusted to provide something more "streamable" and memory efficient in addition to being fast. I recommend reading the papers about original SimdJson to figure out what it does and how it accomplished it.
I'd defer to @ahsonkhan for his stance; but I would expect we aren't going to look into this for the 3.0 timeframe. Instead, 3.0 will likely aim towards getting the new API shipped and stable and perf improvements (like this) will be investigated for a future release.
Reacted by Ahson Khan, Ganbarukamo41 and Soheil Alizadeh
but I would expect we aren't going to look into this for the 3.0 timeframe. Instead, 3.0 will likely aim towards getting the new API shipped and stable and perf improvements (like this) will be investigated for a future release.
Precisely.
The SIMD work definitely looks promising and we should definitely investigate how we can incorporate it into the implementation of System.Text.Json APIs. I prefer we defer this work until after we have locked the API design and features for 3.0. If anyone wants to tackle parts of this or work on prototypes to incorporate some of the SIMD effort with the existing Utf8JsonReader, that would definitely be appreciated!
Many of the most popular SIMD implementations are not “strictly spec compliant” and may have edge case allowances that we can’t support in the built-in implementation
While some more opportunities likely exist, the type of perf gains some people are envisioning simply aren’t possible in a full featured, secure, and robust implementation.
See also the discussion above about being oriented towards different goals, such as streaming and incremental parsing vs parsing entire files at once or similar
That being said, in general if there are incremental improvements with benchmarks showing the performance gains (ideally shown via the existing tests in dotnet/performance), we are open source and people are welcome to contribute
Larger changes should ideally be discussed with the area owners first. But isolated changes and targeted improvements are “typically” fine to just have a PR submitted
This was investigated in #124647 where a couple of vectorization opportunities were identified. These however proved to substantially regress performance, so I'd be inclined to close this issue as done.
@eiriktsarpalis thank you for coming back and taking a look at this. I am wondering does engineers outside of microsoft can tirgger copilot for dotnet repo to actually work over specific things, like in the PR you've investigated? Sorry to ask it here, I am not sure what's the policy on this?
Direct assignment of issues to copilot requires maintainer access, but there are other ways you could do it like prompting copilot to submit a PR either from VS Code or copilot cli.
@eiriktsarpalis From what I have seen in #124647, it does nothing to obtain simdjson's benefit, instead it only replaces several scalar loops with vectorized one.
The paper (https://arxiv.org/abs/1902.08318) I referenced in the original issue demonstrates a two-stage design: stage 1 scans the raw input in 64-byte blocks, uses SIMD and bitset transforms to validate UTF-8, identify strings, classify structural and whitespaces, derive pseudo-structural characters, and turn those bitmasks into an index stream; stage 2 then walks those indexes with a state machine.
This breaks parser data dependencies by precomputing token starts and atom starts up front, so that stage 2 does not need to finish parsing one atom to find the next token.
Well vectorized path for parsing numbers and strings does exist in simdjson, but the main benefit of simdjson cannot just be obtained by optimizing some loops in some local spots. Instead it's an algorithm change.
So I really don't see how can the benchmark result in #124647 can be used for justification on this feature request. Simdjson is never about spot optimizations that just vectorize a couple of scalar loops.
Reacted by Giorgi Chakhidze, Filip Navara and Nikolay Zdravkov
There's probably some, potentially even for #124647 if you do direct rather than indirect vectorization, but I think the more meaningful optimizations for cases would be refactoring the logic to account for "expected sequences".
SkipWhitespace (which skips space, tab, newline, and carriage return) for example is likely slower in the PR because of the double work its doing when a hit is found and the likelyhood of finding a hit given how typical JSON is structured.
Even just for the scalar approach, it would likely be a decent perf win to set it up presuming that you don't have equal potential for the four characters or arbitrary mixing of them. Rather given a line of JSON you're likely to have 2-16 spaces (or 1-4 tabs) followed by an ascii character. You're then unlikely to encounter more than 1 space (particularly at a time), outside of a string which is specially parsed, until the end of the line where you are then likely to encounter a newline. A JSON document which uses a specific newline convention is likely to keep that same convention throughout (i.e. either all \n or all \r\n). You're then likely to repeat the process again for the next line (i.e. have whitespace after the newline, which is the likely longest sequence of whitespace encountered).
Given that, the general logic should likely be structured to account for these possibilities but then any SIMD vectorization should itself match this premise as well, which is that you're extremely likely to hit some non-whitespace character within 32-bytes of the JSON payload, which means that you probably shouldn't be looping at all but instead have a fast path for this special case and only fallback to a loop in the rare edge case.
Such a SIMD path should likewise not have to do double computation as you already have a valid mask -- most of the expense in the PR was likely from all the extra branches and mispredicts being hit (20 cycles each) as you would try to setup for a SIMD loop, get a hit, then have to slice and redo most of the same comparisons again to try to extract the info you wanted. Rather than just getting the mask once and getting the data directly from it without redoing all the work.
SkipDigits really has similar considerations. In a typical case you're going to encounter an ASCII digit and then up to 19 more (covering the typical case for all integer types + double).
Both the scalar path and any SIMD path should really be optimizing for this assumption to avoid unnecessary branching or mispredicts, giving us a simple fast path that understands typical JSON data and avoiding unnecessary work.
Other areas likely also exist too, particularly if they followed the SimdJson like approach of building a map of the info up front as part of validating the initial input was valid JSON -- which is notably possible for JsonDocument but not Utf8JsonReader
area-System.Text.JsonenhancementProduct code improvement that does NOT require public API changes/additionsin-prThere is an active PR which will close this issue when it is mergedtenet-performancePerformance related issue
Activity
terrajobst commentedon Mar 11, 2019
Hey @hez2010 that's a great suggestion. We're in the middle of providing a new perf oriented set of JSON APIs for .NET Core 3.0.
@ahsonkhan, what's your take on this?
Tornhoof commentedon Mar 11, 2019
My personal take on this:
If you take a look at @EgorBo's SimdJsonSharp and the available benchmarks the structural symbol tape concept really shines the larger the data gets, especially if it is pretty-printed (i.e. much whitespace between relevant symbols). If that's not the case, e.g. no pretty-printing, most of the time is spent in parsing of the more complex data types. That's visibile in the benchmark, a simpler, "not-as-valid" double parser is easily ten times faster than the corefx version.
Still I expect SimdJsonSharp (especially if it's further optimized) to be twice as fast (or more) for data sizes of 1kb and larger, compared to the "normal" approach of a byte-per-byte parser.
terrajobst commentedon Mar 11, 2019
I think we'll never have the option to be forgiving in parsing in the name of perf, due to security concerns. However, I don't see a reason why we shouldn't be speeding up the implementation using SIMD.
filipnavara commentedon Mar 12, 2019
SimdJsonSharp does full validation, including checking for valid UTF-8. However, it is designed around parsing large chunks and may need to be adjusted to provide something more "streamable" and memory efficient in addition to being fast. I recommend reading the papers about original SimdJson to figure out what it does and how it accomplished it.
NinoFloris commentedon Apr 13, 2019
Any news on this?
tannergooding commentedon Apr 14, 2019
I'd defer to @ahsonkhan for his stance; but I would expect we aren't going to look into this for the 3.0 timeframe. Instead, 3.0 will likely aim towards getting the new API shipped and stable and perf improvements (like this) will be investigated for a future release.
ahsonkhan commentedon May 9, 2019
Precisely.
The SIMD work definitely looks promising and we should definitely investigate how we can incorporate it into the implementation of
System.Text.JsonAPIs. I prefer we defer this work until after we have locked the API design and features for 3.0. If anyone wants to tackle parts of this or work on prototypes to incorporate some of the SIMD effort with the existingUtf8JsonReader, that would definitely be appreciated!RobertHenry6bev commentedon Apr 9, 2020
See the paper https://arxiv.org/pdf/1902.08318.pdf on ideas for using SIMD
See the git repo https://github.com/simdjson/simdjson
3 remaining items
layomia commentedon Oct 13, 2022
Triage: we should also assess prior art from other serializers and how they utilize SIMD.
unsafePtr commentedon Oct 4, 2025
Maybe there is a chance this can be implemented for .net11?
tannergooding commentedon Oct 4, 2025
Many of the most popular SIMD implementations are not “strictly spec compliant” and may have edge case allowances that we can’t support in the built-in implementation
While some more opportunities likely exist, the type of perf gains some people are envisioning simply aren’t possible in a full featured, secure, and robust implementation.
See also the discussion above about being oriented towards different goals, such as streaming and incremental parsing vs parsing entire files at once or similar
tannergooding commentedon Oct 4, 2025
That being said, in general if there are incremental improvements with benchmarks showing the performance gains (ideally shown via the existing tests in dotnet/performance), we are open source and people are welcome to contribute
Larger changes should ideally be discussed with the area owners first. But isolated changes and targeted improvements are “typically” fine to just have a PR submitted
eiriktsarpalis commentedon Mar 19, 2026
This was investigated in #124647 where a couple of vectorization opportunities were identified. These however proved to substantially regress performance, so I'd be inclined to close this issue as done.
unsafePtr commentedon Mar 19, 2026
@eiriktsarpalis thank you for coming back and taking a look at this. I am wondering does engineers outside of microsoft can tirgger copilot for dotnet repo to actually work over specific things, like in the PR you've investigated? Sorry to ask it here, I am not sure what's the policy on this?
eiriktsarpalis commentedon Mar 19, 2026
Direct assignment of issues to copilot requires maintainer access, but there are other ways you could do it like prompting copilot to submit a PR either from VS Code or copilot cli.
hez2010 commentedon Mar 19, 2026
@eiriktsarpalis From what I have seen in #124647, it does nothing to obtain simdjson's benefit, instead it only replaces several scalar loops with vectorized one.
The paper (https://arxiv.org/abs/1902.08318) I referenced in the original issue demonstrates a two-stage design: stage 1 scans the raw input in 64-byte blocks, uses SIMD and bitset transforms to validate UTF-8, identify strings, classify structural and whitespaces, derive pseudo-structural characters, and turn those bitmasks into an index stream; stage 2 then walks those indexes with a state machine.
This breaks parser data dependencies by precomputing token starts and atom starts up front, so that stage 2 does not need to finish parsing one atom to find the next token.
Well vectorized path for parsing numbers and strings does exist in simdjson, but the main benefit of simdjson cannot just be obtained by optimizing some loops in some local spots. Instead it's an algorithm change.
So I really don't see how can the benchmark result in #124647 can be used for justification on this feature request. Simdjson is never about spot optimizations that just vectorize a couple of scalar loops.
eiriktsarpalis commentedon Mar 19, 2026
Are there any actionable opportunities to apply vectorization in the existing STJ infrastructure?
tannergooding commentedon Mar 19, 2026
There's probably some, potentially even for #124647 if you do direct rather than indirect vectorization, but I think the more meaningful optimizations for cases would be refactoring the logic to account for "expected sequences".
SkipWhitespace(which skips space, tab, newline, and carriage return) for example is likely slower in the PR because of the double work its doing when a hit is found and the likelyhood of finding a hit given how typical JSON is structured.Even just for the scalar approach, it would likely be a decent perf win to set it up presuming that you don't have equal potential for the four characters or arbitrary mixing of them. Rather given a line of JSON you're likely to have 2-16 spaces (or 1-4 tabs) followed by an ascii character. You're then unlikely to encounter more than 1 space (particularly at a time), outside of a string which is specially parsed, until the end of the line where you are then likely to encounter a newline. A JSON document which uses a specific newline convention is likely to keep that same convention throughout (i.e. either all
\nor all\r\n). You're then likely to repeat the process again for the next line (i.e. have whitespace after the newline, which is the likely longest sequence of whitespace encountered).Given that, the general logic should likely be structured to account for these possibilities but then any SIMD vectorization should itself match this premise as well, which is that you're extremely likely to hit some non-whitespace character within 32-bytes of the JSON payload, which means that you probably shouldn't be looping at all but instead have a fast path for this special case and only fallback to a loop in the rare edge case.
Such a SIMD path should likewise not have to do double computation as you already have a valid mask -- most of the expense in the PR was likely from all the extra branches and mispredicts being hit (20 cycles each) as you would try to setup for a SIMD loop, get a hit, then have to slice and redo most of the same comparisons again to try to extract the info you wanted. Rather than just getting the mask once and getting the data directly from it without redoing all the work.
SkipDigitsreally has similar considerations. In a typical case you're going to encounter an ASCII digit and then up to 19 more (covering the typical case for all integer types +double).Both the scalar path and any SIMD path should really be optimizing for this assumption to avoid unnecessary branching or mispredicts, giving us a simple fast path that understands typical JSON data and avoiding unnecessary work.
Other areas likely also exist too, particularly if they followed the SimdJson like approach of building a map of the info up front as part of validating the initial input was valid JSON -- which is notably possible for JsonDocument but not Utf8JsonReader