Repository navigation
Conversation
Add tests that compare the checkpoint writer and reader for text columns with today's per-value loop (_encode_object_values and _decode_object_values), which stays as the fallback: - a frame with pyarrow- and python-backed str and string columns, an object column of strings, string index values and string strata writes and reads without calling the per-value loop, writes the same file as the loop, and loads the same frame (values, dtypes, string storage, NA marker, Arrow type and chunk count); - empty columns, empty strings, non-ASCII UTF-8, one value throughout and a very long string, for each text dtype; multi-chunk, sliced and 32-bit offset Arrow layouts; NumPy strings in an object column; - columns with a missing or non-string value still go through the loop, and unencodable strings, invalid UTF-8 and malformed offsets raise the loop's errors; - an opt-in test (MICROCOSM_FRAME_CHECKPOINT_LARGE_TEXT_TEST=1) for a payload past 2**31 bytes. The tests that need the new conversion fail until it exists. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
write_frame_checkpoint and load_frame_checkpoint converted every text value with a Python loop (_encode_object_values, _decode_object_values). A text column, string index or strata whose values are all strings, none missing, now goes through NumPy and Arrow buffers instead, producing and parsing the same object_scalars_v1 bytes: a tag byte 7 before each value's UTF-8 bytes and one int64 offset per value. - Writing takes the column's values as one Arrow large-string array (64-bit offsets), from the stored chunks for pyarrow-backed str and string columns or from the object values for python-backed and object columns, and places the tag bytes and the UTF-8 bytes with NumPy. - Reading checks the offsets as before, checks every tag is the string tag, removes the tag bytes with NumPy, validates UTF-8 with Arrow and restores the declared dtype, string storage and NA marker. - A column with a missing value, a value that is not a string, a value Arrow refuses (a lone surrogate, invalid UTF-8) or a declared dtype that is not text goes through today's loop, which raises today's errors. Format versions, checkpoint bytes and recorded digests are unchanged. pyarrow.compute.binary_slice is not used: under pyarrow 25.0.0 it fails with "Negative buffer resize" on ordinary large-binary arrays. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add failing tests for what the first adversarial review found: - a str subclass that overrides encode() is written with the bytes its own encode() returns, as the per-value loop writes it; - writing object and python-backed string columns leaves the size of their non-ASCII str objects unchanged (pa.array attaches a cached UTF-8 copy to each one); - a pyarrow chunk whose offsets buffer has spare bytes beyond a whole number of int64 values is written like any other; - offsets whose differences overflow int64 raise the loop's error. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Object and python-backed string columns: only exact str and np.str_ values take the fast path, since the per-value loop calls each value's own encode(), which a subclass can override. Their bytes now come from str.encode through map and one bytes.join, not pa.array: Arrow reads a str with PyUnicode_AsUTF8AndSize, which attaches a cached UTF-8 copy to every non-ASCII str it reads for the rest of that str's life, so writing a checkpoint made a frame's non-ASCII strings larger. - pyarrow-backed columns: read exactly the array's own offsets from the Arrow offsets buffer, which may hold spare bytes beyond a whole number of int64 values. - Reading: offsets outside the payload, which pass the boundary check when their differences overflow int64, go to the per-value loop, which raises the same error as before; the byte mask is freed before the strings are built. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add failing tests for what the second adversarial review found: - a str subclass whose type hashes like str and compares equal to it, and one whose type cannot be hashed, are written by the per-value loop; an object column of floats whose type cannot be hashed is written at all; - encoding short strings from an object or python-backed column peaks at no more traced memory than the per-value loop (today about six times as much: one bytes object per value plus bytes.join's buffer table); - when pandas refuses to build a pyarrow-backed column from one Arrow array, as pandas 2.2 does for 2 GiB of text, the reader uses the loop. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Python strings: compare each value's type with str and np.str_ by identity (id), not through a set. A metaclass can make another type hash like str and compare equal to it, which let a str subclass with its own encode() through, or make a type unhashable, which made the write raise even for an object column of floats. - Python strings: encode 8,192 values at a time into one bytearray and compute the offsets in place. Encoding every value at once held one bytes object per value and bytes.join's buffer table, about six times the per-value loop's peak memory for short strings. The peak is now below the loop's: 31.9 MiB against 47.3 MiB for 2 million strings of 7 characters, 46.2 against 79.9 MiB for 200,000 strings of 200. - Reading pyarrow-backed columns: if pandas refuses to build the column from one Arrow array (pandas 2.2 casts it to 32-bit offsets, which refuses 2 GiB of text), use the per-value loop. - Tests: build the object arrays of values with unhashable types element by element, since np.asarray keys a dict by each value's type. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add tests for what the third adversarial review found: - byte comparison of Python-string columns across two whole encoding batches and one value more; no earlier test passed one batch, so a version that put every batch's lengths in the first batch's place passed them all; - peak traced memory of writing Python strings against the per-value loop, now also for 1,000 strings of 16 KiB and 20,000 of 400 characters (a batch's encoded values, their join and the bytearray were held at once, about 1.5 times the loop's peak); - peak traced memory of reading every text dtype, with and without a missing value, against the reader before the change (the new reader held the payload, a second copy of the text and a byte mask; its fallback held the payload array and its bytes copy together); - the frame case with an object index is relabelled: the writer stores an object index of strings as a pyarrow-backed str index, as before. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Reading: remove the tag bytes by moving the text to the front of the payload array in place, about 1 MiB of whole values at a time, and turn the offsets into the text's own offsets in place, so no second copy of the text, payload-sized mask or second offsets array is held while the values are built. When a value is not valid UTF-8, or pandas refuses the Arrow array, both arrays are read again for the per-value loop. Before the loop, the payload array is dropped once its bytes copy exists, as before the change. - Writing Python strings: refuse non-string columns with infer_dtype before the type scan; drop each batch's encoded values before keeping its join, and join the batches once at the end (a single batch is used as it is) instead of appending to a bytearray. - Writing pyarrow-backed columns: place the text with masks covering about 1 MiB of whole values at a time. - Docstrings say which columns use Arrow buffers and which str.encode, and every reason the reader leaves a column to the per-value loop. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- The reader now rewrites the offsets and payload HDF5 fills in place, and the Python-string encoder's payload is read-only, so the test with a payload past 2**31 bytes passes writable copies, as HDF5 would. - The memory tests take one dtype for each conversion path (str and string storage share one), and explain why each case is large against the fixed temporaries of one encoding batch or one step. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
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.
Tracker task 1.5 · 9 (project "15. US build memory, CPU and run time"). This carries out part B2 of decision 1.5 · 6: no new checkpoint format and no dictionary encoding. Instead, the checkpoint writer and reader build and parse today's bytes for text without a Python loop over each value.
What changes
Text columns, string index values and strata are stored with the
object_scalars_v1encoding:Before this change,
_encode_object_valuesand_decode_object_valuesconverted the values one at a time in Python. Now_write_seriesand_read_seriestry a conversion without that loop first.strandstringcolumns: the column's Arrow chunks are widened to 64-bit offsets and joined. The tag bytes and the UTF-8 bytes are then placed with NumPy masks, each covering about 1 MiB of whole values.strornp.str_, compared byid. Subclasses are excluded because the old code calls each value's ownencode(), which a subclass can override.str.encodethroughmap, 8,192 at a time. Each batch is joined withbytes.join, and the batches are joined once at the end.PyUnicode_AsUTF8AndSize, which attaches a cached UTF-8 copy to every non-ASCII string for the rest of its life.Everything else goes through today's per-value loop, unchanged, and raises the same errors:
strsubclass;Unchanged: checkpoint bytes, format versions 2 and 3 (version 3 only when a frame has a nullable boolean column), and every recorded file digest. That covers the US base steps, the QRF donor and recipient files, the ASEC raw-stage checkpoint, the multispine pool and UK stage checkpoints.
Tests
The new file
packages/microcosm-build/tests/engine_free/shared/test_frame_checkpoint_text_columns.pyproduces its expected bytes and values with today's loop, which is kept as the fallback.strandstringcolumns, an object column of strings, string index values and string strata:np.str_values in an object column.pd.NA) in every dtype; object integers, floats, bytes and booleans;strsubclasses, including ones whose type hashes likestror cannot be hashed; floats whose type cannot be hashed.MICROCOSM_FRAME_CHECKPOINT_LARGE_TEXT_TEST=1, about 4 GB of memory) for pyarrowstrand object columns. Skipped in CI.What I ran, under Python 3.14.6, pandas 3.0.3, pyarrow 25.0.0, numpy 2.4.6 and h5py 3.16.0:
I did not run the existing
test_frame_checkpoint.pyor any other suite; CI runs them. Each round of review fixes was committed as failing tests first, then the change.Timings
The timing script uses a synthetic copy of the 15 text series of one post-clone US base checkpoint, built as in the 1.5 · 6 timing script: 9.85 million values, with the real row counts, distinct counts and string lengths. Best of 3, on a 10-core Mac at load average 14–17.
write_frame_checkpoint/load_frame_checkpoint; files byte-identical):An earlier revision timed at load 7–8 ran at about 2.5 times these speeds: 3.13 → 0.28 s to write and 2.09 → 0.14 s to read the text. The ratios are similar.
Peak memory with
tracemalloc, compared side by side with the old code:Adversarial review: three rounds, each fixed with failing tests first
strsubclass overridingencode()got different bytes;pa.arrayattached cached UTF-8 copies to the frame's non-ASCII strings;IndexErrorwhere the old code raisedValueError.pyarrow.compute.binary_slice. Under pyarrow 25.0.0 it fails with "Negative buffer resize" on ordinary large-binary arrays.Not covered, and things noticed outside this change
test_us_puf_support_base_equivalence.py) was not run. Task 1.5 · 8, which would have made it runnable at reduced size, is cancelled.microcosm-frame'spandas>=2.3, but was not tested. The lock pins 3.0.3, and the CI and Modal images sync the lock. pyarrow is a dependency ofmicrocosm-build(pyarrow>=15), and every pyarrow call used exists in 15.strindex. Under pandas 3,pd.Series(index.array)infersstr, and that conversion attaches cached UTF-8 copies to the index's non-ASCII strings._check_object_offsetscompares offset differences, which wrap around int64 for offsets far outside the payload.Environment variables
None in production code.
MICROCOSM_FRAME_CHECKPOINT_LARGE_TEXT_TESTonly lets the opt-in large-payload test run; nothing provides it, so CI skips that test.🤖 Generated with Claude Code