I’m a novice at writing high-performance code with Julia (1.11).
I have an unknown function from a user, and I want to check if it accepts any of several possible sets of keyword names. But for reasons unknown to me, hasmethod is very slow when applied to kwargs:
What exactly is going on under the hood that makes the second call so much slower (and the third call not much slower than that)? Should I avoid checking for keywords like this entirely if I need to write fast code, or is there a workaround?
2ns is currently around the benchmark results of integer addition, an indication that the actual call got compiled out of the benchmark loop. Use $ for runtime arguments in BenchmarkTools, it changes quite a bit:
I think that’s a realistic scenario if one wants to use hasmethod inside a function (with constant keyword names).
Apart from speed, I don’t think that using hasmethod is a good idea in this case. The reason is that it cannot deal with functions accepting a variable number of keyword arguments. Imagine the following pattern, which often I find using myself:
You’d need the function and argument types to be constant too, then the compiler is free to do the call at compile-time and be invalidated when method definitions change. Obviously that’s much more limited than a runtime call.