-
Notifications
You must be signed in to change notification settings - Fork 183
Optimize header reading #1449
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
Merged
Merged
Optimize header reading #1449
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0bf8ffb
Header Optimization
scottf 7eaa83a
Header Optimization
scottf ecced55
Header Optimization
scottf dde54f5
added tests and more known keys
scottf e29ad99
added tests and more known keys
scottf 0d83f57
added tests and more known keys
scottf 8ad6422
made constants final
scottf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,7 +30,7 @@ public IncomingHeadersProcessor(byte[] serialized) { | |
throw new IllegalArgumentException(SERIALIZED_HEADER_CANNOT_BE_NULL_OR_EMPTY); | ||
} | ||
|
||
// is tis the correct version | ||
// is this the correct version | ||
for (int x = 0; x < HEADER_VERSION_BYTES_LEN; x++) { | ||
if (serialized[x] != HEADER_VERSION_BYTES[x]) { | ||
throw new IllegalArgumentException(INVALID_HEADER_VERSION); | ||
|
@@ -42,17 +42,15 @@ public IncomingHeadersProcessor(byte[] serialized) { | |
Token terminus = new Token(serialized, serializedLength, serializedLength - 2, TokenType.CRLF); | ||
Token token = new Token(serialized, serializedLength, HEADER_VERSION_BYTES_LEN, null); | ||
|
||
boolean hadStatus = false; | ||
if (token.isType(TokenType.SPACE)) { | ||
token = initStatus(serialized, serializedLength, token); | ||
if (token.samePoint(terminus)) { | ||
return; // status only | ||
} | ||
hadStatus = true; | ||
} | ||
|
||
if (token.isType(TokenType.CRLF)) { | ||
initHeader(serialized, serializedLength, token, hadStatus); | ||
initHeader(serialized, serializedLength, token); | ||
} | ||
else { | ||
throw new IllegalArgumentException(INVALID_HEADER_COMPOSITION); | ||
|
@@ -71,7 +69,7 @@ public Status getStatus() { | |
return inlineStatus; | ||
} | ||
|
||
private void initHeader(byte[] serialized, int len, Token tCrlf, boolean hadStatus) { | ||
private void initHeader(byte[] serialized, int len, Token tCrlf) { | ||
// REGULAR HEADER | ||
Token peek = new Token(serialized, len, tCrlf, null); | ||
while (peek.isType(TokenType.TEXT)) { | ||
|
@@ -90,7 +88,7 @@ private void initHeader(byte[] serialized, int len, Token tCrlf, boolean hadStat | |
if (headers == null) { | ||
headers = new Headers(); | ||
} | ||
headers.add(tKey.getValue(), tVal.getValue()); | ||
headers.add(tKey.getValueCheckKnownKeys(), tVal.getValue()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use the new function to get the key, checking against known keys. |
||
peek = new Token(serialized, len, tCrlf, null); | ||
} | ||
peek.mustBe(TokenType.CRLF); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,12 @@ | |
|
||
package io.nats.client.support; | ||
|
||
import org.jspecify.annotations.NonNull; | ||
import org.jspecify.annotations.Nullable; | ||
|
||
import static io.nats.client.support.NatsConstants.*; | ||
import static io.nats.client.support.NatsJetStreamConstants.*; | ||
import static io.nats.client.support.Status.*; | ||
import static java.nio.charset.StandardCharsets.US_ASCII; | ||
|
||
public class Token { | ||
|
@@ -22,6 +27,7 @@ public class Token { | |
private final int start; | ||
private int end; | ||
private boolean hasValue; | ||
private final int valueLength; | ||
|
||
public Token(byte[] serialized, int len, Token prev, TokenType required) { | ||
this(serialized, len, prev.end + (prev.type == TokenType.KEY ? 2 : 1), required); | ||
|
@@ -48,11 +54,14 @@ public Token(byte[] serialized, int len, int cur, TokenType required) { | |
} else if (required == TokenType.CRLF || required == TokenType.SPACE) { | ||
throw new IllegalArgumentException(INVALID_HEADER_COMPOSITION); | ||
} else { | ||
byte ender1 = CR; | ||
byte ender2 = CR; | ||
byte ender1; | ||
byte ender2; | ||
if (required == null || required == TokenType.TEXT) { | ||
type = TokenType.TEXT; | ||
} else if (required == TokenType.WORD) { | ||
ender1 = CR; | ||
ender2 = CR; | ||
} | ||
else if (required == TokenType.WORD) { | ||
ender1 = SP; | ||
ender2 = CR; | ||
type = TokenType.WORD; | ||
|
@@ -74,6 +83,7 @@ public Token(byte[] serialized, int len, int cur, TokenType required) { | |
} | ||
hasValue = true; | ||
} | ||
valueLength = hasValue ? end - start + 1 : 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. holding length in a variable removes the need to calculate it repeatedly |
||
} | ||
|
||
private void mustBeCrlf(int len, int cur) { | ||
|
@@ -96,8 +106,171 @@ public boolean hasValue() { | |
return hasValue; | ||
} | ||
|
||
@NonNull | ||
public String getValue() { | ||
return hasValue ? new String(serialized, start, end - start + 1, US_ASCII).trim() : EMPTY; | ||
return hasValue ? valueAsString() : EMPTY; | ||
} | ||
|
||
private String valueAsString() { | ||
return new String(serialized, start, valueLength, US_ASCII).trim(); | ||
} | ||
|
||
@NonNull | ||
public String getValueCheckKnownKeys() { | ||
if (valueLength == 0) { | ||
return EMPTY; | ||
} | ||
byte b = serialized[start]; | ||
if (b == 'N') { | ||
if (valueEquals(NATS_STREAM_BYTES)) { | ||
return NATS_STREAM; | ||
} | ||
if (valueEquals(NATS_SEQUENCE_BYTES)) { | ||
return NATS_SEQUENCE; | ||
} | ||
if (valueEquals(NATS_TIMESTAMP_BYTES)) { | ||
return NATS_TIMESTAMP; | ||
} | ||
if (valueEquals(NATS_SUBJECT_BYTES)) { | ||
return NATS_SUBJECT; | ||
} | ||
if (valueEquals(NATS_LAST_SEQUENCE_BYTES)) { | ||
return NATS_LAST_SEQUENCE; | ||
} | ||
if (valueEquals(NATS_NUM_PENDING_BYTES)) { | ||
return NATS_NUM_PENDING; | ||
} | ||
} | ||
return valueAsString(); | ||
} | ||
|
||
@Nullable | ||
public String getValueCheckKnownStatuses() { | ||
if (valueLength == 0) { | ||
return null; | ||
} | ||
byte b = serialized[start]; | ||
if (b == 'B') { | ||
if (valueEquals(BATCH_COMPLETED_BYTES)) { | ||
return BATCH_COMPLETED; | ||
} | ||
if (valueEquals(BAD_REQUEST_BYTES)) { | ||
return BAD_REQUEST; | ||
} | ||
} | ||
else if (b == 'E') { | ||
if (valueEquals(EXCEEDED_MAX_PREFIX_BYTES)) { | ||
return EXCEEDED_MAX_PREFIX; | ||
} | ||
if (valueEquals(EXCEEDED_MAX_WAITING_BYTES)) { | ||
return EXCEEDED_MAX_WAITING; | ||
} | ||
if (valueEquals(EXCEEDED_MAX_REQUEST_BATCH_BYTES)) { | ||
return EXCEEDED_MAX_REQUEST_BATCH; | ||
} | ||
if (valueEquals(EXCEEDED_MAX_REQUEST_EXPIRES_BYTES)) { | ||
return EXCEEDED_MAX_REQUEST_EXPIRES; | ||
} | ||
if (valueEquals(EXCEEDED_MAX_REQUEST_MAX_BYTES_BYTES)) { | ||
return EXCEEDED_MAX_REQUEST_MAX_BYTES; | ||
} | ||
if (valueEquals(EOB_TEXT_BYTES)) { | ||
return EOB_TEXT; | ||
} | ||
} | ||
else if (b == 'N') { | ||
if (valueEquals(NO_RESPONDERS_TEXT_BYTES)) { | ||
return NO_RESPONDERS_TEXT; | ||
} | ||
if (valueEquals(NO_MESSAGES_BYTES)) { | ||
return NO_MESSAGES; | ||
} | ||
} | ||
else if (b == 'F') { | ||
if (valueEquals(FLOW_CONTROL_TEXT_BYTES)) { | ||
return FLOW_CONTROL_TEXT; | ||
} | ||
} | ||
else if (b == 'I') { | ||
if (valueEquals(HEARTBEAT_TEXT_BYTES)) { | ||
return HEARTBEAT_TEXT; | ||
} | ||
} | ||
else if (b == 'M') { | ||
if (valueEquals(MESSAGE_SIZE_EXCEEDS_MAX_BYTES_BYTES)) { | ||
return MESSAGE_SIZE_EXCEEDS_MAX_BYTES; | ||
} | ||
} | ||
else if (b == 'L') { | ||
if (valueEquals(LEADERSHIP_CHANGE_BYTES)) { | ||
return LEADERSHIP_CHANGE; | ||
} | ||
} | ||
else if (b == 'S') { | ||
if (valueEquals(SERVER_SHUTDOWN_BYTES)) { | ||
return SERVER_SHUTDOWN; | ||
} | ||
} | ||
else if (b == 'C') { | ||
if (valueEquals(CONSUMER_DELETED_BYTES)) { | ||
return CONSUMER_DELETED; | ||
} | ||
if (valueEquals(CONSUMER_IS_PUSH_BASED_BYTES)) { | ||
return CONSUMER_IS_PUSH_BASED; | ||
} | ||
} | ||
return valueAsString(); | ||
} | ||
|
||
public Integer getIntValue() throws NumberFormatException { | ||
if (valueLength == 0) { | ||
return null; | ||
} | ||
byte b = serialized[start]; | ||
if (b == '4') { | ||
if (valueEquals(BAD_REQUEST_CODE_BYTES)) { | ||
return BAD_REQUEST_CODE; | ||
} | ||
if (valueEquals(NOT_FOUND_CODE_BYTES)) { | ||
return NOT_FOUND_CODE; | ||
} | ||
if (valueEquals(BAD_JS_REQUEST_CODE_BYTES)) { | ||
return BAD_JS_REQUEST_CODE; | ||
} | ||
if (valueEquals(CONFLICT_CODE_BYTES)) { | ||
return CONFLICT_CODE; | ||
} | ||
} | ||
else if (b == '1') { | ||
if (valueEquals(FLOW_OR_HEARTBEAT_STATUS_CODE_BYTES)) { | ||
return FLOW_OR_HEARTBEAT_STATUS_CODE; | ||
} | ||
} | ||
else if (b == '5') { | ||
if (valueEquals(NO_RESPONDERS_CODE_BYTES)) { | ||
return NO_RESPONDERS_CODE; | ||
} | ||
} | ||
else if (b == '2') { | ||
if (valueEquals(EOB_CODE_BYTES)) { | ||
return EOB_CODE; | ||
} | ||
} | ||
return Integer.parseInt(getValue()); | ||
} | ||
|
||
public boolean valueEquals(byte @NonNull [] bytes) { | ||
if (valueLength != bytes.length) { | ||
return false; | ||
} | ||
|
||
for (int i = 0; i < bytes.length; i++) { | ||
if (bytes[i] != serialized[i + start]) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public boolean samePoint(Token token) { | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
variable was never used in the initHeader function that it was passed to and there are no other uses, so it was removed.