Skip to content
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

Forward length to parent #304

Merged
merged 2 commits into from
Jun 13, 2022
Merged

Forward length to parent #304

merged 2 commits into from
Jun 13, 2022

Conversation

jishnub
Copy link
Member

@jishnub jishnub commented Jun 13, 2022

This might lead to simpler code, especially if the length of the parent is easy to compute without evaluating a product (e.g. for StaticArrays where it is known at compile time, and may be constant-propagated).

On nightly with this PR

julia> A = OffsetArray(reshape(SA[1,2,3,4],2,2), 3, 4)
2×2 OffsetArray(reshape(::SVector{4, Int64}, 2, 2), 4:5, 5:6) with eltype Int64 with indices 4:5×5:6:
 1  3
 2  4

julia> @code_typed length(A)
CodeInfo(
1return 4
) => Int64

@codecov
Copy link

codecov bot commented Jun 13, 2022

Codecov Report

Merging #304 (b0cff2c) into master (9b018f0) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master     #304   +/-   ##
=======================================
  Coverage   96.43%   96.44%           
=======================================
  Files           5        5           
  Lines         449      450    +1     
=======================================
+ Hits          433      434    +1     
  Misses         16       16           
Impacted Files Coverage Δ
src/OffsetArrays.jl 98.29% <100.00%> (+<0.01%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9b018f0...b0cff2c. Read the comment docs.

Copy link
Member

@johnnychen94 johnnychen94 left a comment

Choose a reason for hiding this comment

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

I believe Julia compiler can optimize this well, but it's harmless to add this.

@jishnub
Copy link
Member Author

jishnub commented Jun 13, 2022

Without this, I find the code_native to be

julia> @code_native debuginfo=:none length(A)
	.text
	.file	"length"
	.globl	julia_length_2616               # -- Begin function julia_length_2616
	.p2align	4, 0x90
	.type	julia_length_2616,@function
julia_length_2616:                      # @julia_length_2616
	.cfi_startproc
# %bb.0:                                # %top
	movq	40(%rdi), %rax
	imulq	32(%rdi), %rax
	retq
.Lfunc_end0:
	.size	julia_length_2616, .Lfunc_end0-julia_length_2616
	.cfi_endproc
                                        # -- End function
	.section	".note.GNU-stack","",@progbits

whereas with this

julia> @code_native debuginfo=:none length(A)
	.text
	.file	"length"
	.globl	julia_length_2618               # -- Begin function julia_length_2618
	.p2align	4, 0x90
	.type	julia_length_2618,@function
julia_length_2618:                      # @julia_length_2618
	.cfi_startproc
# %bb.0:                                # %top
	movl	$4, %eax
	retq
.Lfunc_end0:
	.size	julia_length_2618, .Lfunc_end0-julia_length_2618
	.cfi_endproc
                                        # -- End function
	.section	".note.GNU-stack","",@progbits

so it seems that the imulq is avoided. I'm uncertain if the compiler can do this by itself, maybe it can, but I guess this is harmless, as you say.

@jishnub jishnub merged commit dfb8540 into JuliaArrays:master Jun 13, 2022
@jishnub jishnub deleted the length branch June 13, 2022 10:02
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