Skip to content

Commit

Permalink
Preserve original page objects when region remains unchanged
Browse files Browse the repository at this point in the history
Avoid creating new objects when region is same as original page
Cherry-pick of trinodb/trino@122f513 (trinodb/trino#17385)

Co-authored-by: Karol Sobczak <[email protected]>
  • Loading branch information
2 people authored and tdcmeehan committed Dec 19, 2023
1 parent 2e140a6 commit 1e6dc1d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,19 @@ public Page getSingleValuePage(int position)
return wrapBlocksWithoutCopy(1, singleValueBlocks);
}

// getRegion() is used to get a sub-page or region of a page based on the given positionOffset and length
public Page getRegion(int positionOffset, int length)
{
if (positionOffset < 0 || length < 0 || positionOffset + length > positionCount) {
throw new IndexOutOfBoundsException(format("Invalid position %s and length %s in page with %s positions", positionOffset, length, positionCount));
}

// Avoid creating new objects when region is same as original page
if (positionOffset == 0 && length == positionCount) {
return this;
}

// Create a new page view with the specified region
int channelCount = getChannelCount();
Block[] slicedBlocks = new Block[channelCount];
for (int i = 0; i < channelCount; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,27 @@
import static com.google.common.base.Verify.verify;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotEquals;
import static org.testng.Assert.assertSame;
import static org.testng.Assert.assertTrue;

public class TestPage
{
@Test
public void testGetRegion()
{
assertEquals(new Page(10).getRegion(5, 5).getPositionCount(), 5);
Page page = new Page(10);
Page region = page.getRegion(0, 10);
assertEquals(page.getRegion(5, 5).getPositionCount(), 5);
assertEquals(region.getPositionCount(), 10);
assertSame(page, region);
}

@Test
public void testGetEmptyRegion()
{
Page page = new Page(10);
assertEquals(new Page(0).getRegion(0, 0).getPositionCount(), 0);
assertEquals(new Page(10).getRegion(5, 0).getPositionCount(), 0);
assertEquals(page.getRegion(5, 0).getPositionCount(), 0);
}

@Test(expectedExceptions = IndexOutOfBoundsException.class, expectedExceptionsMessageRegExp = "Invalid position 1 and length 1 in page with 0 positions")
Expand Down

0 comments on commit 1e6dc1d

Please sign in to comment.