Skip to content

Comments

throw LightBoundsError instead of BoundsError#345

Open
nsajko wants to merge 2 commits intoJuliaArrays:masterfrom
nsajko:LightBoundsError
Open

throw LightBoundsError instead of BoundsError#345
nsajko wants to merge 2 commits intoJuliaArrays:masterfrom
nsajko:LightBoundsError

Conversation

@nsajko
Copy link
Member

@nsajko nsajko commented Feb 20, 2026

Context: While BoundsError is common in the Julia world, its use is problematic for compiler optimizations. More specifically, Julia is gaining the ability to eliminate heap allocations as of recently, even for code that is not eliminated by dead code elimination or eliminated by constant folding. After the compiler gains interprocedural escape analysis capabilities, elimination of heap allocations should become applicable more often. The problem with BoundsError is that a BoundsError value holds a reference to the array that was attempted to be indexed out-of-bounds. Thus a BoundsError throw escapes the value of the array. Thus a BoundsError throw makes the elimination of the underlying heap allocation impossible, unless the throw is first eliminated by @inbounds or by other compiler optimizations.

The LightBoundsError exception from package LightBoundsErrors.jl addresses this by storing typeof(array) and axes(array) instead of storing array. The new package is already being adopted as a dependency by FixedSizeArrays.jl and by MemoryViews.jl.

Switching from BoundsError to LightBoundsError could hypothetically break a dependent package, in case the dependent branches on something like exception isa BoundsError while error handling in a catch block.

Context: While `BoundsError` is common in the Julia world, its use is
problematic for compiler optimizations. More specifically, Julia is
gaining the ability to eliminate heap allocations as of recently, even
for code that is not eliminated by dead code elimination or eliminated
by constant folding. After the compiler gains interprocedural escape
analysis capabilities, elimination of heap allocations should become
applicable more often. The problem with `BoundsError` is that a
`BoundsError` value holds a reference to the array that was attempted
to be indexed out-of-bounds. Thus a `BoundsError` throw escapes the
value of the array. Thus a `BoundsError` throw makes the elimination of
the underlying heap allocation impossible, unless the throw is first
eliminated by `@inbounds` or by other compiler optimizations.

The `LightBoundsError` exception from package
[LightBoundsErrors.jl](https://github.com/JuliaArrays/LightBoundsErrors.jl)
addresses this by storing `typeof(array)` and `axes(array)` instead of
storing `array`. The new package is already being adopted as a
dependency by
[FixedSizeArrays.jl](https://github.com/JuliaArrays/FixedSizeArrays.jl)
and by
[MemoryViews.jl](https://github.com/BioJulia/MemoryViews.jl).

Switching from `BoundsError` to `LightBoundsError` could hypothetically
break a dependent package, in case the dependent branches on something
like `exception isa BoundsError` while error handling in a `catch`
block.
@nsajko nsajko marked this pull request as ready for review February 20, 2026 09:22
@nsajko
Copy link
Member Author

nsajko commented Feb 20, 2026

The noalloc test failure on Julia v1.13 is unrelated.

@aplavin
Copy link
Member

aplavin commented Feb 20, 2026

I understand the motivation, but switching to an exception type from another package (not Base) is a non-starter for such a foundational and hugely popular package IMO...
What are the plans on getting such a lightweight "boundserror" into Base?

@nsajko
Copy link
Member Author

nsajko commented Feb 20, 2026

switching to an exception type from another package (not Base) is a non-starter for such a foundational and hugely popular package

I do not see why, when the time comes for a breaking release. StructArrays.jl already has three strong deps, why not add a fourth one?

What are the plans on getting such a lightweight "boundserror" into Base?

I suppose after a few months or years it would be good to make a PR to JuliaLang/julia, then make the LightBoundsErrors.jl package just export the functionality provided by Base for high enough version of Julia. However you would presumably still want to depend on the package to be able to support older Julia releases.

@nsajko
Copy link
Member Author

nsajko commented Feb 20, 2026

I suppose after a few months or years it would be good to make a PR to JuliaLang/julia

This presupposes that LightBoundsError will by required for the implementation of Base itself, though. Which is something I feel positive towards, but it may or may not materialize.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants