Skip to content

Enable stacking of 0-dim arguments #3 #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 27, 2025

Conversation

manuelbb-upb
Copy link
Contributor

I needed stacking to work with 0-dim objects.
This seems closely related to issue #3.
For me, however, the arguments to StackView really are array-like.
Julia's builtin stack can handle 0-dim objects.
With these small changes, the following code should work, too:

using StackViews

# 0-dim arrays
x = Array{Float64}(undef)
y = Array{Float64}(undef)

z = StackView(x, y)
z isa AbstractVector{Float64}   # true
z[1] = 1
z[2] = 2
z == [1, 2] # true

# 0-dim views
A = rand(2)
a1 = view(A, 1)
a2 = view(A, 2)
ndims(a1) == ndims(a2) == 0 # true
B = StackView(a1, a2)
B isa AbstractVector{Float64}   # true
B[1] = 1
B[2] = 2
B == A      # true, setting values in `B` affects views of `A`
B == [1, 2] # true

# Numbers 
# Numbers work (if given as `slices::Tuple`)
v = StackView((1, 2))
v isa AbstractVector{Int} # true
# setting values does not work! numbers are immutable!
# `Ref` does not work neither, issues with OffsetArrays

@@ -119,7 +119,7 @@ function Base.axes(A::StackView{T,N,D}) where {T,N,D}
prev, post = Base.IteratorsMD.split(frame_axes, Val(D-1))

# use homogenous range to make _append_tuple happy
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I use typeof(first(frame_axes)) rather than eltype(prev) for a "performance" reason.
Mostly Any[...] vs [...]`, can you check if they still work similarly in terms of performance for non-trivial large arrays (ttfx time and average time)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can do some comparisons today. Used eltype(prev) here because _apend_tuple assumes a homogeneous tuple prev anyways, and I thought eltype would be something like eltype(::NTuple{N, T}) where {N, T}=T. Upon closer inspection, eltype on tuple (or rather tuple types) is a bit more complicated, but there does not seem to be obvious problems with type stability.
With previous typeof(first(frame_axes)) there were issues because frame_axes == () for 0-dim slices. Alternative: new helper _fill_range_type with methods for non-empty tuples (falling back to typeof(first(frame_axes)) and for empty tuples (returning Base.Bottom e.g. )

@johnnychen94
Copy link
Member

I just updated and enabled the CI on the master branch. Might be worth rebase on that so we have CI to test against this PR.

Most of the functionality seemed to just work.
Only `axes` needed changing.
Copy link

codecov bot commented May 26, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (1546b7b) to head (7367dda).
Report is 2 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff            @@
##            master       #15   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            1         1           
  Lines           45        47    +2     
=========================================
+ Hits            45        47    +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@johnnychen94 johnnychen94 merged commit 0943204 into JuliaArrays:master May 27, 2025
17 checks passed
@johnnychen94
Copy link
Member

Sorry, I forgot to mention it, @manuelbb-upb would you mind submitting a PR with tests for 0-dim arguments?
This can be very helpful to identify potential regressions in the future.

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