-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make derivative 5-8 times faster #3322
Conversation
Wow, this is a huge performance improvement! That is a smart idea, thanks Paul. Your PR looks good to go. I have only one thought: the old solution with |
@josdejong Good question! When Things could behave differently if
Old code would depend on whether constructed node appears anywhere in the input as a sub-expression. New code will always take derivative of it. |
Bulletproof way would be to add memoization to |
@josdejong PTAL Will post a new benchmark shortly, can't promise it will be as good as the previous one though. |
I've updated the benchmark and it made measurement error smaller (or whatever that Adding Let me know if you have any suggestions how to improve this PR. Numbers for the new benchmark:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the updates and for thinking this through.
I quite like your refactoring into a function isConst
. The logic makes sense and it makes the code indeed better readable and requires less context knowledge in all the places where it is used. Nice that this even improves an existing case :)
I think you don't need to worry about the performance of the latest version of your PR vs the first version. Over all, I see the performance of the derivative
now (still) is about 100x faster, which is just insane 🚀. Here the benchmark results on my machine:
// develop branch
ddf x 46.57 ops/sec ±0.63% (157 runs sampled)
df x 155 ops/sec ±0.86% (177 runs sampled)
// PR 3322
ddf x 4,997 ops/sec ±1.54% (192 runs sampled)
df x 16,256 ops/sec ±0.47% (191 runs sampled)
I made one inline comment about a comment 😜, can you have a look at that?
Dropped the TODO comment. |
Nevermind. It may have had something to do with the fact that |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks again👌 . Merging now.
Published now in |
derivative seems to spend most of its time inside _toString, because
constNodes[foo]
converts keys to strings.I'm not sure Set is really needed as a separate type, but because
_derivative
is atyped
function I had to introduce it.Benchmark before the change (Core i5 2017 macbook):
ddf x 23.77 ops/sec ±0.54% (43 runs sampled)
df x 79.58 ops/sec ±0.57% (69 runs sampled)
ddf x 23.62 ops/sec ±0.54% (43 runs sampled)
df x 78.86 ops/sec ±0.50% (74 runs sampled)
ddf x 23.73 ops/sec ±0.47% (43 runs sampled)
df x 79.73 ops/sec ±0.57% (69 runs sampled)
ddf x 23.85 ops/sec ±0.64% (44 runs sampled)
df x 80.17 ops/sec ±0.43% (69 runs sampled)
Benchmark with this PR (same old macbook):
ddf x 1,781 ops/sec ±14.63% (80 runs sampled)
df x 5,161 ops/sec ±30.53% (62 runs sampled)
ddf x 1,769 ops/sec ±17.68% (79 runs sampled)
df x 5,286 ops/sec ±30.00% (72 runs sampled)
ddf x 1,910 ops/sec ±4.58% (85 runs sampled)
df x 4,275 ops/sec ±41.16% (58 runs sampled)
ddf x 1,959 ops/sec ±4.91% (88 runs sampled)
df x 4,474 ops/sec ±37.39% (62 runs sampled)