Skip to content

view: add experimental map views - #103

Merged
edsiper merged 5 commits into
masterfrom
map-views
Nov 26, 2026
Merged

edsiper merged 5 commits into
masterfrom
map-views

Conversation

@edsiper

@edsiper edsiper commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

Problem

In high-load environments, transforming key/value data is on the hot path. Today every transformation has to build a cfl_kvlist for each map first: a heap pair, a key string and a variant per attribute, most of which are never read or changed. In Fluent Bit this is the largest remaining cost when a processor edits log attributes at a high rate.

What this adds

cfl_view: a performance optimization for data transformation under high load. A view gives the interface of a string-keyed map over data that stays where it already is (the bytes it arrived in, another library's objects, or a native cfl_kvlist), so pairs are looked up and edited without building a CFL object for each one.

A view is a table with one entry per pair: a borrowed key and an opaque backend handle. Values are not decoded at load time. Lookups and edits run on that table, and the original data is never written, so it works as a copy-on-write overlay.

  • Reading: cfl_view_find(), cfl_view_find_ex() (case-insensitive, like cfl_kvlist_fetch()), cfl_view_value_at(), cfl_view_get(). Scalars are returned in place without allocating.
  • Editing: typed append and replace for string, bytes, bool, int64, uint64, double and null; rename; remove.
  • CFL objects: arrays, kvlists or any cfl_variant can be handed to a view, which takes ownership.
  • Ownership modes: keys and values passed to edits are borrowed by default; cfl_view_copy_set() makes the view copy them into its own storage (a lazily created arena, reused across loads).
  • Materialization: cfl_view_materialize(), cfl_view_materialize_in() (arena) and cfl_view_materialize_at() build native CFL objects from the current content.
  • Backends: two optional callbacks (value, materialize). CFL ships one for cfl_kvlist, with cfl_view_kvlist_commit() to write the edits back to the list, including into arena-backed lists.

Views do not replace cfl_kvlist: a view can always be materialized into one, and the normal containers remain the right choice when throughput is not a concern.

How this differs from referenced values. CFL variants can already borrow a string or bytes payload (referenced). That skips the payload copy, but every pair still costs a cfl_kvpair, a cfl_variant and a key copy, and every value is decoded whether it is read or not. A view removes that per-pair work: no objects per pair, borrowed keys, values decoded only when read. The Fluent Bit path this was measured against already uses referenced values and an arena.

VIEW.md documents the model, the ownership rules, and how to write a backend and an encoder.

Compatibility

  • Additive only: one new public header, one new source file, no change to existing symbols, layouts or behavior.
  • The interface is marked experimental in the header, VIEW.md, the README and the changelog: names, layouts and semantics can change in any release until that notice is removed.
  • cfl_view_find() compares keys byte by byte. This differs from cfl_kvlist_fetch(), which ignores ASCII case; cfl_view_find_ex() provides that behavior.

Downstream

The intended first consumer is Fluent Bit (log attribute processing); ctraces attributes are the natural second one. No consumer needs changes for this PR. I validated it against a Fluent Bit tree ported to this API: build, unit tests under Valgrind, focused integration tests normally and under strict Valgrind, and an A/B run showing no CPU change against the in-tree version of the same code.

Testing

scripts/agent-verify.sh        # 36/36 tests pass, git diff --check clean
  • cfl-test-view: ten cases covering kvlist reads of every type, edit + commit ordering, typed literals, copy mode, owned objects, arena-backed lists, a backend over data that is not made of CFL objects, and table growth.
  • cfl-test-view is clean under Valgrind (--leak-check=full) and under ASan + UBSan, and passes under MemorySanitizer locally.
  • cfl_view.h is part of the self-contained header checks, and the installed-consumer fixture uses a view.
  • Builds without warnings (-Wall -Wextra) with gcc gnu99, gcc gnu17 and clang gnu99.

CI fixes included

Two small commits unrelated to views, needed for a green run on any branch:

  • scripts: clear CDPATH with an explicit empty string fixes the Shellcheck SC1007 failure in scripts/agent-*.sh.
  • ci: reduce address randomization for the sanitizer jobs fixes clang-sanitize-memory, which has failed on every run (including master) since ubuntu-latest moved to Ubuntu 26.04. A different set of tests segfaulted each time, including programs with an empty main(). The job now lowers vm.mmap_rnd_bits on the runner before the Clang 11 analysis container starts.

A view is a performance optimization for high-load environments where
transforming key/value data is on the hot path. It gives the interface
of a string-keyed map over data that stays where it already is: the
bytes it arrived in, or a native cfl_kvlist. It keeps one entry per
pair with a borrowed key and a backend handle, so lookups, renames,
removals and inserts do not build a CFL object for every key and
value.

The interface covers typed append and replace, owned CFL objects,
optional copying of the keys and values handed to edits, exact and
case-insensitive lookup, materialization into a kvlist or an arena,
and a kvlist backend that commits the edits back to the list.

It is marked experimental: names, layouts and semantics can change.

Signed-off-by: Eduardo Silva <eduardo@chronosphere.io>
Exercise reads, typed edits, copy mode, owned objects, arena-backed
lists, a backend over data that is not made of CFL objects, and table
growth. Add the header to the self-contained header checks and use a
view from the installed consumer fixture.

Signed-off-by: Eduardo Silva <eduardo@chronosphere.io>
Add VIEW.md with the model, the API, the ownership rules, and guides
for writing a backend and an encoder. Reference it from the README,
the changelog and the contributor guide.

Signed-off-by: Eduardo Silva <eduardo@chronosphere.io>
Shellcheck reports SC1007 for 'CDPATH= cd', which fails the lint job
on every pull request. Assign the empty string explicitly; the
behavior is the same.

Signed-off-by: Eduardo Silva <eduardo@chronosphere.io>
The clang-sanitize-memory job fails on every run since ubuntu-latest
moved to Ubuntu 26.04: a different set of tests segfaults each time,
including programs that only contain an empty main(). The Clang 11
sanitizer runtime in the analysis container reserves fixed address
ranges, which the higher mmap randomization of the new runner kernel
can overlap.

Lower vm.mmap_rnd_bits on the runner before the analysis container
starts.

Signed-off-by: Eduardo Silva <eduardo@chronosphere.io>
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