Repository navigation
Conversation
This is an experimental commit. Likely it causes a memory leak. However, ZUNIONSTORE benefits from performance improvements. Initial tests on ~70k keys went from 14s to ~200ms.
Attempt to document some of my thinking in comments. I'm sure this should be fixed before the pull request goes through.
|
Our use case is centered around unique keys, I misunderstood the use case for listing the same key multiple times. I'll fix that and reissue the pull request. |
When passing in duplicate keys to ZUNIONSTORE, it is expected their aggregates count as many times as they are present.
zunionCloneAndFree is a new function which does as it says. It will clone a ZSET and free the old copy. This is good for resorting a ZSET in case it's values were directly modified.
|
Some quick notes:
Add some tests to |
|
Thanks for your feedback.
I'll add the new test. Time to dust off my TCL. |
|
Yeah, you're right about my mistaking replacing for updating aggregate values. The existing Redis zunion tests are actually enforcing order checking. I think the test cases just don't have a case where the union is a different order than the original values after multiple updates. Maybe test with negative weights or negative scores on some elements so the order of the union isn't the order of the traversal to create it? I see what you mean about Thanks for getting all this sorted! |
|
I'll run this on various bits of live data we have and paste the performance differences I encounter here. If this increase in speed can be replicated generically, I need it in my life. 😄 |
|
Thanks @michael-grunder! Waiting for the benchmarks :-) 👍 |
|
p.s. sorry reading the full thread only now, I'll start reviewing the patch and comments more closely to check if I can find some semantical issue here. 14 s -> 200 ms is a huge step forward... even if it happens only for special use cases. |
|
Update. I'm trying to rewrite the code in a simpler way, but guaranteeing that the aggregation is performed with the same logic and cache-friendly way. This is the script I'm using to test: The problem is, with the above script, I'm not able to replicate the big speed difference between the two implementations. Please could @kjmph provide some details about the key / zset layout so that I can use it as a testbed to check if my new implementation performs as fast as needed? |
|
Nevermind... just adding more keys trashes even more the access schema of the original implementation, and now I can see a very big difference in performances. |
|
Hey guys, I ran some benchmarks from production data we're using of three different types, with various set counts. The three types are as follows:
My results are as follows: Terrible data: Better data: Good data: I too am seeing a big increase in performance, and I also verified that the input and output cardinality was unchanged with the old and new code. I didn't verify that the actual output |
|
Oh man... this is epic! |
|
Great! I'm working at the new implementation, I hope Michael could re-run the tests against mine as well so that we are sure we are not regressing compared to the original implementation provided by the OP. |
|
Of course, I'll run it when it's up. 😺 |
The user @kjmph provided excellent ideas to improve speed of ZUNIONSTORE (in certain cases by many order of magnitude), together with an implementation of the ideas. While the ideas were sounding, the implementation could be improved both in terms of speed and clearness, so that's my attempt at reimplementing the speedup proposed, trying to improve by directly using just a dictionary with an embedded score inside, and reusing the single-pass aggregate + order-later approach. Note that you can't apply this commit without applying the previous commit in this branch that adds a double in the dictEntry value union. Issue #1786.
|
Hey, I just tried to implement the same ideas as @kjmph in a more idiomatic way. I believe that since I touched more internals, my implementation should also be faster than the proposed one. See commits fe85d06 and 932cc3e. Waiting for the new @michael-grunder benchmarks before merging. |
|
Hey, thanks for the interest! I was busy working this morning, sorry for the delay. I'm glad we can push forward performance of ZUNIONSTORE! I'll run our internal benchmark through this as well.. Give me a second. |
|
Thank you @kjmph ! |
|
We are operating on 73690 zsets, with average cardinality ~250. -- unpatched --this pull request -- antirez's mod It looks great to me! |
|
Awesome, thank you. I've to think a bit about if merging this into 2.8 or not... It's tempting because the slowdown in certain cases is bug-equivalent given the amount of unneeded work performed. |
|
If it helps you at all, I merged the original mod into 2.8 for local usage. It was a bit fuzzy to apply the patch, but it was clean. It works well for us.. I can try merging your patch, see if that works locally. |
|
Hey guys, Terrible data: Better data: Good data: So I'm not seeing an improvement over @kjmph's mod, but that being said it's a benchmark so it could simply have to do with the data I'm using itself. I'm headed out the door but I'll re-run these in a more formalized way (it's a bit hackey right now) and post an update. As far as I'm concerned the improvement is massive either way. Cheers! |
|
Since I have your ear on the subject. Performance of ~8s of my mod still wasn't good enough. I decreased it to roughly ~200ms. I would like to air it, maybe this isn't the right forum. I apologize if I am out of turn. However, I preloaded the full set of zsets(keys) into a compact multi-dimensional C array of doubles. Then, the ZUNIONSTORE is replaced with this custom command that takes the intersection between a provided set of key names and the preloaded set of keys and writes the aggregate to a destination key. It performs very well. Although, we only care about sums of weight 1.0, so I didn't code any other aggregates or weights into it. It should be easy to extend though. Is this a paradigm that is interesting to Redis? |
|
Also +1 for merging into 2.8. :) |
|
@kjmph the use case sounds interesting, but probably not general enough currently, however assuming at some point we add a matrix data type... this may be back into the mix of stuff that are possible using matrix and operations defined on the new data type. A field to explore... probably around 3.2, which is supposed to be the "back to data structures and API" fiesta ;-) |
|
@michael-grunder thanks! A few of this columns actually look like a regression speed when using my updated code compared to @kjmph's. It is strange since my benchmarks and @kjmph original use case is faster with the new code that should do a bit less work, however I've the feeling that this may have something to do with dictExpand() calls I added... that trash locality, so when the access is repeated to the same elements, it may be a win to actually have a rehashing in progress instead to start with a huge table... Please if you find some more time, would be awesome to have the benchmarks running with the dictExpand() calls commented, however I'll also try to work with parameters similar to yours to see if I'll be able to find tests where the original code was faster, and to check if actually my hypothesis has something to do with that. Thanks again! |
|
@antirez I'll run a-b comparisons with the lines commented as well as against the original pull itself. In addition, I'll formalize the benchmark script just a bit more and post it so you can see if there is something about the test itself that is broken (don't see how, but it's always possible). I would even upload the rdb but it's client production data and I would likely have lawyers knocking on my door by morning. :P |
|
Here are my updated benchmarks. The The code I used to benchmark is here: Terrible data: Better data: Good data: |
|
Hey everyone, I was just thinking about this again today. It looks like the test with Potential idea: maybe generate sets in an independent script so we could test with Any other ideas? We're looking at micro-optimizations at this point. The overall approach is a huge improvement from 40s down to under 1s for results. |
|
+1 👍 |
|
Thank for bringing this up, I'm going to merge my version in this week since if there is not a huge bias towards the other version, mine is composed of simpler code I'm more confident backporting to 2.8. And judging from the other comments, there is no big bias to favor the original implementation in this PR. Thanks. |
|
Shameless bump to see if we can get this into 2.8.14 :) |
|
That would be amazing 👍 |
The user @kjmph provided excellent ideas to improve speed of ZUNIONSTORE (in certain cases by many order of magnitude), together with an implementation of the ideas. While the ideas were sounding, the implementation could be improved both in terms of speed and clearness, so that's my attempt at reimplementing the speedup proposed, trying to improve by directly using just a dictionary with an embedded score inside, and reusing the single-pass aggregate + order-later approach. Note that you can't apply this commit without applying the previous commit in this branch that adds a double in the dictEntry value union. Issue #1786.
This is not a regression but issue #1786 showed the need for this test.
The user @kjmph provided excellent ideas to improve speed of ZUNIONSTORE (in certain cases by many order of magnitude), together with an implementation of the ideas. While the ideas were sounding, the implementation could be improved both in terms of speed and clearness, so that's my attempt at reimplementing the speedup proposed, trying to improve by directly using just a dictionary with an embedded score inside, and reusing the single-pass aggregate + order-later approach. Note that you can't apply this commit without applying the previous commit in this branch that adds a double in the dictEntry value union. Issue #1786.
This is not a regression but issue #1786 showed the need for this test.
|
Just added a test for the issue found initially, of Redis test not testing order after an union operation, and merged into both unstable and 2.8. Thanks everybody for the collaboration with this issue! |
The user @kjmph provided excellent ideas to improve speed of ZUNIONSTORE (in certain cases by many order of magnitude), together with an implementation of the ideas. While the ideas were sounding, the implementation could be improved both in terms of speed and clearness, so that's my attempt at reimplementing the speedup proposed, trying to improve by directly using just a dictionary with an embedded score inside, and reusing the single-pass aggregate + order-later approach. Note that you can't apply this commit without applying the previous commit in this branch that adds a double in the dictEntry value union. Issue #1786.
This is not a regression but issue #1786 showed the need for this test.
When attempting to union many keys, ZUNIONSTORE was taking 14s. With these modifications, it is down to ~200ms. However, I guarantee I don't understand how refcnts are used in Redis. The reason I need refcnts is because when aggregating, I use the destination ZSET as the accumulated aggregate. Then, because ZSETs sort on insert, I need to sort the destination. I did so by creating a new ZSET and inserting all the values/scores into it. The method described here is more cache friendly by iterating through each key, rather than scanning forward through all keys for the aggregate. It also visits each value once, rather than multiple times.