Skip to content

Performance: mapping a STR column rehashes every out-of-line string and writes into the private mapping #720

Closed

Description

@ihrfv

Since #398, col_validate_str_region recomputes hash32 for every out-of-line element of a STR column on both the load path and the mmap path. On the mmap path the write lands in a MAP_PRIVATE mapping, so the kernel copies every descriptor page, even when the stored hash was already right. Mapping a STR column therefore costs time proportional to rows plus pool bytes, and private memory of 16 bytes per row, every time the table is mapped.

;; 262 144 rows. STR column of 20-byte strings (out of line), and an I64 column for comparison.
;; Maps each 10 times, keeps every mapping alive, prints the time and the process footprint.
(set n 262144)
(.sys.exec "rm -rf /tmp/rfstrmap")
(.db.splayed.set "/tmp/rfstrmap/s/t" (table [s] (list (map (fn [i] (format "row-%-padding-x" (+ 10000000 i))) (til n)))))
(.db.splayed.set "/tmp/rfstrmap/i/t" (table [i] (list (til n))))
(set pid (get (.sys.info) 'pid))
(set fp (fn [label] (do (print (format "% " label)) (.sys.exec (format "footprint -p % 2>/dev/null | grep -E 'phys_footprint:'" pid)))))
(set run (fn [dir] (do (set t0 (.time.now))
                       (set held (map (fn [k] (.db.splayed.get dir)) (til 10)))
                       (println (format "% : 10 maps in % ms" dir (- (.time.now) t0)))
                       held)))
(fp "start")
(set H1 (run "/tmp/rfstrmap/i/t"))
(fp "after 10 maps of the I64 table")
(set H2 (run "/tmp/rfstrmap/s/t"))
(fp "after 10 maps of the STR table")
(.sys.exec "rm -rf /tmp/rfstrmap")
(exit 0)

Actual, v2.11.0 (dc9b12a6), macOS arm64. The STR column file is 12 058 880 bytes.

start     phys_footprint: 61 MB
/tmp/rfstrmap/i/t : 10 maps in 0 ms
after 10 maps of the I64 table     phys_footprint: 62 MB
/tmp/rfstrmap/s/t : 10 maps in 25 ms
after 10 maps of the STR table     phys_footprint: 102 MB

v2.9.1 gives the same: 26 ms, and 61 MB to 102 MB.

Each map of the STR table takes about 2.5 ms and adds 4.1 MB of private memory. 4.1 MB is the descriptor array, 262 144 × 16 bytes. The I64 table maps in no measurable time and adds nothing.

Expected: mapping a column file written by a current writer costs no time per row and leaves the descriptor pages shared with the page cache.

Where

col_validate_str_region, src/store/col.c:1311-1360 on v2.11.0 and :1277-1319 on v2.9.1. It makes two passes over hdr->len elements. The first checks bounds and memcmp's the 4-byte prefix against the pool. The second hashes each out-of-line string and stores elems[i].hash32 unconditionally. The comment above the second pass says why: files written before the hash cache carry padding there, and the mmap path relies on MAP_PRIVATE to repair the mapping without touching the file.

Two things would each help. Storing only when the value differs would keep pages of current files shared, though the hashing would still read every string. A marker in the column header, written once the writer computes the hashes, would let the reader skip the pass for those files and keep the repair for legacy ones.

dev at b3c8222b does not change src/store/col.c.

Impact

A read replica that remaps a table on every publish pays this on every STR column every time. On our data, 96 columns of which 14 are STR, it costs about 0.37 ms per 65 536-row STR column per map and 125 to 140 bytes of private memory per row per replica, and a remap of 5 million rows takes about 400 ms.

Activity

  1. changed the title [-]Mapping a STR column rehashes every out-of-line string and writes into the private mapping[/-] [+]Performance: mapping a STR column rehashes every out-of-line string and writes into the private mapping[/+] on Oct 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions