lemire opened a new pull request, #70781:
URL: https://github.com/apache/doris/pull/70781
### Background
`JsonBinaryValue::from_json_string` turns JSON text into JSONB. It runs for
`CAST(... AS JSON)`, the `json_parse*` and `json_valid` functions, and JSONB
columns read from Arrow, protobuf, JNI and CSV. It calls `JsonbParser::parse`
(`be/src/util/jsonb_parser_simd.h`). That function builds a new
`simdjson::ondemand::parser` and a new `simdjson::padded_string` for every
document, then walks the document into a `JsonbWriter`. Callers that convert a
column already keep one `JsonBinaryValue`, and so one `JsonbWriter`, across
rows.
### What problem does this PR solve?
Issue Number: None
Related PR: None
PR Type: Performance
Problem Summary:
On short documents, such as log lines of 150 to 300 bytes, most of the cost
of `JsonbParser::parse` is allocating the parser's internal buffers and the
padded copy of the input for every row. This PR keeps them in
`JsonBinaryValue`, next to the writer it already reuses. `CAST` to JSONB, which
built a new `JsonBinaryValue` for every row, now keeps one per batch. The JSONB
produced is unchanged.
### Details
- **Why it was slow:** The cost follows from the code.
`simdjson::ondemand::parser` allocates its structural index and string buffers
on the first `iterate()`, and `padded_string` allocates and copies the input,
so each row pays for several allocations and frees.
- **Optimization:** `JsonbParser::ParseBuffers` holds a parser and a padded
input string. The new `parse(pch, len, writer, buffers)` overload copies the
input into that string, zeroes the 64 padding bytes as `padded_string` does,
and parses it. `JsonBinaryValue` owns a `ParseBuffers`. The three-argument
`parse` uses a local `ParseBuffers`, as before. `ParseJsonbFromString` takes
its `JsonBinaryValue` from the batch loop instead of creating one per row.
- **Results:** New `be/benchmark/benchmark_jsonb_parse.hpp`, which parses
1,024 log-like rows into a `JsonbWriter`, either with the three-argument
`parse` (fresh buffers for every row, as before) or with reused `ParseBuffers`.
RELEASE `benchmark_test` built in the
`apache/doris:build-env-ldb-toolchain-latest` image, Intel Xeon Gold 6548N
pinned to one core, median of 10 repetitions. The machine was shared (load
average about 35), and the coefficient of variation was 2.6 to 4.5%.
| Rows | Fresh buffers | Reused buffers | Speedup |
|---|---:|---:|---:|
| about 160 bytes | 382 ns/row | 320 ns/row | 1.19x |
| about 270 bytes | 819 ns/row | 728 ns/row | 1.13x |
| about 4.6 KB | 17.4 us/row | 17.2 us/row | not shown |
- **Applicability and cost:** It helps callers that parse many documents
through one `JsonBinaryValue`: `CAST` to JSONB, `json_parse*`, `json_valid`,
and the Arrow, protobuf and JNI readers.
`DataTypeJsonbSerDe::deserialize_one_cell_from_json` (CSV loads into JSONB
columns) still creates a `JsonBinaryValue` per cell, because the nullable serde
calls it one cell at a time. Gains shrink as documents grow and are not
measurable at about 5 KB. Memory: a `JsonBinaryValue` now keeps the parser
buffers (a few times its largest document) and a padded copy of that document
until it is destroyed, as its `JsonbWriter` already keeps its output. No new
config.
### Testing
- New `JsonBinaryValueTest.ReusedValueMatchesFreshParse`
(`be/test/core/value/jsonb_value_test.cpp`): one `JsonBinaryValue` parses
documents of decreasing length, so bytes of the previous document lie right
after each new one (for example digits after a root number), and each result
must equal the JSONB from a fresh `JsonBinaryValue`, including the failures.
- `./run-be-ut.sh --run
--filter='JsonBinaryValueTest.*:JsonbParserTest.*:*Jsonb*:*JsonParse*:*CastToJsonb*'`
in the `apache/doris:build-env-ldb-toolchain-latest` image (ASAN build): 201
tests from 38 suites pass, including `JsonbParserTest`, `FunctionJsonbTEST`,
`DataTypeJsonbSerDeTest` and the new test.
- `benchmark_test` does not build on master because of
`be/benchmark/variant/benchmark_variant_segment.hpp` (`StorageReadOptions` has
no `tablet_schema`), so its include was removed locally, and not in this PR, to
take the numbers above.
### Notes for Reviewers
The buffers live in `JsonBinaryValue` rather than in `thread_local` storage.
`thread_local` buffers would be allocated while one query's memory tracker is
attached and freed only at thread exit, so the query's tracker would not return
to zero (`MemTrackerLimiter::~MemTrackerLimiter`). A standalone measurement
showed no speed difference between the two.
### Release note
Faster conversion of short JSON strings to JSONB, such as `CAST(str AS
JSON)` and `json_parse`.
### Check List (For Author)
- Behavior changed: No
- Does this need documentation: No
- Pre-submission status: Ready
### AI Disclosure
- Tool: Claude Code (Claude Opus 5.5)
- Generated: code, tests, benchmark, and this description
- Human review: not stated
- Prompts: The author shared a plan to reuse the simdjson parser and input
buffer in `JsonbParser::parse` with `thread_local` storage, and asked the agent
to own and deliver it together with a related JSON reader change.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]