-
Notifications
You must be signed in to change notification settings - Fork 247
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
perf(java): Reduce performance regression caused by deleteCharAt #1591
Conversation
Signed-off-by: LiangliangSui <coolsui.coding@gmail.com>
MetaString is not int critical path, maybe we don't need to optimize this? |
Through this optimization we can also reduce the operations of appending and deleting to StringBuilder. Although it is not on the critical path, it is still profitable. |
@@ -74,7 +74,7 @@ private String decodeLowerSpecial(byte[] data) { | |||
boolean stripLastChar = (data[0] & 0x80) != 0; // Check the first bit of the first byte | |||
int bitMask = 0b11111; // 5 bits for the mask | |||
int bitIndex = 1; // Start from the second bit | |||
while (bitIndex + 5 <= totalBits) { | |||
while (bitIndex + 5 <= totalBits && !(stripLastChar && (bitIndex + 2 * 5 > totalBits))) { |
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.
Could you explain what !(stripLastChar && (bitIndex + 2 * 5 > totalBits))
mean? It seems not intutive to me.
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.
(bitIndex + 2 * 5 > totalBits)
indicates that the current character is the last character.
!(stripLastChar && (bitIndex + 2 * 5 > totalBits))
means that if the current character is the last character and the last character needs to be deleted, the while loop will not be entered.
This can reduce the last redundant StringBuilder#append -> StringBuilder#deleteCharAt
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.
Got it, thanks
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.
LGTM
What does this PR do?
This PR reduces the performance regression caused by StringBuilder#deleteCharAt, avoids using deleteCharAt, and makes early judgments in while.
Related issues
Does this PR introduce any user-facing change?
Benchmark
before this PR
with this pr