lemire opened a new pull request, #70780:
URL: https://github.com/apache/doris/pull/70780

   ### Background
   
   The v2 JSON reader (`be/src/format_v2/json/json_reader.cpp`) parses each 
document with the simdjson On-Demand parser, which needs the input followed by 
`SIMDJSON_PADDING` (64) readable bytes. The reader keeps a reusable 
`_padding_buffer` for that. In routine load and stream load (`FILE_STREAM`), a 
document is one pipe message, or several chunks of a chunked HTTP transfer. 
When `read_json_by_line` is false, a document is the whole file split.
   
   ### What problem does this PR solve?
   
   Issue Number: None
   Related PR: None
   PR Type: Performance
   
   Problem Summary:
   
   Pipe messages and whole-file documents were first copied into 
`_document_buffer`, then copied again into `_padding_buffer` before parsing. 
Whole-file reads also zero-filled a freshly allocated `_document_buffer` the 
size of the file. In addition, `open()` zero-filled an 8 MB `_padding_buffer` 
in every reader, even for small documents. This PR reads and copies documents 
straight into `_padding_buffer`, and grows that buffer to the largest document 
instead of allocating 8 MB up front. Results and error handling are unchanged.
   
   ### Details
   
   - **Why it was slow:** The cost follows from the code. For each pipe 
message, `_read_one_document_from_pipe` does `_document_buffer.assign(...)`, 
then `_parse_next_json` does `memcpy(_padding_buffer, _document_view, size)`. 
For a whole-file document, `_document_buffer.resize(n)` allocates and zeroes 
`n` bytes, `read_at` fills them, and the same `memcpy` follows. `open()` runs 
`_padding_buffer.resize(8 MB + 64)` every time.
   - **Optimization:** `_read_one_document_from_pipe` copies messages, and 
appends chunks, directly into `_padding_buffer`. Whole-file mode calls 
`read_at` directly into `_padding_buffer`. `_parse_next_json` copies only when 
the document is not already at the start of the buffer. That is line mode, 
where the line reader's span is still borrowed, and a skipped UTF-8 BOM, where 
it uses `memmove` because the ranges overlap. `_document_buffer` and 
`_padded_size` are removed. The buffer grows with `std::string::resize`, which 
keeps the contents and grows capacity geometrically.
   - **Results:** A standalone program reproduces the old and new buffer 
handling with simdjson On-Demand and a full traversal of every value. These are 
not Doris end-to-end numbers. Apple M4, clang, `-O3`, best of 30 iterations, 
two runs (range shown):
   
     | Workload | Before | After |
     |---|---:|---:|
     | Pipe, 20,000 messages of 89 B | 1.05–1.07 GB/s | 1.15–1.16 GB/s |
     | Pipe, 2,000 messages of 6.3 KB (twitter statuses) | 3.42 GB/s | 
3.56–3.58 GB/s |
     | Whole file, twitter.json (632 KB) | 2.93–3.04 GB/s | 3.28–3.30 GB/s |
     | Whole file, citm_catalog.json (1.7 MB) | 3.28–3.36 GB/s | 3.75–3.96 GB/s 
|
     | Whole file, 40 MB array | 1.93–1.95 GB/s | 2.16–2.19 GB/s |
   
     A new reader's 8 MB zero-fill costs about 49 µs on the same machine, while 
a 1 KB document parses in about 1 µs.
   - **Applicability and cost:** Applies to `FILE_STREAM` (routine load, stream 
load) and to whole-file JSON. Line mode (`read_json_by_line`) keeps its single 
copy and only benefits from the smaller initial allocation. The padding buffer 
is now sized to the largest document seen by the reader, plus 64 bytes. Before, 
it was at least 8 MB. A reader that sees a large document grows the buffer 
once, as before. No new config.
   
   ### Testing
   
   - `be/test/format_v2/json/json_reader_test.cpp`: the line-mode test now 
checks that the document is still borrowed from the line reader and that the 
padding buffer fits the longest line plus padding (not 8 MB). The whole-file 
outer-array test checks that the document is read in place and that the buffer 
is the file size plus padding.
   - `./run-be-ut.sh --run --filter='JsonReader*'` in the 
`apache/doris:build-env-ldb-toolchain-latest` image (ASAN build): 35 tests from 
`JsonReaderTest`, `JsonReaderSplitTest` and `JsonReaderPrefetchTest` pass.
   - The pipe path has no unit test in this file. Its buffer logic (chunked 
appends that grow the buffer, a BOM in whole-file and line mode, a long line 
after short ones) was checked in a standalone program under ASan and UBSan.
   
   ### Release note
   
   Faster JSON routine load, stream load, and whole-file JSON load, with less 
memory per JSON reader.
   
   ### Check List (For Author)
   
   - Behavior changed: No
   - Does this need documentation: No
   - Pre-submission status: Partially verified: the performance numbers come 
from a standalone program that reproduces the reader's buffer handling, not 
from a Doris benchmark.
   
   ### AI Disclosure
   
   - Tool: Claude Code (Claude Opus 5.5)
   - Generated: code, tests, and this description
   - Human review: not stated
   - Prompts: The author asked to apply the single-copy fix of 
nodejs/node#67623 to Doris JSON loading, then to also look at the 8 MB buffer 
allocated in `open()` and at whether a newer simdjson would help (left for a 
separate proposal).
   


-- 
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]

Reply via email to