Benefits to partial functions.

161 views
Skip to first unread message

david hoyt

unread,
Oct 20, 2019, 4:25:55 PM10/20/19
to Clojure
Is there a performance benefit (or detriment) to creating partial functions? Other than the potential reduction of complexity, is there a difference between using partial & comp and defining new functions?

John D. Hume

unread,
Oct 20, 2019, 5:55:19 PM10/20/19
to clo...@googlegroups.com
It's been a long time since I looked at this, but as of a few years ago, the biggest noticeable performance detriment of comp or partial was likely to come if you pass enough args to hit a "& args" overload, which requires creating a fresh object array at each call, when the underlying function being called has a non-"& args" arity.

On one application I worked on, we preferred defining #(anonymous ...) functions to some uses of higher order functions to avoid excessive garbage-creation in some hot spots.

This assumes you don't need primitive type-hint-level optimization.

I wouldn't recommend sacrificing any clarity unless you've profiled and found a real issue.

definition of partial:

definition of comp:
On Sun, Oct 20, 2019, 3:25 PM david hoyt <davidp...@gmail.com> wrote:
Is there a performance benefit (or detriment) to creating partial functions? Other than the potential reduction of complexity, is there a difference between using partial & comp and defining new functions?

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/clojure/146f2a73-9887-4eba-89c6-815eed6841b1%40googlegroups.com.

Alex Engelberg

unread,
Oct 21, 2019, 2:23:30 AM10/21/19
to clo...@googlegroups.com
All functions returned by a given arity of partial or comp are of the same Java class under the hood, whereas defining an anonymous function always generates a new class, and I've heard that fewer classes can result in better performance from a JIT perspective. But I'm just speculating and haven't actually run the benchmarks. So, [citation needed].

From a more practical perspective: I always prefer to give my anonymous functions names (e.g. `(fn foo [x])`, not `(fn [x])`), so that the stack traces are a bit more self-explanatory, but partial and comp don't let you do that. Although sometimes the added readability of a higher-order function outweighs that concern.

b w

unread,
Oct 22, 2019, 5:45:21 PM10/22/19
to clo...@googlegroups.com
When I was putting together lessons to teach Clojure, I was analyzing texts from Gutenberg and doing things like counting the number of words, and the frequency and distribution of words, and things like that, and "partial" was noticeably slower. FWIW.

Reply all
Reply to author
Forward
0 new messages