Welcome! Please see the About page for a little more info on how this works.

+1 vote
ago in Refs, agents, atoms by

Why clojure.core/vswap is a macro with tag hint instead of a function called on clojure.lang.Volatile similar to clojure.core/swap?

1 Answer

+1 vote
ago by

I don't know for sure, but this comment on the initial ticket seems plausible.

https://clojure.atlassian.net/browse/CLJ-1512?focusedCommentId=19049

the vswap! macro is probably for performance reasons (the main motivation of this code to begin with), to avoid using apply or unrolling tons of arities

ago by
edited ago by
thanks for the context!

at least i can see that volatile was added in the scope of transducer's use cases maybe that's why macro was chosen.

Still don't get why swap-similar function making direct call on clojure.lang.Volatile would have not acceptable cost comparing to macro
ago by
Calling through a Clojure function has a cost (resolve the var which involves going through a lock, invoke the function, etc) whereas the macro expands to the interop call directly.
ago by
with direct-linking enabled during compilation function will be called directly instead of resolving through a var , correct? (i am talking about direct function call , not passing function in HOF)
...