Skip to content

Commit c0a2cec

Browse files
dcherianclaude
andcommitted
Short-circuit _getitem for direct variable-name keys
Skip the per-key _get_all fan-out when every key is already a plain data-variable name — the slow path would just fall through to the same result. Decoration via get_associated_variable_names is unchanged. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
1 parent af9fb12 commit c0a2cec

1 file changed

Lines changed: 82 additions & 53 deletions

File tree

‎cf_xarray/accessor.py‎

Lines changed: 82 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,62 +1125,91 @@ def check_results(names, key):
11251125
grid_mapping_names = []
11261126
grid_mapping_names.append("grid_mapping")
11271127

1128+
standard_names = accessor.standard_names
11281129
custom_criteria = ChainMap(*OPTIONS["custom_criteria"])
11291130

1130-
varnames: list[Hashable] = []
1131-
coords: list[Hashable] = []
1132-
successful = dict.fromkeys(key_iter, False)
1133-
for k in key_iter:
1134-
if "coords" not in skip and k in _AXIS_NAMES + _COORD_NAMES:
1135-
names = _get_all(obj, k)
1136-
names = drop_bounds(names)
1137-
check_results(names, k)
1138-
successful[k] = bool(names)
1139-
coords.extend(names)
1140-
elif "measures" not in skip and k in measures:
1141-
measure = _get_all(obj, k)
1142-
check_results(measure, k)
1143-
successful[k] = bool(measure)
1144-
if measure:
1145-
varnames.extend(measure)
1146-
elif "grid_mapping_names" not in skip and k in grid_mapping_names:
1147-
grid_mapping = _get_all(obj, k)
1148-
check_results(grid_mapping, k)
1149-
successful[k] = bool(grid_mapping)
1150-
if grid_mapping:
1151-
varnames.extend(grid_mapping)
1152-
elif "geometries" not in skip and (k == "geometry" or k in _GEOMETRY_TYPES):
1153-
geometries = _get_all(obj, k)
1154-
if geometries and k in _GEOMETRY_TYPES:
1155-
new = itertools.chain(
1156-
_parse_related_geometry_vars(
1157-
ChainMap(obj[g].attrs, obj[g].encoding)
1131+
# Fast path: every key is a direct variable name and matches no CF special key
1132+
reserved: set[Hashable] = set(_AXIS_NAMES).union(
1133+
_COORD_NAMES,
1134+
_GEOMETRY_TYPES,
1135+
("geometry",),
1136+
measures,
1137+
grid_mapping_names,
1138+
custom_criteria,
1139+
cf_role_criteria,
1140+
)
1141+
fast_path = (
1142+
isinstance(obj, Dataset)
1143+
and not skip
1144+
and all(
1145+
k in obj._variables
1146+
and k not in reserved
1147+
and standard_names.get(k, [k]) == [k]
1148+
for k in key_iter
1149+
)
1150+
)
1151+
1152+
varnames: list[Hashable]
1153+
coords: list[Hashable]
1154+
if fast_path:
1155+
varnames = list(key_iter)
1156+
coords = []
1157+
successful = dict.fromkeys(key_iter, True)
1158+
else:
1159+
varnames = []
1160+
coords = []
1161+
successful = dict.fromkeys(key_iter, False)
1162+
for k in key_iter:
1163+
if "coords" not in skip and k in _AXIS_NAMES + _COORD_NAMES:
1164+
names = _get_all(obj, k)
1165+
names = drop_bounds(names)
1166+
check_results(names, k)
1167+
successful[k] = bool(names)
1168+
coords.extend(names)
1169+
elif "measures" not in skip and k in measures:
1170+
measure = _get_all(obj, k)
1171+
check_results(measure, k)
1172+
successful[k] = bool(measure)
1173+
if measure:
1174+
varnames.extend(measure)
1175+
elif "grid_mapping_names" not in skip and k in grid_mapping_names:
1176+
grid_mapping = _get_all(obj, k)
1177+
check_results(grid_mapping, k)
1178+
successful[k] = bool(grid_mapping)
1179+
if grid_mapping:
1180+
varnames.extend(grid_mapping)
1181+
elif "geometries" not in skip and (k == "geometry" or k in _GEOMETRY_TYPES):
1182+
geometries = _get_all(obj, k)
1183+
if geometries and k in _GEOMETRY_TYPES:
1184+
new = itertools.chain(
1185+
_parse_related_geometry_vars(
1186+
ChainMap(obj[g].attrs, obj[g].encoding)
1187+
)
1188+
for g in geometries
11581189
)
1159-
for g in geometries
1160-
)
1161-
geometries.extend(*new)
1162-
if len(geometries) > 1 and scalar_key:
1163-
raise ValueError(
1164-
f"CF geometries must be represented by an Xarray Dataset. To request a Dataset in return please pass `[{k!r}]` instead."
1165-
)
1166-
successful[k] = bool(geometries)
1167-
if geometries:
1168-
varnames.extend(geometries)
1169-
elif k in custom_criteria or k in cf_role_criteria:
1170-
names = _get_all(obj, k)
1171-
check_results(names, k)
1172-
successful[k] = bool(names)
1173-
varnames.extend(names)
1174-
else:
1175-
stdnames = set(_get_with_standard_name(obj, k))
1176-
objcoords = set(obj.coords)
1177-
stdnames = drop_bounds(stdnames)
1178-
if "coords" in skip:
1179-
stdnames -= objcoords
1180-
check_results(stdnames, k)
1181-
successful[k] = bool(stdnames)
1182-
varnames.extend(stdnames - objcoords)
1183-
coords.extend(stdnames & objcoords)
1190+
geometries.extend(*new)
1191+
if len(geometries) > 1 and scalar_key:
1192+
raise ValueError(
1193+
f"CF geometries must be represented by an Xarray Dataset. To request a Dataset in return please pass `[{k!r}]` instead."
1194+
)
1195+
successful[k] = bool(geometries)
1196+
if geometries:
1197+
varnames.extend(geometries)
1198+
elif k in custom_criteria or k in cf_role_criteria:
1199+
names = _get_all(obj, k)
1200+
check_results(names, k)
1201+
successful[k] = bool(names)
1202+
varnames.extend(names)
1203+
else:
1204+
stdnames = set(_get_with_standard_name(obj, k))
1205+
objcoords = set(obj.coords)
1206+
stdnames = drop_bounds(stdnames)
1207+
if "coords" in skip:
1208+
stdnames -= objcoords
1209+
check_results(stdnames, k)
1210+
successful[k] = bool(stdnames)
1211+
varnames.extend(stdnames - objcoords)
1212+
coords.extend(stdnames & objcoords)
11841213

11851214
# these are not special names but could be variable names in underlying object
11861215
# we allow this so that we can return variables with appropriate CF auxiliary variables

0 commit comments

Comments
 (0)