Skip to content

perf(fts): reduce embedded Jieba startup time and memory use - #929

Open
egolearner wants to merge 4 commits into
alibaba:mainfrom
egolearner:codex/defer-embedded-jieba-init
Open

egolearner wants to merge 4 commits into
alibaba:mainfrom
egolearner:codex/defer-embedded-jieba-init

Conversation

@egolearner

@egolearner egolearner commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

Opening a collection with an embedded Jieba FTS field currently loads the dictionary and builds a mutable trie during pipeline setup. This PR defers that work until the first operation that uses the tokenizer, then uses a precompiled dictionary and a shared read-only trie.

Implementation

  • Prepare the embedded Jieba pipeline when opening the collection. Bind explicitly configured dictionaries during setup so path and configuration errors still surface early.
  • Generate the bundled dictionary as typed columns. The generator also records trie level boundaries and the order in which to visit dictionary entries. For the bundled 348,982 entries, 12 contiguous row ranges encode that order.
  • On first use, scan those ranges once to populate a contiguous node array. Exact lookup and DAG construction reference the same dictionary entries. User dictionaries continue to use cppjieba's mutable trie.

Measurements

Historical read-only collection timings on Linux (host 191)

Both Release builds used the same one-document collection on an Intel Xeon Platinum 8369B, opened it read-only, and searched for 分词.

Operation main at PR base (a26f8ab) · two RSS-probe runs PR · latency probe, median of 30 fresh processes
Open the collection read-only 420.5 ms / 431.0 ms 30.20 ms
First FTS query after open 3.8 ms / 3.0 ms 62.57 ms
Repeat the same FTS query Not timed individually 0.11 ms

The two main values in each row are elapsed times between consecutive memory snapshots, in run order. Those intervals include reading process memory statistics. The probe took its next snapshot after 31 further queries. The PR values come from the dedicated latency probe with a warm filesystem cache. The probes used different timing boundaries, so these figures do not support a Linux speedup ratio. The matched macOS benchmark below measures both builds with the same timer.

Footprint on host 191 main at PR base PR
Process RSS after 32 FTS queries, median of 12 fresh processes 211.09 MiB 150.54 MiB
CPython 3.11 wheel 18,012,499 bytes 17,887,835 bytes

The historical Linux PR build used the code committed as 07ef4a1. The only later change at the PR head (e3f26b8) renamed a local variable to satisfy GCC's shadowing check.

Read-only collection latency

The table compares the PR base with the PR head on macOS arm64, using Release builds of the C API. Both versions opened the same on-disk collection with one embedded Jieba FTS field and one document containing 中文分词技术. Each fresh process opened the collection read-only and searched twice for 分词. Both queries had to return the expected document. Values are medians over 30 fresh processes per version, with alternating run order and a warm filesystem cache.

Operation PR base (a26f8ab) PR head (e3f26b8)
Open the collection read-only 110.0 ms 9.47 ms
First FTS query after open 0.677 ms 19.4 ms
Repeat the same FTS query 0.107 ms 0.107 ms
From start of open to first query result 110.7 ms 29.2 ms

The base initializes Jieba during open. The PR moves that cost to the first FTS query and reduces the time to the first result by 74%. The last row is measured end to end in each process.

Validation

  • Ran the Jieba tokenizer, tokenizer pipeline manager, FTS column indexer, and FTS query test binaries directly on Linux. All 131 tests passed. The Jieba and FTS query binaries also passed on macOS (26 tests).
  • Checked every bundled word and prefix against cppjieba, plus DAG edges at multiple word-length limits, shared initialization, and a synthetic dictionary with duplicate and out-of-order entries.
  • Used the built wheel to open a read-only collection and run a Chinese FTS query. All four tokenizer modes produced identical token text, byte offsets, and positions across the benchmark corpora.

Prepare the tokenizer pipeline at collection open and load the embedded dictionary on the first tokenizing read or write. Share the immutable base trie across pipelines while binding external dictionaries at open.
Generate trie level offsets and lexical row ranges alongside the precompiled dictionary. Build contiguous nodes from the shared dictionary entries on first use and keep cppjieba lookup semantics for both exact words and DAG edges.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant