Skip to content

Allocation regression: global const getfield allocates in some cases #50317

@NHDaly

Description

@NHDaly

Here's a small regression that is showing up pretty large in the RAI codebase (we have a global lock in our database pager that's way slower now).

Somehow accessing the fields of a global constant has become type unstable in some cases.

struct Wrapper
    lock::ReentrantLock
end

Base.lock(f::Function, m::Wrapper) = Base.lock(f, m.lock)
Base.lock(m::Wrapper) = Base.lock(m.lock)
Base.unlock(m::Wrapper) = Base.unlock(m.lock)

const MONITOR = Wrapper(ReentrantLock())

function foo()
    Base.@lock MONITOR begin
        return 2+2
    end
end
foo()
@time foo()  # 0 allocs on 1.8, 1 alloc on 1.9

The allocation only shows up when referencing the global const variable. It disappears if you pass it in as an argument:

function bar(x)
    Base.@lock x begin
        return 2+2
    end
end
bar(MONITOR)
@time bar(MONITOR) # 0 allocs on 1.8, 0 allocs on 1.9

This might be related to #50073, but i'm not sure.

Metadata

Metadata

Assignees

No one assigned

    Labels

    performanceMust go fasterregressionRegression in behavior compared to a previous versionregression 1.9Regression in the 1.9 release

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions