-
Notifications
You must be signed in to change notification settings - Fork 986
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
CommandHandler.write() is O(N^2) #709
Comments
Isn't that a duplicate of #693? Could you upgrade to Lettuce 5.0.2.RELEASE and retry? |
I still have this problem in 5.0.2.RELEASE. It's a different issue, but it's similar: in #693 there were unnecessary |
The check for |
The problem is not caused by the size of the batch. As you could see in the ticket's description, sending 40000 commands took 80 seconds, which is very slow - and the time clearly increased quadratically. I'm not saying we should get rid of the check, but having it shouldn't decrease client's performance.
The second solution seems to be the best for me. Could I create a PR with it? |
Duplicates can occur upon batch add, not so much when individual commands get added. I thought about changing |
Still, it's better to create a node on As you said - after moving the check to |
I was more heading towards using |
Lettuce now checks for command stack duplicates in CommandHandler.writeBatch(…) by using LinkedHashSet. Duplicates occur usually in batch submissions and the check in the single command path causes additional cost that isn't necessary in the majority of cases. Using LinkedHashSet as intermediate collection reduces contains cost from O(N^2) to constant time.
Lettuce now checks for command stack duplicates in CommandHandler.writeBatch(…) by using LinkedHashSet. Duplicates occur usually in batch submissions and the check in the single command path causes additional cost that isn't necessary in the majority of cases. Using LinkedHashSet as intermediate collection reduces contains cost from O(N^2) to constant time.
I pushed a change, and a new snapshot |
Works like a charm:
Thank you so much for fixing it! When can we expect version |
Cool, thanks for giving it a try. I don't know when 5.0.3 will be released. As an intermediate solution, feel free to use snapshots of 5.0.x. I don't have a release date yet, I expect some feedback from Spring 2.0 GA in the next weeks. |
Lettuce now checks for command stack duplicates in CommandHandler.writeBatch(…) by using LinkedHashSet. Duplicates occur usually in batch submissions and the check in the single command path causes additional cost that isn't necessary in the majority of cases. Using LinkedHashSet as intermediate collection reduces contains cost from O(N^2) to constant time.
Lettuce now checks for command stack duplicates in CommandHandler.writeBatch(…) by using LinkedHashSet. Duplicates occur usually in batch submissions and the check in the single command path causes additional cost that isn't necessary in the majority of cases. Using LinkedHashSet as intermediate collection reduces contains cost from O(N^2) to constant time.
I am using version 5.0.1.RELEASE.
It looks like
CommandHandler.write()
method is of O(N^2) complexity, where N is number of written commands. It's caused by this check:https://github.com/lettuce-io/lettuce-core/blob/efa80382fa28570cbaac44681a5dc47768291b92/src/main/java/io/lettuce/core/protocol/CommandHandler.java#L413
Usually, it won't be such a big problem as channel thread will decrease the size of the stack quickly enough. But for some reasons, we want to have only one thread, i.e. we want the main thread to also handle Lettuce's Netty connection. We know that doing so is dangerous, but we want to keep it this way. In such a case, writing large batches becomes really slow.
Let me show you a simple benchmark (I am aware of some simplifications, e.g.
EventLoopGroupProvider
not being shut down properly, but this code is just to demonstrate the issue):https://gist.github.com/gszpak/e9af789cc69718524b28d9ee9adfac16
The results of the benchmark are:
Is it possible to get rid of the
contains
check I mentioned or use more efficient data structures for command stack?Thank you!
The text was updated successfully, but these errors were encountered: