Skip to content

perf(zarr-indexing): skip the sort in _last_occurrences for sorted indices - #442

Open
d-v-b wants to merge 9 commits into
mainfrom
claude/zarr-indexing-last-occurrences-fastpath-a3bb59
Open

d-v-b wants to merge 9 commits into
mainfrom
claude/zarr-indexing-last-occurrences-fastpath-a3bb59

Conversation

@d-v-b

@d-v-b d-v-b commented Nov 6, 2026 •

Copy link
Copy Markdown
Owner

🤖 AI text below 🤖

Writes whose indices land on a chunk in sorted order no longer sort them to drop repeated indices, so deduplication is linear in the number of points; writes without repeats also skip a gather copy. Unsorted indices behave as before.

_last_occurrences now checks in O(n) whether the flat positions are non-decreasing, the usual case since a chunk's points arrive in C order. If they are, repeats are adjacent and the last of each run is kept via np.flatnonzero(np.append(flat[1:] != flat[:-1], True)). If there are no repeats, or fewer than two positions, it returns slice(None), so _scatter_numpy and _scatter_planned index without copying. Unsorted input still goes through np.unique. Callers only use the result to pick matching rows of coordinates and values, so the order of the kept positions doesn't matter. The sorted-input check is borrowed from the last_occurrences helper that Bhaskar Gurram (bhaskargurram-ai) wrote in upstream zarr-developers#4601; credit for the idea goes to them.

A new parametrized test, test_last_occurrences_matches_sequential_assignment, checks empty, single, sorted, sorted-with-repeats, unsorted-with-repeats and reversed input against sequential NumPy assignment.

Timing for _last_occurrences on 5M intp positions (NumPy 2.5.1, Python 3.13, macOS arm64, best of 5):

input before after speedup
sorted, no repeats 12.9 ms 1.2 ms 11.0x
sorted, repeats 183.3 ms 3.1 ms 59.6x
unsorted, repeats 766.7 ms 765.2 ms 1.0x

The zarr-indexing suite passes on Python 3.13 (1745 passed), and just typecheck and just lint are clean. On Python 3.15, four tests fail on main too, because they match the __index__ error message that 3.15 changed.

🤖 Generated with Claude Code

d-v-b and others added 7 commits November 4, 2026 09:58
…elopers#4597)

* feat(zarr-metadata): draw nested fields and codec pipelines

Add Hypothesis strategies for struct, cast_value and sharding_indexed,
which draw the fields they hold from the same Strategies mapping, and a
`pipelines` strategy that walks a codec pipeline, drawing each codec for
the chunk the codec before it hands on via its definition's transition.
A shard's inner and index pipelines are drawn for the chunks its
definition's `pipelines` gives. Shards and structs nest at most one
level deep, and strategies are wrapped in a mapping with a short repr so
Hypothesis's error notes stay under its 30,000-character warning.

Every strategy record's functions now take the Strategies mapping as
their last argument.

Assisted-by: ClaudeCode:claude-opus-5-5

* fix(zarr-metadata): reject non-numeric data types in arithmetic codecs

`NOT_NUMBERS` predates the zarr-python data types, so `cast_value` and
`scale_offset` accepted `fixed_length_utf32`, `null_terminated_bytes`,
`raw_bytes` and `variable_length_bytes`, and the strategies drew them.
The cast_value spec defines the codec only for floating-point and
integral data types.

Assisted-by: ClaudeCode:claude-opus-5-5

* fix(zarr-metadata): let the later of two same-named strategies win

`_scope` documented this but raised `ScopeConflictError`, so `declarations`
failed for any definition named as a default, which worked before this
branch. Pipelines and data type names now draw only the definition that
claims a name, and `declarations` reads a draw with its own definition
claiming the name.

Assisted-by: ClaudeCode:claude-opus-5-5

* test(zarr-metadata): check a same-named definition reads its own draws

The test compared a draw with the bare name, which depended on the
example Hypothesis returned and said nothing of which definition read
it. Every spelling is now read by the definition it was drawn for.

Assisted-by: ClaudeCode:claude-opus-5-5

* chore(zarr-metadata): number the nested strategies changelog fragments after zarr-developers#4597

Assisted-by: ClaudeCode:claude-opus-5-5

* fix(zarr-metadata): keep same-named definitions out of nested draws

The struct and shard strategies removed the default definition by
identity before drawing what they hold. A custom definition with the
same name stayed in the inner strategies, so it could claim the name
there and be drawn, and the default then refused the result with a
ValueError. Filter by name instead.

Assisted-by: ClaudeCode:claude-opus-5-5

* fix(zarr-metadata): draw long runs in rectilinear dimensions

A run held at most 4 chunks, so the `[size, count]` spelling was never
drawn for what it is for, and a long dimension was covered by a few
small chunks and one huge one. Draw counts sized to the dimension, and
always write a run too long to list as a pair. Reach tests now cover
each spelling of a dimension, not only that a pair is drawn at all.

Assisted-by: ClaudeCode:claude-opus-5-5

* fix(zarr-metadata): sample float fills, shard factors and struct fields evenly

A measured audit found strategies that drew valid metadata from little
of what they could. Hex float fills came from a small-biased integer, so
83% of float64 fills were subnormal and few were normal. Integer float
fills stopped at the limit of the narrowest type. Rectilinear edges
rarely shared a factor, so 70% of a shard's axes held inner chunks of
length 1. A struct field's type was drawn after its name, which made
the first data type about a fifth of all fields.

Draw hex fills from floats, infinities and NaN payloads as well as raw
bits, limit integers by width, draw edges with a common factor, and draw
a field's type first. Divisor search is now bounded, so a very long
shard no longer takes billions of steps. New tests measure shares at the
number of examples a test runs, which plain reach tests do not see.

Assisted-by: ClaudeCode:claude-opus-5-5

* fix(zarr-metadata): keep regular chunk lengths within a signed 64-bit size

Axes can be up to 2**63 - 1 long, but regular and bare rectilinear chunk
lengths reached twice that, which a reader indexing with int64 cannot
hold. Cap them. A rectilinear last edge needs no cap: the runs before it
cover at least one factor, so rounding up cannot pass the axis.

Correct two docstrings, and require a covering run to count more than one
chunk in its reach test, since the leftover edge passed it on its own.

Assisted-by: ClaudeCode:claude-opus-5-5

* fix(zarr-metadata): draw non-finite and hex scale_offset values

An offset and a scale are fill values of the chunk's data type, but the
strategy drew only finite floats, and never an integer spelling. It now
draws every spelling a fill value takes, and still never a scale of zero.

Assisted-by: ClaudeCode:claude-opus-5-5
…add definitions with union (zarr-developers#4598)

* feat(zarr-metadata)!: replace scope update with copy(update=...) and add definitions with union

`ZarrV3Context.update(**slots)` and `ZarrV2Context.update(**slots)` read
as a merge, but replace each slot whole, so passing one new definition
silently dropped every default. They become `copy(*, update=None)`,
after pydantic's `model_copy(update=...)`, whose replace-whole meaning
pydantic users already know. Adding definitions is `union`, which keeps
every definition the scope has and refuses a conflicting one; the docs,
docstrings and call sites now use it instead of splicing a slot's
definitions back in.

Fixes #424

Assisted-by: ClaudeCode:claude-opus-5-5

* docs(zarr-metadata): number the scope copy changelog fragment after its PR

Assisted-by: ClaudeCode:claude-opus-5-5

* test(zarr-metadata): check copy refuses another format's definition and name copy in scope docstrings

Assisted-by: ClaudeCode:claude-opus-5-5
updates:
- [github.com/astral-sh/ruff-pre-commit: v0.16.10 → v0.17.3](astral-sh/ruff-pre-commit@v0.16.10...v0.17.3)
- [github.com/DavidAnson/markdownlint-cli2: v0.23.3 → v0.24.0](DavidAnson/markdownlint-cli2@v0.23.3...v0.24.0)
- [github.com/zizmorcore/zizmor-pre-commit: v1.30.1 → v1.31.1](zizmorcore/zizmor-pre-commit@v1.30.1...v1.31.1)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Davis Bennett <davis.v.bennett@gmail.com>
* feat(zarr-metadata): draw whole v3 documents

Add `array_documents`, `group_documents` and
`consolidated_group_documents`. An array's members are drawn to fit each
other: the chunk grid for the shape, the fill value for the data type,
and the codecs for the grid's chunks of that data type. A consolidated
listing holds each node's parent, as a group, before the node.

`pipelines` takes `spellings`, as `declarations` does. A drawn document
its definitions refuse raises a ValueError, and strategies with nothing
to draw some member from raise a LookupError. `Spellings` and
`CHARACTERS` move to `_strategy` so the new module can share them.

Assisted-by: ClaudeCode:claude-opus-5-5

* chore(zarr-metadata): number the document strategies changelog fragment after #439

Assisted-by: ClaudeCode:claude-opus-5-5

* fix(zarr-metadata): sample document shapes, listings and data types evenly

Shapes capped axes at 2**16 and rank at 5, with a quarter of documents
rank 0 and few holding a zero-length axis. Draw each rank about as often,
up to 8, and axes that are zero, short, or past 32 bits. Draw a document's
data type first: drawn after the shape, the first in the list was a fifth
of all documents. A listed group now sometimes holds the empty listing
the reference implementation writes, a fresh one for each group. New
tests bound these shares.

Assisted-by: ClaudeCode:claude-opus-5-5

* chore(zarr-metadata): number the document strategies changelog fragment after zarr-developers#4602

Assisted-by: ClaudeCode:claude-opus-5-5
)

Bumps the actions group with 3 updates: [astral-sh/setup-uv](https://github.com/astral-sh/setup-uv), [scientific-python/upload-nightly-action](https://github.com/scientific-python/upload-nightly-action) and [zizmorcore/zizmor-action](https://github.com/zizmorcore/zizmor-action).


Updates `astral-sh/setup-uv` from 10.3.2 to 10.3.3
- [Release notes](https://github.com/astral-sh/setup-uv/releases)
- [Commits](astral-sh/setup-uv@07a047e...68559e9)

Updates `scientific-python/upload-nightly-action` from 0.6.5 to 0.6.6
- [Release notes](https://github.com/scientific-python/upload-nightly-action/releases)
- [Commits](scientific-python/upload-nightly-action@16fa02e...5a74a2a)

Updates `zizmorcore/zizmor-action` from 0.7.1 to 0.8.0
- [Release notes](https://github.com/zizmorcore/zizmor-action/releases)
- [Commits](zizmorcore/zizmor-action@85b2b9f...53290ba)

---
updated-dependencies:
- dependency-name: astral-sh/setup-uv
  dependency-version: 10.3.3
  dependency-type: direct:production
  update-type: version-update:semver-patch
  dependency-group: actions
- dependency-name: scientific-python/upload-nightly-action
  dependency-version: 0.6.6
  dependency-type: direct:production
  update-type: version-update:semver-patch
  dependency-group: actions
- dependency-name: zizmorcore/zizmor-action
  dependency-version: 0.8.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
  dependency-group: actions
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Davis Bennett <davis.v.bennett@gmail.com>
…dices

When the flat in-hull positions are non-decreasing (the usual C-order
case), repeats are adjacent and the last occurrence of each can be found
in O(n) without np.unique. When there are no repeats at all, return a
full slice so callers index without a gather copy.

Assisted-by: ClaudeCode:claude-opus-5-5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Assisted-by: ClaudeCode:claude-opus-5-5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…s#4603

Assisted-by: ClaudeCode:claude-opus-5-5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant