Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reduce latency of JLCall after loading other packages #3

Merged
merged 1 commit into from
Dec 21, 2024

Conversation

songjhaha
Copy link
Member

function JLCall could get about 50s latency after loading a lot of big packages in julia 1.9

use Base.inferencebarrier to reduce this latency

@songjhaha
Copy link
Member Author

here is the code about the performance:

using TyJuliaCAPI

function bench(n)
    fProxy = TyJuliaCAPI.JV_ALLOC(+)
    arg1Proxy = TyJuliaCAPI.JV_ALLOC(1.0)
    arg2Proxy = TyJuliaCAPI.JV_ALLOC(2.3)
    out = [JV()]
    args = [arg1Proxy, arg2Proxy]
    kwargs = Ptr{TyTuple{JSym,JV}}(C_NULL)
    Base.GC.enable(false)

    t1 = time_ns()
    GC.@preserve out args kwargs begin
        for i in 1:n
            ret = TyJuliaCAPI.JLCall(pointer(out), fProxy, TyJuliaCAPI.TyList(2, pointer(args)), TyJuliaCAPI.TyList(0, kwargs))
            TyJuliaCAPI.JV_DEALLOC(out[1])
        end
    end
    t2 = time_ns()
    Base.GC.enable(true)
    TyJuliaCAPI.JV_DEALLOC(fProxy)
    TyJuliaCAPI.JV_DEALLOC(arg1Proxy)
    TyJuliaCAPI.JV_DEALLOC(arg2Proxy)

    println("Time cost every call in $(n) loops: ", (t2 - t1) / n, " ns")
end

println("call first time to compile")
bench(1)
# v0.5.8 : cost per call: 20 ~ 30+ us
# this PR: cost per call: 20+ us

println("call second time to benchmark")
bench(1_000_000)
# v0.5.8 : cost per call: 334.5068 ns
# this PR: cost per call: 328.1931 ns

@songjhaha songjhaha merged commit 715ac91 into main Dec 21, 2024
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant