Pangoraw / Coil.jl

꩜ Lift Julia array operations to MLIR dialects and run using IREE.
MIT License
41 stars 0 forks source link

faster cvcall macro #2

Closed vtjnash closed 1 year ago

vtjnash commented 1 year ago

This probably does not intend to be re-running dlopen on every call, since that is generally a bad idea. Instead, you can memoize it with a hidden Ref object, as roughly seen here: https://github.com/JuliaPy/PyCall.jl/pull/84/files (oddly, this feature was removed later in https://github.com/JuliaPy/PyCall.jl/pull/84/files)

https://github.com/Pangoraw/Coil.jl/blob/389237f18ee056a3c73b98ccc7d468260ff17edb/src/utils.jl#L21-L22

Even that was a bit overcomplicated with the use of globals, since you just need a local Ref object:

     z = Ref(C_NULL)
     quote
         let z = $z[]
             if z == C_NULL
                z = $z[] = dlvsym($library::Ptr{Void}, $(esc(func)), $(esc(version)))
             end
             z
         end
     end

(I don't define func, version, or how to get library here, but the first 2 come from the user, and the latter can be computed and stored globally at some earlier time or in some similar way)

Pangoraw commented 1 year ago

Thanks for the suggestion! I implemented symbol memoization in https://github.com/Pangoraw/Coil.jl/commit/f0204ad2e84dffddb06f9920e3f75d8518e6c696.

vtjnash commented 1 year ago

Nice! One further small optimization to consider: you may want to only compute the handle when you need it for dlsym, and not earlier

Pangoraw commented 1 year ago

you may want to only compute the handle when you need it for dlsym, and not earlier

Good catch, thanks!