Repository navigation
Use bounds checks for keyword indices - #32
Merged
Merged
Conversation
Reject out-of-bounds keyword indices before retrieving values. Keep the native indexing fallback for unsupported selectors and providers so existing Not indexing remains correct and fast.
penelopeysm
reviewed
Nov 1, 2026
| # However, there's no `checkbounds` method (yet): | ||
| # https://github.com/rafaqz/DimensionalData.jl/issues/1156 | ||
|
|
||
| @inline function _canview_kw(optic::Index, x) |
Member
There was a problem hiding this comment.
Curious, is there a reason to inline?
Fresh-process comparisons show overlapping timings and identical allocation counts with and without the annotation. Let the compiler choose whether to inline the helper.
penelopeysm
approved these changes
Nov 1, 2026
penelopeysm
added a commit
that referenced
this pull request
Nov 1, 2026
Closes #27. Stacked on #32. Support native indexing in `canview` and `getvalue` for `Tuple`, `Ref` and custom containers, including dynamic indices, keyword indices and nested fields. ```julia getvalue((y=(1.0,),), @vn(y[1])) # 1.0 getvalue((y=Ref(2.0),), @vn(y[])) # 2.0 ``` Containers without a bounds interface use their native `getindex`. Concrete Tuple and Ref leaf lookups use bounds or assignment checks directly, avoiding an exception-handling fallback in log-density and gradient evaluation. Numbers and characters retain their existing scalar meaning. Interrupts from fallback access propagate. Validation: all 51,437 tests pass. Tests pair successful `hasvalue` and `getvalue` calls and cover custom positional/keyword indexers, nested and dynamic access, invalid paths, and unassigned refs. Wren integration probes restore the prior Tuple/Ref log-density and Enzyme gradient timings with no allocations. Array and custom lookups also remain allocation-free, with a small cost for checking existence compared with Wren's previous unchecked getter. --------- Co-authored-by: Penelope Yong <penelopeysm@gmail.com>
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.
Closes #31. Base for #29.
Use keyword bounds checks for standard indices, so an out-of-bounds lookup returns
falsewithout tryinggetindex. Keep native indexing as the fallback for other selectors and arrays without keyword bounds methods.The fallback remains necessary after DimensionalData #1183. This reproduces without loading VarNames in both DimensionalData 0.30.2 and 0.30.3:
The keyword bounds method dispatches through
Dimension{<:StandardIndices}.Not(1)falls outside that type, althoughgetindexaccepts it. Trying and catching that error on every lookup makes validNotindexing much slower. The guard therefore sends nonstandard selectors straight to the existing fallback. It also checks that a keyword bounds method exists before calling it.Raise the test-only DimensionalData minimum to 0.30.2, which first includes keyword bounds checks. Add cases for out-of-bounds keywords, range/colon selectors and arrays without keyword bounds methods. Existing
Nottests retain coverage of the selector fallback.Validation: all 51,414 tests pass. Lookup tests also pass on DimensionalData 0.30.2 and 0.30.3. Benchmarks retain the allocation counts for valid lookups. Invalid integer-keyword lookup uses no allocations and skips
getindex, instead of throwing and catching a bounds error.