Skip to content

Fix s/fn performance issues in Clojure - #430

Merged
frenchy64 merged 2 commits into
plumatic:masterfrom
frenchy64:sfn-no-wrapper
Nov 9, 2021
Merged

Fix s/fn performance issues in Clojure#430
frenchy64 merged 2 commits into
plumatic:masterfrom
frenchy64:sfn-no-wrapper

Conversation

@frenchy64

Copy link
Copy Markdown
Contributor

While trying to implement s/defprotocol, I needed the following invariant to hold in Clojure to access __methodImplCache:

(let [f (s/fn this [] this)]
  (identical? f (f)))

The fix I chose was to use ^{:schema schema} (fn ...) instead of (with-meta (fn ...) {:schema schema}). I believe this also fixed all the documented performance caveats for s/fn in Clojure as it means wrappers are eliminated.

The perf characteristics for CLJS seem unchanged (still uses the wrapper). I worked around CLJS-968 by placing the function evaluation in non-tail position. I decided working around this issue was better than using macros/if-cljs (which I treat as a last resort).

I also moved a test, please ignore whitespace changes when reviewing.

Comment thread src/cljc/schema/core.cljc

@w01fe w01fe left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice, thanks!

@frenchy64

Copy link
Copy Markdown
Contributor Author

There was an oversight in lein all that meant the latest cljs was never tested. I needed to drop Clojure 1.7 from the test suite because CLJS doesn't work with it (but we only support Clojure 1.8 and later anyway).

@frenchy64
frenchy64 merged commit ac25cad into plumatic:master Nov 9, 2021
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.

2 participants