Skip to content

Enable SIMD for JSON API #28937

Closed
Closed

Description

@terrajobst

From @hez2010 on March 9, 2019 9:19

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.

Copied from original issue: dotnet/aspnetcore#8366

Activity

  1. terrajobst commented on Mar 11, 2019

    @terrajobst
    ContributorAuthor

    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?

  2. Tornhoof commented on Mar 11, 2019

    @Tornhoof
    Contributor

    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.

  3. terrajobst commented on Mar 11, 2019

    @terrajobst
    ContributorAuthor

    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.

  4. filipnavara commented on Mar 12, 2019

    @filipnavara
    Member

    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.

  5. NinoFloris commented on Apr 13, 2019

    @NinoFloris
    Contributor

    Any news on this?

  6. tannergooding commented on Apr 14, 2019

    @tannergooding
    Member

    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.

  7. ahsonkhan commented on May 9, 2019

    @ahsonkhan
    Contributor

    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!

  8. removed their assignment
    on May 9, 2019
  9. transferred this issue fromdotnet/corefxon Feb 1, 2020
  10. added this to the Future milestone on Feb 1, 2020
  11. removed
    help wanted
    on Feb 21, 2020
  12. modified the milestones: Future, 7.0.0 on Oct 15, 2021
  13. 3 remaining items

  14. layomia commented on Oct 13, 2022

    @layomia
    Contributor

    Triage: we should also assess prior art from other serializers and how they utilize SIMD.

  15. unsafePtr commented on Oct 4, 2025

    @unsafePtr
    Contributor

    Maybe there is a chance this can be implemented for .net11?

  16. tannergooding commented on Oct 4, 2025

    @tannergooding
    Member

    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

  17. tannergooding commented on Oct 4, 2025

    @tannergooding
    Member

    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

  18. added
    in-pr
    on Feb 20, 2026
  19. eiriktsarpalis commented on Mar 19, 2026

    @eiriktsarpalis
    Member

    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.

  20. unsafePtr commented on Mar 19, 2026

    @unsafePtr
    Contributor

    @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?

  21. eiriktsarpalis commented on Mar 19, 2026

    @eiriktsarpalis
    Member

    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.

  22. hez2010 commented on Mar 19, 2026

    @hez2010
    Contributor

    @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.

  23. eiriktsarpalis commented on Mar 19, 2026

    @eiriktsarpalis
    Member

    Are there any actionable opportunities to apply vectorization in the existing STJ infrastructure?

  24. tannergooding commented on Mar 19, 2026

    @tannergooding
    Member

    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

  25. locked and limited conversation to collaborators on Apr 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area-System.Text.Jsonenhancementin-prtenet-performance

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions