Repository: JuliaLang/julia
File: base/timing.jl (Julia)

Code before:
    Base.gc_bytes(b0)
    @noinline f(args...)
    Base.gc_bytes(b1)
    return b1[] - b0[]
end
only(methods(allocated)).called = 0xff

@constprop :none function allocations(f, args::Vararg{Any,N}) where {N}
    stats = Base.gc_num()
    @noinline f(args...)
    diff = Base.GC_Diff(Base.gc_num(), stats)
    return Base.gc_alloc_count(diff)
end
only(methods(allocations)).called = 0xff

function is_simply_call(@nospecialize ex)
    is_simple_atom(a) = a isa QuoteNode || a isa Symbol || !isa_ast_node(a)
    Meta.isexpr(ex, :call) || return false
    for a in ex.args
        is_simple_atom(a) && continue
        Meta.isexpr(a, :..., 1) && is_simple_atom(a.args[1]) && continue
        return false
    end
    # Ensure Expr(:call, .+, ...) get wrapped
    if ex.args[1] isa Symbol
        sa = String(ex.args[1])
        startswith(sa, ".") &&
            !endswith(sa, ".") &&
            isoperator(Symbol(sa[2:end])) &&
            return false
    end
    return true
end

function _gen_allocation_measurer(ex, fname::Symbol)
    if isexpr(ex, :call)
        if !is_simply_call(ex)
            ex = :((() -> $ex)())
        end
        pushfirst!(ex.args, GlobalRef(Base, fname))
        return quote
            Experimental.@force_compile
            $(esc(ex))
        end
    elseif fname === :allocated
        # v1.11-compatible implementation
        return quote
            Experimental.@force_compile
            local b0 = Ref{Int64}(0)
            local b1 = Ref{Int64}(0)
            gc_bytes(b0)

Review comment (suggestion):
```suggestion
        sa = String(ex.args[1]::Symbol)
```

Code after:
    Base.gc_bytes(b0)
    @noinline f(args...)
    Base.gc_bytes(b1)
    return b1[] - b0[]
end
only(methods(allocated)).called = 0xff

@constprop :none function allocations(f, args::Vararg{Any,N}) where {N}
    stats = Base.gc_num()
    @noinline f(args...)
    diff = Base.GC_Diff(Base.gc_num(), stats)
    return Base.gc_alloc_count(diff)
end
only(methods(allocations)).called = 0xff

function is_simply_call(@nospecialize ex)
    is_simple_atom(a) = a isa QuoteNode || a isa Symbol || !isa_ast_node(a)
    Meta.isexpr(ex, :call) || return false
    for a in ex.args
        is_simple_atom(a) && continue
        Meta.isexpr(a, :..., 1) && is_simple_atom(a.args[1]) && continue
        return false
    end
    # Ensure Expr(:call, .+, ...) get wrapped
    if ex.args[1] isa Symbol
        sa = String(ex.args[1]::Symbol)
        startswith(sa, ".") &&
            !endswith(sa, ".") &&
            isoperator(Symbol(sa[2:end])) &&
            return false
    end
    return true
end

function _gen_allocation_measurer(ex, fname::Symbol)
    if isexpr(ex, :call)
        if !is_simply_call(ex)
            ex = :((() -> $ex)())
        end
        pushfirst!(ex.args, GlobalRef(Base, fname))
        return quote
            Experimental.@force_compile
            $(esc(ex))
        end
    elseif fname === :allocated
        # v1.11-compatible implementation
        return quote
            Experimental.@force_compile
            local b0 = Ref{Int64}(0)
            local b1 = Ref{Int64}(0)
            gc_bytes(b0)