Skip to content

Commit

Permalink
Avoid extra byte copies in Pub/Sub when Msg contains DirectByteBuffer or
Browse files Browse the repository at this point in the history
HeapByteBuffers with position>0 or limit!=capacity.
  • Loading branch information
markif committed Feb 8, 2016
1 parent 4b7ad90 commit 523dae3
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 7 deletions.
5 changes: 3 additions & 2 deletions src/main/java/zmq/Mtrie.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package zmq;

import java.nio.ByteBuffer;
import java.util.HashSet;
import java.util.Set;

Expand Down Expand Up @@ -350,7 +351,7 @@ else if (c == min + count - 1) {
}

// Signal all the matching pipes.
public void match(byte[] data, int size, IMtrieHandler func, Object arg)
public void match(ByteBuffer data, int size, IMtrieHandler func, Object arg)
{
Mtrie current = this;
int idx = 0;
Expand All @@ -373,7 +374,7 @@ public void match(byte[] data, int size, IMtrieHandler func, Object arg)
break;
}

byte c = data[idx];
byte c = data.get(idx);
// If there's one subnode (optimisation).
if (current.count == 1) {
if (c != current.min) {
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/zmq/Trie.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

package zmq;

import java.nio.ByteBuffer;

public class Trie
{
private int refcnt;
Expand Down Expand Up @@ -213,7 +215,7 @@ else if (c == min + count - 1) {
}

// Check whether particular key is in the trie.
public boolean check(byte[] data)
public boolean check(ByteBuffer data)
{
// This function is on critical path. It deliberately doesn't use
// recursion to get a bit better performance.
Expand All @@ -226,13 +228,13 @@ public boolean check(byte[] data)
}

// We've checked all the data and haven't found matching subscription.
if (data.length == start) {
if (data.remaining() == start) {
return false;
}

// If there's no corresponding slot for the first character
// of the prefix, the message does not match.
byte c = data[start];
byte c = data.get(start);
if (c < current.min || c >= current.min + current.count) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/zmq/XPub.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ protected boolean xsend(Msg msg)

// For the first part of multi-part message, find the matching pipes.
if (!more) {
subscriptions.match(msg.data(), msg.size(),
subscriptions.match(msg.buf(), msg.size(),
markAsMatching, this);
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/zmq/XSub.java
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,6 @@ protected boolean xhasIn()

private boolean match(Msg msg)
{
return subscriptions.check(msg.data());
return subscriptions.check(msg.buf());
}
}

0 comments on commit 523dae3

Please sign in to comment.