Skip to content

Commit

Permalink
Avoid creating new objects when region is same as original page
Browse files Browse the repository at this point in the history
  • Loading branch information
sopel39 committed May 8, 2023
1 parent 16eca5e commit 122f513
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
4 changes: 4 additions & 0 deletions core/trino-spi/src/main/java/io/trino/spi/Page.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ public Page getRegion(int positionOffset, int length)
throw new IndexOutOfBoundsException(format("Invalid position %s and length %s in page with %s positions", positionOffset, length, positionCount));
}

if (positionOffset == 0 && length == positionCount) {
return this;
}

int channelCount = getChannelCount();
Block[] slicedBlocks = new Block[channelCount];
for (int i = 0; i < channelCount; i++) {
Expand Down
5 changes: 4 additions & 1 deletion core/trino-spi/src/test/java/io/trino/spi/TestPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,17 @@
import static org.assertj.core.api.Assertions.assertThatThrownBy;
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);
assertEquals(page.getRegion(5, 5).getPositionCount(), 5);
assertSame(page.getRegion(0, 10), page);
}

@Test
Expand Down

0 comments on commit 122f513

Please sign in to comment.