Skip to content

Commit 35e164a

Browse files
committed
perf(zarr-metadata): resolve judges the envelope without walking the JSON it refined
`resolve` refines the whole field before reading it, yet its envelope check walked each configuration member as JSON again, and each nested field's configuration once more. `envelope_problems` judges the envelope alone, and `resolve` asks it of the field and of each nested field; `validate_metadata_field_v3`, for a value nothing has refined, is it plus the JSON check of the configuration's members. For a shard holding four codecs, a resolve makes 766 Python calls instead of 1,070. Assisted-by: ClaudeCode:claude-opus-5-5
1 parent 0ca46e5 commit 35e164a

2 files changed

Lines changed: 38 additions & 9 deletions

File tree

‎packages/zarr-metadata/src/zarr_metadata/v3/_common.py‎

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,23 @@ def validate_metadata_field_v3(
3636
"""Return every reason `value` is not a v3 metadata field.
3737
3838
A metadata field is a bare name string or a mapping containing `name` and
39-
optional `configuration` and `must_understand` members.
39+
optional `configuration` and `must_understand` members: an envelope, as
40+
`envelope_problems` judges it, around a configuration whose members are
41+
JSON.
42+
"""
43+
envelope = envelope_problems(value, allow_must_understand_false=allow_must_understand_false)
44+
return (*envelope, *_configuration_json_problems(value))
45+
46+
47+
def envelope_problems(
48+
value: object, *, allow_must_understand_false: bool
49+
) -> tuple[ValidationProblem, ...]:
50+
"""Every reason `value` is not a v3 metadata field's envelope, what its configuration holds left unjudged.
51+
52+
The envelope is what sits around the configuration: a string `name`, a
53+
`configuration` that is an object of string keys, a boolean
54+
`must_understand`, and nothing else. `resolve` asks this of a field it
55+
has refined to JSON already, so a configuration is walked once.
4056
"""
4157
if isinstance(value, str):
4258
return ()
@@ -72,9 +88,6 @@ def validate_metadata_field_v3(
7288
problems.append(
7389
ValidationProblem(("configuration",), "expected string keys", "invalid_type")
7490
)
75-
else:
76-
for key, item in cast("Mapping[str, object]", configuration).items():
77-
problems.extend(prefixed("configuration", prefixed(key, validate_json(item))))
7891
if "must_understand" in field:
7992
must_understand = field["must_understand"]
8093
if not isinstance(must_understand, bool):
@@ -92,6 +105,23 @@ def validate_metadata_field_v3(
92105
return tuple(problems)
93106

94107

108+
def _configuration_json_problems(value: object) -> tuple[ValidationProblem, ...]:
109+
"""Each member of `value`'s configuration that is not JSON, located; nothing where no object of string keys is there to walk."""
110+
if not isinstance(value, Mapping):
111+
return ()
112+
configuration = cast("Mapping[object, object]", value).get("configuration")
113+
if not isinstance(configuration, Mapping):
114+
return ()
115+
members = cast("Mapping[object, object]", configuration)
116+
if not all(isinstance(key, str) for key in members):
117+
return ()
118+
return tuple(
119+
found
120+
for key, item in cast("Mapping[str, object]", members).items()
121+
for found in prefixed("configuration", prefixed(key, validate_json(item)))
122+
)
123+
124+
95125
def is_metadata_field_v3(value: object) -> TypeGuard[ZarrV3MetadataFieldJSON]:
96126
"""Whether `value` is a v3 metadata field: a bare name or a named config."""
97127
if isinstance(value, str):
@@ -113,6 +143,7 @@ def parse_metadata_field_v3(value: object) -> ZarrV3MetadataFieldJSON:
113143

114144
__all__ = [
115145
"ZarrV3MetadataFieldJSON",
146+
"envelope_problems",
116147
"is_metadata_field_v3",
117148
"parse_metadata_field_v3",
118149
"validate_metadata_field_v3",

‎packages/zarr-metadata/src/zarr_metadata/v3/_definition.py‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
typeddict_keys,
5959
unread_in,
6060
)
61-
from zarr_metadata.v3._common import ZarrV3MetadataFieldJSON, validate_metadata_field_v3
61+
from zarr_metadata.v3._common import ZarrV3MetadataFieldJSON, envelope_problems
6262

6363
if TYPE_CHECKING:
6464
from collections.abc import Sequence
@@ -530,9 +530,7 @@ def _located(prefix: Loc, problems: Iterable[ValidationProblem]) -> Problems:
530530

531531
def _envelope(field: _NestedField) -> Problems:
532532
"""What is wrong with a nested field's envelope, at the field."""
533-
return _located(
534-
field.loc, validate_metadata_field_v3(field.json, allow_must_understand_false=False)
535-
)
533+
return _located(field.loc, envelope_problems(field.json, allow_must_understand_false=False))
536534

537535

538536
def _configuration_checked(
@@ -652,7 +650,7 @@ def _resolve_field(
652650
a `must_understand` of `false` says nothing about it, so it is reported
653651
beside the field that was read, which later layers can still judge.
654652
"""
655-
envelope = _located(loc, validate_metadata_field_v3(data, allow_must_understand_false=False))
653+
envelope = _located(loc, envelope_problems(data, allow_must_understand_false=False))
656654
resolved, found = _read(data, kind, context, loc)
657655
return resolved, (*envelope, *found)
658656

0 commit comments

Comments
 (0)