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

docstring setindex #32738

Merged
merged 6 commits into from
Oct 2, 2019
Merged

docstring setindex #32738

merged 6 commits into from
Oct 2, 2019

Conversation

matbesancon
Copy link
Contributor

Even though it's not exported, it is nice to have the semantics available in a docstring, to use it elsewhere (see https://github.com/JuliaDiffEq/ArrayInterface.jl/pull/15/files)

base/tuple.jl Outdated
setindex(c::Tuple, v, i::Integer)

Creates a new tuple similar to `x` with the value at index `i` set to `v`.
An out-of-bound `i` makes it a no-op.
Copy link
Member

Choose a reason for hiding this comment

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

is that intentional? Seems like odd behavior. I would assume it should boundscheck by default?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No idea, @vtjnash was the last to edit the line

Copy link
Sponsor Member

Choose a reason for hiding this comment

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

Was introduced in #20154 as a simple helper function. The behavior is very much intentional there, but perhaps we should see if we can add a @boundscheck block without sacrificing performance if we're going to move towards this being more documented.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Wouldn't it be considered as a breaking change then (replacing noop with BoundsError)?

Copy link
Sponsor Member

Choose a reason for hiding this comment

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

Not really as it was neither exported nor documented. That's why if we document it, it'd be good to make sure the semantics are what we want.

Copy link
Member

Choose a reason for hiding this comment

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

@mbauman mbauman added the domain:docs This change adds or pertains to documentation label Jul 30, 2019
@matbesancon
Copy link
Contributor Author

First take on @mbauman's idea:
https://gist.github.com/matbesancon/69fca8afdba2a65fc3a539cf18863a13

It seems slightly slower, but not all the time (which is curious)

@matbesancon
Copy link
Contributor Author

@tkf for the moment only ArrayInterface.jl is using setindex on arrays, maybe this could be considered in Base

@matbesancon
Copy link
Contributor Author

up here, I updated the gist above, there seems to be some significant performance hits when typeof(v) is not a bitstype (Array, String).
cc @mbauman

@matbesancon
Copy link
Contributor Author

matbesancon commented Aug 20, 2019

In any case, @inbounds removes the performance overhead (phew)

@matbesancon
Copy link
Contributor Author

bump here, @mbauman?

@mbauman
Copy link
Sponsor Member

mbauman commented Sep 24, 2019

I don't think we should document it to be a no-op for out of bounds accesses. I think we should change the behavior to check bounds with @boundscheck before documenting it.

@matbesancon
Copy link
Contributor Author

Thanks, I'll make the commits following the gist above

@matbesancon
Copy link
Contributor Author

no idea why the tests are failing here, it seems unrelated to setindex

@mbauman
Copy link
Sponsor Member

mbauman commented Sep 30, 2019

No, I believe that's very much related. Looks like we've been relying on the ignore-out-of-bounds behavior in some internal implementations.

@matbesancon
Copy link
Contributor Author

Sorry yes, it's a bit weird to have part of the CI running fine.
The error comes from selectdim:

@inline selectdim(A::AbstractArray, d::Integer, i) = _selectdim(A, d, i, setindex(map(Slice, axes(A)), i, d))
@noinline function _selectdim(A, d, i, idxs)
    d >= 1 || throw(ArgumentError("dimension must be ≥ 1, got $d"))
    nd = ndims(A)
    d > nd && (i == 1 || throw(BoundsError(A, (ntuple(k->Colon(),d-1)..., i))))
    return view(A, idxs...)
end

The good news is this should be fixable without breaking, but could lead to performance regression. Since we are errorring in setindex, the check should be before calling it, meaning we cannot @inline selectdim anymore as easily (there needs to be branching with error throwing here)

@mbauman
Copy link
Sponsor Member

mbauman commented Oct 1, 2019

I'd just copy the old implementation and call it something internally like _dropping_setindex or somesuch.

As far as why ~half the CI services report ✅ and the other half report ❌, it's because we've split the process in two parts. The "package" buildbots just build Julia whereas the "test" buildbots are the ones that actually run the test suite. There are other buildbots that specifically run a handful of other checks.

@matbesancon
Copy link
Contributor Author

I'd just copy the old implementation and call it something internally like _dropping_setindex or somesuch.

The other possibility is using the intern _setindex implementation, just did that

@StefanKarpinski
Copy link
Sponsor Member

Any reason not to just merge this once CI passes?

@matbesancon
Copy link
Contributor Author

this is changing some behavior if somebody was using the no-op as a feature

@mbauman
Copy link
Sponsor Member

mbauman commented Oct 1, 2019

I say we merge it — it's a (formerly) undocumented internal function, so if folks were relying on this somewhat obscure functionality it's on them.

@mbauman
Copy link
Sponsor Member

mbauman commented Oct 2, 2019

Thanks so much for the patience and endurance in pushing this one through, @matbesancon!

@mbauman mbauman merged commit b6991f2 into JuliaLang:master Oct 2, 2019
@matbesancon
Copy link
Contributor Author

Thanks for the help along the way, my pleasure :)

@matbesancon matbesancon deleted the setindex-doc branch October 2, 2019 15:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:docs This change adds or pertains to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants