Repository navigation
perf(cpp): cut allocations when deserializing device ids - #1090
Merged
Merged
Conversation
Two small fixes on the metadata deserialization path, which runs once
per device index entry and is hot during table scans:
* DeviceMetaIndexEntry::deserialize_from() built the device id with
StringArrayDeviceID("init") and immediately discarded those segments.
That constructor runs the path parser (ANTLR4 when enabled) on the
placeholder string, so every device paid a parse plus an allocation.
Default-construct instead.
* SerializationUtil::read_var_char_ptr() copied each segment through a
temporary mem_alloc'ed buffer: mem_alloc, memcpy, new std::string,
mem_free. Read straight into the string's own buffer instead, one
allocation per segment.
3.2M-row scan over 1000 devices: 0.376s -> 0.290s (~23%); the two
changes measured at ~17% and ~9% respectively.
Round-trip device ids whose segments include one longer than the small string buffer, an empty segment and a null segment, and pin the rewritten read_var_char_ptr() contract: null sentinel yields nullptr, zero length yields an empty string, negative length is rejected.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## develop #1090 +/- ##
===========================================
+ Coverage 66.51% 66.56% +0.04%
===========================================
Files 778 782 +4
Lines 56577 56712 +135
Branches 9105 9136 +31
===========================================
+ Hits 37633 37749 +116
- Misses 17291 17304 +13
- Partials 1653 1659 +6 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
read_var_char_ptr() resized the output string before reading, so a corrupt 4-byte length field (any value up to INT32_MAX) made it allocate and zero-fill up to 2 GiB before the short read was detected, and a failed resize threw std::bad_alloc out of the read path. Check the length against the remaining bytes first and return E_BUF_NOT_ENOUGH without allocating. Add a truncated-stream test. Also stop leaking the segment strings in the two new device-id tests: the StringArrayDeviceID constructor deep-copies them, so the caller keeps ownership (LeakSanitizer would flag the Ubuntu ASan job).
Parallel CTest processes can seed the name generator in the same clock tick; one then removes the file another is opening (E_ALREADY_EXIST at the fixture's open, observed as SEGFAULT / test failure on CI). Append pid and a per-process counter, as the writer tests already do.
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues block approval of the device-ID deserialization changes.
No open issues 🎉
What changed in this PR
This PR reduces allocation overhead in C++ device-ID deserialization, improving the metadata-reading path used by device lookups and scans.
Changes:
- Remove placeholder device-ID construction and read segments directly into string buffers.
- Reject invalid or oversized segment lengths before allocating, with tests for round trips, null/empty segments, and truncated input.
- Add process IDs and counters to reader-test filenames to avoid collisions during parallel execution.
| File | Description |
|---|---|
cpp/test/reader/tsfile_reader_test.cc |
Make generated filenames unique across parallel test processes. |
cpp/test/common/device_id_test.cc |
Add deserialization round-trip and invalid-input coverage. |
cpp/src/common/tsfile_common.h |
Default-construct device IDs without an unused placeholder. |
cpp/src/common/allocator/byte_stream.h |
Eliminate temporary segment buffers and validate lengths before allocation. |
Review effort: Balanced
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Two avoidable costs on the metadata deserialization path. It runs once per device index entry, and each device lookup re-deserializes a whole leaf node (up to
max_degree_of_index_node_ = 256entries), so these costs were paid hundreds of times per device on a scan:DeviceMetaIndexEntry::deserialize_from()built its device id withStringArrayDeviceID("init")and then immediately overwrote it. Constructing that string runs the path splitter (split_stringin the current build — theANTLR4code path is selected by a macro that is not defined for the library targets;ENABLE_ANTLR4only pulls in the runtime), plus astd::stringallocation, for a value that was never used. ~0.16 µs per entry.SerializationUtil::read_var_char_ptr()copied each segment through a temporarymem_alloc'ed buffer:mem_alloc,memcpy,new std::string,mem_free.Change
StringArrayDeviceIDinstead of passing"init".Benchmark
3.2M rows / 1000 devices, table-model full scan, single thread (alternating dylib A/B). Each device lookup deserializes a leaf of up to 256 entries, which is why a per-entry cost of ~0.16 µs shows up in the scan:
read_var_char_ptr"init"fixTesting
Full C++ suite: 1460 tests, 1457 passed, 3 skipped. New unit tests cover device-id round trips, null/empty segments, negative lengths and truncated streams.