Skip to content
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

Avoid Unnecessary Autoboxing & Allocations #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ abstract class BaseStoreOperationImpl extends OperationImpl {
private static final int OVERHEAD = 32;
private static final OperationStatus STORED = new OperationStatus(true,
"STORED", StatusCode.SUCCESS);
private static final String ZERO = "0";
protected final String type;
protected final String key;
protected final int flags;
Expand All @@ -66,9 +67,11 @@ assert getState() == OperationState.READING : "Read ``" + line

@Override
public void initialize() {
byte[] keyBytes = KeyUtil.getKeyBytes(key);
ByteBuffer bb = ByteBuffer.allocate(data.length
+ KeyUtil.getKeyBytes(key).length + OVERHEAD);
setArguments(bb, type, key, flags, exp, data.length);
+ keyBytes.length + OVERHEAD);
setArgumentsWithKey(bb, type, keyBytes, flags == 0 ? ZERO : flags,
exp == 0 ? ZERO : exp, data.length);
assert bb.remaining() >= data.length + 2 : "Not enough room in buffer,"
+ " need another " + (2 + data.length - bb.remaining());
bb.put(data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class CASOperationImpl extends OperationImpl implements CASOperation {
false, "NOT_FOUND", CASResponse.NOT_FOUND, StatusCode.ERR_NOT_FOUND);
private static final OperationStatus EXISTS = new CASOperationStatus(false,
"EXISTS", CASResponse.EXISTS, StatusCode.ERR_EXISTS);
private static final String ZERO = "0";

private final String key;
private final long casValue;
Expand Down Expand Up @@ -76,9 +77,11 @@ assert getState() == OperationState.READING : "Read ``" + line

@Override
public void initialize() {
byte[] keyBytes = KeyUtil.getKeyBytes(key);
ByteBuffer bb = ByteBuffer.allocate(data.length
+ KeyUtil.getKeyBytes(key).length + OVERHEAD);
setArguments(bb, "cas", key, flags, exp, data.length, casValue);
+ keyBytes.length + OVERHEAD);
setArgumentsWithKey(bb, "cas", keyBytes, flags == 0 ? ZERO : flags,
exp == 0 ? ZERO : exp, data.length, casValue);
assert bb.remaining() >= data.length + 2 : "Not enough room in buffer,"
+ " need another " + (2 + data.length - bb.remaining());
bb.put(data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ public void handleLine(String line) {

@Override
public void initialize() {
ByteBuffer b = ByteBuffer.allocate(KeyUtil.getKeyBytes(key).length
+ OVERHEAD);
setArguments(b, "delete", key);
byte[] keyBytes = KeyUtil.getKeyBytes(key);
ByteBuffer b = ByteBuffer.allocate(keyBytes.length + OVERHEAD);
setArgumentsWithKey(b, "delete", keyBytes);
b.flip();
setBuffer(b);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,10 @@ public void handleLine(String line) {

@Override
public void initialize() {
int size = KeyUtil.getKeyBytes(key).length + OVERHEAD;
byte[] keyBytes = KeyUtil.getKeyBytes(key);
int size = keyBytes.length + OVERHEAD;
ByteBuffer b = ByteBuffer.allocate(size);
setArguments(b, mutator.name(), key, amount);
setArgumentsWithKey(b, mutator.name(), keyBytes, amount);
b.flip();
setBuffer(b);
}
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/net/spy/memcached/protocol/ascii/OperationImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,19 @@ protected final void setArguments(ByteBuffer bb, Object... args) {
bb.put(CRLF);
}

void setArgumentsWithKey(ByteBuffer bb, String type, byte[] key,
Object... args)
{
bb.put(KeyUtil.getKeyBytes(type));
bb.put((byte) ' ');
bb.put(key);
for (Object o : args) {
bb.put((byte) ' ');
bb.put(KeyUtil.getKeyBytes(String.valueOf(o)));
}
bb.put(CRLF);
}

OperationErrorType classifyError(String line) {
OperationErrorType rv = null;
if (line.startsWith("ERROR")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ public void handleLine(String line) {

@Override
public void initialize() {
ByteBuffer b = ByteBuffer.allocate(KeyUtil.getKeyBytes(key).length
+ OVERHEAD);
setArguments(b, CMD, key, cas);
byte[] keyBytes = KeyUtil.getKeyBytes(key);
ByteBuffer b = ByteBuffer.allocate(keyBytes.length + OVERHEAD);
setArgumentsWithKey(b, CMD, keyBytes, cas);
b.flip();
setBuffer(b);
}
Expand Down