Skip to content

Unexpected Allocation for small union: Union{Nothing,Tuple{String}} #57157

Open
@NHDaly

Description

@NHDaly

The following code demonstrates a performance issue where a function that returns a Union{Nothing,Tuple{String}} heap-allocates the returned tuple:

julia> versioninfo()
Julia Version 1.12.0-DEV.1571
Commit 671cd5e1db7 (2024-11-07 02:50 UTC)
Build Info:
  Official https://julialang.org release
Platform Info:
  OS: macOS (arm64-apple-darwin22.4.0)
  CPU: 12 × Apple M2 Max
  WORD_SIZE: 64
  LLVM: libLLVM-18.1.7 (ORCJIT, apple-m2)
Threads: 1 default, 0 interactive, 1 GC (on 8 virtual cores)
Environment:
  JULIA_SSL_CA_ROOTS_PATH =

julia> using BenchmarkTools

julia> zot(s) = s=="" ? nothing : (s,)
zot (generic function with 2 methods)

julia> foo(s) = s=="" ? nothing : s
foo (generic function with 2 methods)

julia> @btime zot($("a"))
  6.750 ns (1 allocation: 16 bytes)
("a",)

julia> @btime foo($("a"))
  4.625 ns (0 allocations: 0 bytes)
"a"

julia> @btime zot($(""))
  2.250 ns (0 allocations: 0 bytes)

And this is unique to Tuple{String}. With Tuple{Int}, we don't see it:

julia> bar(x) = x==0 ? nothing : (x,)
bar (generic function with 2 methods)

julia> @btime bar(10)
  1.125 ns (0 allocations: 0 bytes)
(10,)

Also, I see this on 1.12, 1.10, as well as all the way back to 1.0 on the various versions I tested. So not a recent regression.

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