Skip to content

Unexpected allocations caused by unused typevars #29393

@rdeits

Description

@rdeits

Under some circumstances, having an unused typevar in a method definition causes the method to unexpectedly allocate and perform poorly. For example:

julia> function foo(y::T) where {N, T}
         1.0 * y
       end
foo (generic function with 1 method)

julia> using BenchmarkTools
[ Info: Recompiling stale cache file /home/BOSDYN/rdeits/.julia/compiled/v1.0/BenchmarkTools/ZXPQo.ji for BenchmarkTools [6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf]

julia> @btime foo($(1))
  13.938 ns (1 allocation: 16 bytes)
1.0

where the N parameter is unused. Removing that parameter fixes the problem:

julia> function foo(y::T) where {T}
         1.0 * y
       end
foo (generic function with 1 method)

julia> @btime foo($(1))
  1.239 ns (0 allocations: 0 bytes)
1.0

Is this and expected performance issue? I've observed it with Julia 1.0 and the latest nightly build.

julia> versioninfo()
Julia Version 1.0.1-pre.171
Commit 398d87829d (2018-09-26 22:52 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Xeon(R) CPU E3-1535M v6 @ 3.10GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.0 (ORCJIT, skylake)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions