Skip to content

"try foo(); finally; end" should be optimized to "foo()" #55834

@stevengj

Description

@stevengj

I notice a missing optimization: try foo(); finally; end is not optimized to foo(). For example, look at @code_native foo(3) for foo(x) = try; return x+1; finally; end — there is a whole bunch of overhead (_ijl_excstack_state, _ijl_enter_handler, _sigsetjmp, …) compared to x+1.

I noticed this because (related to #55754) I was looking at print(io, x) performance on IOBuffers. The fallback for print(io, x) is defined as:

function print(io::IO, x)
    lock(io)
    try
        show(io, x)
    finally
        unlock(io)
    end
    return nothing
end

but the default lock(io) and unlock(io) methods do nothing. So, on an IOBuffer at least, this whole function should be optimized to just (show(io, x); nothing), but it isn't.

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