-
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
DATAREDIS-1212 Avoid allocation when accessing DefaultMessage channel and body #559
DATAREDIS-1212 Avoid allocation when accessing DefaultMessage channel and body #559
Conversation
@theigl Please sign the Contributor License Agreement! Click here to manually synchronize the status of this Pull Request. See the FAQ for frequently asked questions. |
@theigl Thank you for signing the Contributor License Agreement! |
@@ -826,7 +826,7 @@ public void onMessage(Message message, @Nullable byte[] pattern) { | |||
|
|||
private boolean isKeyExpirationMessage(Message message) { | |||
|
|||
if (message == null || message.getChannel() == null || message.getBody() == null) { | |||
if (message == null || !message.hasChannel() || !message.hasBody()) { |
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.
Note: The new methods hasChannel
and hasBody
also check if the underlying byte arrays are empty, so this slightly changes the behavior of these conditionals.
66338d6
to
247424a
Compare
@mp911de: My related PR for Spring Session was just approved. Could you give me some feedback on the changes proposed in this PR? If this was merged, filtering incoming messages in Spring Session would be completely allocation free. |
Avoiding allocations is a fine and welcome enhancement. I think we can optimize for the empty case by having an empty array constant. Introducing additional methods on the interface would start cluttering the interface and we want to avoid such a development. That being said, let's change this pull request to optimize for empty byte arrays first and try removing the |
@mp911de: Thanks for your feedback!
I'm not sure I can follow you. How can we compare against the empty array constant without calling
I agree. It didn't feel right to add these methods, especially the one referencing |
I think I wasn't really clear where I wanted this PR to go. Introducing constants for empty byte arrays is a tiny change that we can introduce without exposing additional methods. My aim is to see how much we gain from eliminating |
@mp911de: Thanks for clarifying this! We can definitely remove the null checks. This will reduce allocations for everyone using The problem is that Spring Session directly uses
There really isn't much that can be done about these two allocations. Since I know that the message channel and body are never changed in my application, I'll override my Redis I'll adjust this PR to simply remove the null-checks in |
Sounds good. Let us know whether there's a need to make extension points protected/public so you can easier hook into customizations. |
a9de7cc
to
c837f18
Compare
@mp911de: I adjusted the PR to only remove the redundant null-checks in
I'm using Redisson where this is relatively easy to achieve. Supplying your own It would be great to have some kind of pluggable |
Remove overly pessimistic null checks. Use constant when channel/body are empty to avoid allocations. Remove superfluous final keywords. Original pull request: #559.
Remove overly pessimistic null checks. Use constant when channel/body are empty to avoid allocations. Remove superfluous final keywords. Original pull request: #559.
Remove overly pessimistic null checks. Use constant when channel/body are empty to avoid allocations. Remove superfluous final keywords. Original pull request: #559.
Thank you for your contribution. That's merged, polished, and backported now. |
This PR avoids allocating new byte arrays when accessing
DefaultMessage.getChannel
andDefaultMessage.getBody
.DefaultMessage.channelStartsWith
andDefaultMessage.bodyStartsWith
are added so Spring Session can process messages without allocations:https://github.com/spring-projects/spring-session/blob/0f3ea33b50678a764a50f7851b86fa45a99d2c85/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/RedisIndexedSessionRepository.java#L503
See https://jira.spring.io/browse/DATAREDIS-1212