Skip to content

Commit

Permalink
Optimize in ByteString to prevent excessive object allocation during …
Browse files Browse the repository at this point in the history
…string search (#1034)

Co-authored-by: Junchuan Wang <juncwang@juncwang-ld4.linkedin.biz>
  • Loading branch information
junchuanwang and Junchuan Wang authored Nov 5, 2024
1 parent 6664ccc commit 5ec7eb9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ and what APIs have changed, if applicable.

## [Unreleased]

## [29.62.1] - 2024-11-05
- Enhancements in ByteString and its ByteIterator to reduce object allocation

## [29.62.0] - 2024-10-28
- Check and take configurable action for invalid partition weight

Expand Down Expand Up @@ -5755,7 +5758,8 @@ patch operations can re-use these classes for generating patch messages.

## [0.14.1]

[Unreleased]: https://github.com/linkedin/rest.li/compare/v29.62.0...master
[Unreleased]: https://github.com/linkedin/rest.li/compare/v29.62.1...master
[29.62.1]: https://github.com/linkedin/rest.li/compare/v29.62.0...v29.62.1
[29.62.0]: https://github.com/linkedin/rest.li/compare/v29.61.0...v29.62.0
[29.61.0]: https://github.com/linkedin/rest.li/compare/v29.60.0...v29.61.0
[29.60.0]: https://github.com/linkedin/rest.li/compare/v29.59.0...v29.60.0
Expand Down
14 changes: 7 additions & 7 deletions data/src/main/java/com/linkedin/data/ByteString.java
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,12 @@ private ByteIterator copy()
return new ByteIterator(this);
}

private void fillFromAnother(ByteIterator other) {
_currentByteArray = other._currentByteArray;
_currentByteIndex = other._currentByteIndex;
_finished = other._finished;
}

private void next()
{
//Shift the internal pointer to the next byte.
Expand Down Expand Up @@ -737,9 +743,6 @@ public int indexOfBytes(byte[] targetBytes)
//This is a reference on where to resume in case we get a mismatch.
ByteIterator resumeByteIterator = byteIterator.copy();

//We skip the first since byteIterator will begin there.
resumeByteIterator.next();

for (int i = 0; i < targetBytes.length;)
{
//If we have exhausted everything in the ByteString, then we return -1.
Expand All @@ -754,11 +757,8 @@ public int indexOfBytes(byte[] targetBytes)
//There was a mismatch so we reset i and prepare to start over.
i = 0;
//Update byteIterator to point to the next byte where our comparison will begin.
byteIterator = resumeByteIterator;
//Keep track of where to resume in the future.
resumeByteIterator = resumeByteIterator.copy();
//Skip the next since byteIterator will begin there.
resumeByteIterator.next();
byteIterator.fillFromAnother(resumeByteIterator);
continue;
}

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version=29.62.0
version=29.62.1
group=com.linkedin.pegasus
org.gradle.configureondemand=true
org.gradle.parallel=true
Expand Down

0 comments on commit 5ec7eb9

Please sign in to comment.