Skip to content

document: quote, and allocators in the storages - #3

Merged
pinhopro merged 24 commits into
mainfrom
pmr
Nov 15, 2026
Merged

pinhopro merged 24 commits into
mainfrom
pmr

Conversation

@pinhopro

Copy link
Copy Markdown
Contributor

Summary

Two additions to webcpp::document, both made for webcpp::trystero (webcpporg/webcpp#17) and both general:

  • quote: JSON.stringify's quoting of a string alone, appended to any string of the caller's (the writer's own code, moved, not duplicated); invalid UTF-8 is errc::type, the output untouched.
  • Allocators in the storages: basic_owned<Allocator> and basic_external<Allocator> (owned, external unchanged as their defaults), with pmr::document, pmr::value, pmr::const_value, pmr::parser. The allocator holds the owned image (16-aligned blocks, checked) and every operation's working state; documents are allocator-aware as the standard containers are; nothing(alloc) holds nothing and allocates nothing; a whole text through parse, parse_into or the new finish_with(last) never allocates Boost.JSON's stack; erasure allocates nothing and cannot run out of room; const reads ask the allocator for nothing but the cases doc/allocation.adoc lists. With exceptions, an allocator's std::bad_alloc (or Boost.Interprocess's) is errc::capacity with the content unchanged.

One source break: owned and external are aliases now, so a forward declaration struct owned; no longer compiles (changelog, Unreleased). Image bytes, sizes and every existing test are unchanged.

Verification

From scratch: native with and without exceptions (72 tests, 21 examples), emscripten in debug and release with and without exceptions (67), wasip2 and wasip3 (68), the oracle, the JavaScript module with Chrome (5 of 5), the page, clang-tidy in four modes, and a fresh independent review whose findings are fixed with tests that failed first.

🤖 Generated with Claude Code

R added 20 commits November 14, 2026 19:51
A public quote(text, out) appends a string to a string of the caller's as a
JSON string literal, with the bytes stringify writes of the same string: the
writer's QuoteJSONString moves to <webcpp/document/quote.hpp>, and the writer
calls it. The UTF-8 overload refuses text that is not UTF-8 with errc::type and
leaves the output unchanged; the UTF-16 overload escapes a lone surrogate as
JSON.stringify does since ES2019. Only the output allocates, which the test
proves with a std::pmr::string on a counting resource and a replaced operator
new.
Writing JSON gains a section, A string alone, quoted, whose listing is a new
region of document_streaming.cpp: a JSON text written field by field into a
std::pmr::string on a monotonic arena, a lone surrogate of UTF-16, and text
that is not UTF-8 refused. The changelog names quote under Unreleased.
…UTF-8 cases

The test compared quote with stringify, which writes strings through the
same detail::quote_json, so those checks proved nothing. It now compares
quote with the bytes Node 26 writes, held as literals: a long string of
quotes, each escaped, and a run of UTF-16 groups past the writer's buffer.

It accepts C1's controls, C2 80 to C2 9F, as they are, and refuses lead
bytes past F4, overlong forms of three and four bytes, and sequences of
four bytes cut short, as Node's TextDecoder in its fatal mode refuses them.
basic_owned<Allocator> and basic_external<Allocator> are the storages of a
document, owned and external the standard allocator's, pmr::owned and
pmr::external a memory resource's, with pmr::document, pmr::basic_document,
pmr::value, pmr::const_value and pmr::parser. A document is allocator-aware as
the standard containers are: its owned image is allocated, grown and freed
through the allocator, rebound to 16-byte blocks; a move takes the allocator;
a move assignment between allocators that are not equal and do not propagate
copies the image; the copy constructor asks
select_on_container_copy_construction; create, clone, from_image, format, open,
parse, parse_into and the parser take an allocator; and the documents that
pop, shift, splice, slice and concat make are in the source's allocator. With
exceptions, an allocator that has no room for the image is errc::capacity.
The tree's reading half, tree_view, reads a document of any allocator, so a
deep copy crosses allocators. The working state still uses the standard
allocator but for compaction's stack, which follows.
…r_stored

Four test programs, empty yet, for the working state of the tree, the handles,
the parser and the writer in a document's allocator; a resource that fails
from a chosen allocation on; and tree_view::member_stored, a lookup by a key in
the stored encoding, which transcodes nothing.
basic_document::nothing(alloc) is the state a moved-from document is in,
named: no image, nothing allocated and no failure, its root reading
undefined, a change and clone refused with errc::type until a move assignment
gives it an image, so that a library can hand one out where it has no
document to give, without exceptions too. Also clean what clang-tidy reported
in the storage, the region and the move assignment, and in the counting
tests' arena.
The document's allocator, rebound, now holds all that an operation needs
while it runs, for owned and external documents alike, and nothing of it
reaches the global operator new:
- the tree stages a change's nodes, values, probe sizes and doomed roots in
  it, transcodes a new UTF-16 key straight into its block of the image, and
  sorts a literal's repeated keys in it; a lookup by a UTF-8 key compares it
  with stored UTF-16 keys unit by unit, and an erasure lists its subtree
  through the erased nodes' own scalars, so neither allocates nor fails;
  erase_member_by_id erases a member by its node; compaction's stack;
- a handle of a missing member holds its key there, copies included; a value
  is prepared there; Object.assign's keys, the array methods' entries and the
  containers' views likewise; == walks both trees with no stack at all;
- the parser's frames, path, keys, pending text, selectors and callbacks,
  which a type-erased delivery in the allocator holds instead of
  std::function; finish(last) gives Boost.JSON the last chunk with
  more=false, so that parse, parse_into and a whole text given to finish
  never suspend and never allocate Boost.JSON's stack, the one allocation a
  parse in chunks still makes in the global heap;
- the writer's stack and the piece stringify_to holds back for its sink.
With exceptions, an allocator with no room is errc::capacity and the
operation changes nothing, since every change allocates its working state
before its first change; stringify, which returns its text, throws
std::bad_alloc. The four allocator tests count the global operator new,
check each resource is given back all it handed out, and fail every
operation at each of its allocations in turn.
The tree's and the values' allocation tests take arenas of several MB, so
their memory may grow on Emscripten, and the external document's buffer of
64 KiB is on the heap, past Emscripten's stack of 64 KiB.
A section of its own, after Images, names the storages and what their
allocator holds, shows a document parsed, changed and written in a resource
of the program's own, cloned into a second and moved back, lists how each
document is made in an allocator, how moves and copies treat it, what an
allocator with no room does, what stays outside it, and which types can be
shared between processes, as the coding guidelines ask. The pages on images,
parsing, writing JSON, errors, the sugar and testing say what is now true of
their memory, and AGENTS.md the section, its program and the rule an
operation's allocations follow.
…ocator's own pointer

An allocator may ignore an over-aligned request, and a 16-aligned block is over-aligned
wherever std::max_align_t's alignment is 8 (arm64 macOS, MSVC): an image at another
alignment put rbtree_best_fit's first block where the tree does not look. A buffer that
is not 16-aligned is now given back at once and the operation returns errc::capacity.
The region keeps the allocator's pointer to the image as the allocator gave it and gives
the blocks back through it, never through a pointer rebuilt from the address, which a
pointer that carries a segment or a tag would not be. Tests: an allocator that aligns
to 8, and one whose pointer carries a tag.
An operation caught only std::bad_alloc inside noexcept functions, so an allocator of
Boost.Interprocess, whose bad_alloc derives from std::exception alone, ended the
program. detail::unless_allocation_fails, the one place that catches an allocator's
failure, takes std::bad_alloc, a type derived from it, and Boost.Interprocess's
bad_alloc; the image, a change's staging, the conversions, the writer, the selectors
and the parser go through it. The page and the storages state the requirement: what an
allocator throws besides those ends the program. Test: a resource that fails with
Boost.Interprocess's bad_alloc at every step of the operations that allocate.
A standard container allocates nothing in a const member, so several threads may read
one at once; a pmr document's const reads raced inside its resource. stringify now keeps
its stack in its std::string's allocator and asks the document's allocator for nothing.
The writer's stack has room of its own for 16 frames, and the piece stringify_to holds
back 4096 bytes of its own, the default chunk's, so that a value nested 16 deep written
in chunks of at most 4096 bytes allocates nothing, and a dozen small texts no longer take
4 KiB each of a monotonic arena; past that room they grow in the document's allocator by
doubling. allocation.adoc lists the const reads that still allocate there (stringify_to
past its room, a missing member of a long key, clone, slice, concat, compact_into), and
allocation.adoc and sharing.adoc say that threads using them at once need a synchronized
resource. Tests: the const reads ask nothing, a dozen small texts in a 64 KiB arena, and
the writer's allocations counted past its own room.
…tors that rebind explicitly

pmr::external's move assignment never copies, its image being no allocator's, and is
now noexcept with every external storage and every allocator that propagates or is
always equal; static_asserts pin it. The Allocator requirements ask only that a
rebound allocator be constructed from another, A a(b): a prepared value's buffers and
the array methods' entries converted the document's allocator implicitly, and now
construct their own. Tests: an allocator whose rebinding constructor is explicit,
stateful and neither standard nor polymorphic, through the parser whole and in chunks
with a selector, the handles, Object.assign, the array methods, compaction, a copy
and stringify_to past its own room, under both traits; an allocator that propagates
on a move assignment; and one always equal though stateful. The tests' aligned
operator new now takes any power of two, as the standard's does, which an allocator
of char asks.
…value

A handle of nothing prepared the value in the global heap, and a handle of a
document that holds nothing, or of a place gone since, in the document's allocator,
before set returned errc::type, against the page's promise that such a document
allocates nothing. set now asks first whether the place can take a change (a
document with an image, a node still there, a missing member of an object with a
UTF-8 key it could hold, a missing element of an array) and refuses with errc::type,
or errc::capacity for a key it could not hold, before anything is prepared: the
refusal comes before the value's own errors and allocates nothing. Tests: every kind
of value through four places that cannot take it, and a document that holds nothing,
with no allocation anywhere.
…every key order

fails_softly now requires each failure to be the resource's one refusal and every
byte the operation took before it given back, besides the document's content as it
was, so that a leak on a path of failure fails. The tree's allocation tests ran under
JavaScript's key order alone; they run under sorted and insertion_order keys too.
… reuse

Reviving bytes, every string allocated a buffer sized for base64 before the text was
read, and every object read as bytes reserved its count before its shape was checked:
the text is now read first, without allocating, and the buffer allocated at the bytes'
exact size, and an object's shape is checked before its buffer, so that text that is
not base64 and objects of another shape allocate nothing. A prepared value's clear()
freed its buffers, against the parser's keeping it to reuse them: it now keeps them,
with their room, for the values prepared next, so that the parser's strings transcoded
to UTF-16 allocate as often as the longest grows. Tests: parses that revive bytes
allocate as often as parses that do not, and a prepared value's strings and bytes
allocate once.
finish(std::string_view last) overloaded the published finish(), so that
&basic_parser::finish no longer compiled without a cast. The member that takes the
last chunk, or the whole text, is now finish_with, which parse and parse_into call,
and finish() is untouched; a static_assert pins its address. The changelog names
the one source break of the storages, a forward declaration of owned or external,
which are aliases now, and the new member.
… failure leave

An erasure cannot run out of room, but it still returns errc::type for what is no
object or no array and errc::range for elements past the end. An operation that fails
for want of room leaves the document's content as it was, while the bytes of the
heap's free space may differ and a failed parse_into leaves its buffer formatted. The
storages list the working state their allocator holds, the parser's Storage names the
storages of any allocator, a member's erasure lowers its object's key count, and two
code spans that Asciidoctor rendered with a typographic apostrophe are reworded.
AGENTS.md states the allocator's rules as they are now.
…oc Comment

clang-tidy, with exceptions and without, found a braced return without its trailing
comma in the region, a string_view::substr that the analysis sees throw inside a
noexcept read of base64, and in the tests handles and results that can be const,
casts through void, a function too complex, helpers outside the anonymous namespace,
single characters appended as strings, a test helper unused without exceptions, and
a source read after a move that threw by design. The fluent-chain rule found calls
three deep in the allocation tests. The Doc Comments rule found a colon after
@ref quote, which MrDocs drops.
R added 4 commits November 15, 2026 10:58
…y reads alike on every standard library

On libstdc++ allocator_traits::pointer is an alias of T*, so a const before it qualified the pointer through an alias, which misc-misplaced-const refuses; libc++ declares the alias otherwise and the local lint did not see it.
tree_view::member checks the key of a UTF-16 document for UTF-8 under
if constexpr: the plain if's condition is the constant false under
the default traits, which MSVC 14.3 refuses in every program (C4127,
an error under /WX). Each member of a selector's table of operations
has an initializer, as trystero's handler's has, else MSVC 14.3 and
14.5 warn that operations_of, which sets every member, is filled with
zeros (C4268). allocator_tree_test names its image limit image_limit:
basic_document::create's parameter max_bytes hid the test's global of
that name, and MSVC warns of that wherever it compiles the member
(C4459). allocator_parse_test names the raw string that holds an
escaped quote outside BOOST_TEST_EQ, whose argument MSVC 14.3's
preprocessor splits there (C2017).

Each was reproduced with MSVC 19.44 and 19.50 on Compiler Explorer;
fixed, each compiles there under /W4 /WX, and the table's callbacks,
before and after, are called as they should be.
…rger piece only when it needs them

A stringify_to that keeps to its stack's and its piece's own rooms now
makes no standard container. The MSVC STL's checked iterators, a debug
build's default, allocate a proxy through a container's allocator when
the container is made, so each such stringify_to took two blocks of
the document's resource there: against the page's reads that allocate
nothing, which several threads may make at once whatever the resource,
and against the dozen small texts that take nothing of a 64 KiB arena.
…ks on every standard library

The MSVC STL's checked iterators allocate a 16-byte proxy through a
container's allocator whenever the container is made or moved, in its
noexcept constructors too. The debug lanes therefore counted more
blocks than the document asked for, and ended the program (exit status
3) where a failing resource refused a proxy inside such a constructor.

counting.cpp measures what an empty std::pmr::vector allocates. A
counting resource leaves blocks of that size and alignment out of its
counts, and a failing resource never refuses one nor counts it against
its budget; where a standard library allocates nothing so, nothing is
measured and the tests are what they were. A block of the document's
own of that size and alignment, as the two 64-bit words a change's plan
may reserve, is neither counted nor refused there: no operation whose
allocations are counted exactly makes one, as a run with the measure
forced to it on Clang and GCC shows, and the failures at it are proven
on the other standard libraries. The small texts of stringify_to are
counted with every block, so that a container the writer made for them
fails there too; stringify is compared with a reference string made in
the same count, its stack's list with what a container allocates for
itself. The page says what such a build allocates, and that a proxy an
allocator has no room for ends the program.
@pinhopro
pinhopro merged commit b9d9e4f into main Nov 15, 2026
25 checks passed
@pinhopro
pinhopro deleted the pmr branch November 17, 2026 04:06
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