Commit Graph

10 Commits

Author SHA1 Message Date
Conrad Ludgate
ce6bbca8d7 make use of esc string opt 2025-07-18 09:59:38 +01:00
Conrad Ludgate
d6f4dc4949 provide EscapedStr type for faster string encoding 2025-07-18 09:59:38 +01:00
Conrad Ludgate
b8435190d1 make unicode handling cold 2025-07-18 09:59:38 +01:00
Conrad Ludgate
ff08c78489 rather than using bytes[start..i], we can slice the bytes instead and use bytes[..i]. this ends up optimising better 2025-07-18 09:59:35 +01:00
Conrad Ludgate
654be07090 the ESCAPE table contains the escape character we need to insert, so we don't need to roundtrip via the CharEscape enum.
We also don't need to return the 'rest' slice, we can just insert it into the vec

Lastly, we can simplify the hotloop by making write_char_escape cold and moving the vec write inside this fn
2025-07-18 09:56:56 +01:00
Conrad Ludgate
8ba106d832 move str values into str module 2025-07-18 09:46:00 +01:00
Conrad Ludgate
03522b3434 turn KeyEncoder into a marker trait 2025-07-18 09:44:55 +01:00
Conrad Ludgate
e2bd8e4c61 vendor some serialization tests from serde_json 2025-07-18 09:40:22 +01:00
Conrad Ludgate
44201814b9 add benchmark for json str escaping 2025-07-18 09:38:48 +01:00
Conrad Ludgate
55aef2993d introduce a JSON serialization lib (#12417)
See #11992 and #11961 for some examples of usecases.

This introduces a JSON serialization lib, designed for more flexibility
than serde_json offers.

## Dynamic construction

Sometimes you have dynamic values you want to serialize, that are not
already in a serde-aware model like a struct or a Vec etc. To achieve
this with serde, you need to implement a lot of different traits on a
lot of different new-types. Because of this, it's often easier to
give-in and pull all the data into a serde-aware model
(serde_json::Value or some intermediate struct), but that is often not
very efficient.

This crate allows full control over the JSON encoding without needing to
implement any extra traits. Just call the relevant functions, and it
will guarantee a correctly encoded JSON value.

## Async construction

Similar to the above, sometimes the values arrive asynchronously. Often
collecting those values in memory is more expensive than writing them as
JSON, since the overheads of `Vec` and `String` is much higher, however
there are exceptions.

Serializing to JSON all in one go is also more CPU intensive and can
cause lag spikes, whereas serializing values incrementally spreads out
the CPU load and reduces lag.
2025-07-07 15:12:02 +00:00