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

Type-unstable similar and copy #356

Closed
cdsousa opened this issue Sep 10, 2015 · 5 comments · Fixed by #370
Closed

Type-unstable similar and copy #356

cdsousa opened this issue Sep 10, 2015 · 5 comments · Fixed by #370

Comments

@cdsousa
Copy link
Contributor

cdsousa commented Sep 10, 2015

For example, doing code_warntype(copy, (Images.Image{Float64,2,Array{Float64,2}},)), yields an abstract ::Images.Image{T,N,A<:AbstractArray{T,N}} return type.

This also happens with the similar method, and seems to be due to a type-unstable Image constructor.

@cdsousa
Copy link
Contributor Author

cdsousa commented Sep 11, 2015

I've around this and it seems the type-unstability resumes to line https://github.com/timholy/Images.jl/blob/master/src/core.jl#L14 ,
and more concretely to the ndims return value which, unlike eltype and typeof, doesn't allow full type inference of the new Image object...

@cdsousa
Copy link
Contributor Author

cdsousa commented Sep 11, 2015

And this seems to be an upstream issue:

type X{T}; end
f(x) = X{ndims(x)}()
code_warntype(f, (Array{Int,2},))

returns

Variables:
  x::Array{Int64,2}

Body:
  begin  # none, line 1:
      return call((top(apply_type))(Main.X,2)::Type{_<:X{T}})::X{T}
  end::X{T}

Does anyone knows if this is something already discussed in Julia list or issues?

@timholy
Copy link
Member

timholy commented Sep 26, 2015

It's due to this:

julia> @inferred(deepcopy(img.properties))
ERROR: return type Dict{ASCIIString,Any} does not match inferred return type Any
 in error at ./error.jl:21

which is used in Images' copy function. We use it because of this:

julia> p = Dict{ASCIIString,Any}("Hi" => rand(2,2))
Dict{ASCIIString,Any} with 1 entry:
  "Hi" => 2x2 Array{Float64,2}:

julia> pointer(p["Hi"])
Ptr{Float64} @0x00007fbccb82b760

julia> pc = copy(p)
Dict{ASCIIString,Any} with 1 entry:
  "Hi" => 2x2 Array{Float64,2}:

julia> pointer(pc["Hi"])
Ptr{Float64} @0x00007fbccb82b760

@timholy
Copy link
Member

timholy commented Sep 26, 2015

JuliaLang/julia#13323

@cdsousa
Copy link
Contributor Author

cdsousa commented Sep 26, 2015

There may be different causes for the issue at each method. For the similar method the problem is in the Image constructor (#356 (comment)) due to the unstability of ndims which suffers from an inference bug already reported: JuliaLang/julia#13082.

There is a workaround for that, and I should have already proposed a PR:
Replacing line https://github.com/timholy/Images.jl/blob/master/src/core.jl#L14
with

Image{T,N}(data::AbstractArray{T,N}, props::Dict) = Image{eltype(data),N,typeof(data)}(data,props)

will fix similar method (and probably other methods too).

cdsousa added a commit to cdsousa/Images.jl that referenced this issue Sep 26, 2015
timholy added a commit that referenced this issue Oct 15, 2015
Circumvent type-instability of deepcopy. Fixes #356
timholy added a commit that referenced this issue Nov 3, 2015
Tests that the fix in #393 covers #356
@timholy timholy mentioned this issue Nov 3, 2015
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 a pull request may close this issue.

2 participants