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

perf(java): Reduce performance regression caused by deleteCharAt #1591

Merged
merged 1 commit into from
Apr 29, 2024

Conversation

LiangliangSui
Copy link
Contributor

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?

  • Does this PR introduce any public API change?
  • Does this PR introduce any binary protocol compatibility change?

Benchmark

before this PR
image

with this pr
image

Signed-off-by: LiangliangSui <coolsui.coding@gmail.com>
@chaokunyang
Copy link
Collaborator

MetaString is not int critical path, maybe we don't need to optimize this?

@LiangliangSui
Copy link
Contributor Author

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))) {
Copy link
Collaborator

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.

Copy link
Contributor Author

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

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, thanks

Copy link
Collaborator

@chaokunyang chaokunyang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@chaokunyang chaokunyang merged commit f218c91 into apache:main Apr 29, 2024
32 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants