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

Use reference count in pages index #10634

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package com.facebook.presto.operator;

import com.facebook.presto.Session;
import com.facebook.presto.array.ReferenceCountMap;
import com.facebook.presto.metadata.FunctionRegistry;
import com.facebook.presto.metadata.Metadata;
import com.facebook.presto.metadata.MetadataManager;
Expand Down Expand Up @@ -213,13 +214,19 @@ public void addPage(Page page)
positionCount += page.getPositionCount();

int pageIndex = (channels.length > 0) ? channels[0].size() : 0;
ReferenceCountMap referenceCountMap = new ReferenceCountMap();
for (int i = 0; i < channels.length; i++) {
Block block = page.getBlock(i);
if (eagerCompact) {
block = block.copyRegion(0, block.getPositionCount());
}
channels[i].add(block);
pagesMemorySize += block.getRetainedSizeInBytes();

block.retainedBytesForEachPart((object, size) -> {
Copy link
Contributor

Choose a reason for hiding this comment

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

My memory is that this is unbelievably expensive, which is why we only did this very sparingly. Did you do some performance analysis?

Copy link
Contributor

Choose a reason for hiding this comment

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

yes, we should avoid using retainedBytesForEachPart as much as possible. The ultimate goal is to remove this interface and the reference count map if possible. Memory accounting dedup should happen somewhere else, or there is an elegant way to do so. Also, if we have to use this interface, run benchmarks with huge inputs (e.g., 1 billion distinct blocks or something like that) to show the effect.

Copy link
Contributor Author

@sopel39 sopel39 May 17, 2018

Choose a reason for hiding this comment

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

Thanks for clarification. I was wondering if there are some issues with using retainedBytesForEachPart method.

Did you do some performance analysis?

I will add some benchmarks of building/compacting PagexIndex with pages containing high number of channels.

Also, if we have to use this interface, run benchmarks with huge inputs (e.g., 1 billion distinct blocks or something like that) to show the effect.

This ReferenceCountMap exists only for the duration of the page processing (it should be the same in compact() too), so the overhead factor should be linear with the number of page channels. I will add such benchmarks.

if (referenceCountMap.incrementAndGet(object) == 1) {
pagesMemorySize += size;
}
});
}

for (int position = 0; position < page.getPositionCount(); position++) {
Expand All @@ -239,6 +246,8 @@ public void compact()
if (eagerCompact) {
return;
}
ReferenceCountMap decrementReferenceCountMap = new ReferenceCountMap();
ReferenceCountMap incrementReferenceCountMap = new ReferenceCountMap();
for (int channel = 0; channel < types.size(); channel++) {
ObjectArrayList<Block> blocks = channels[channel];
for (int i = nextBlockToCompact; i < blocks.size(); i++) {
Expand All @@ -247,8 +256,18 @@ public void compact()
// Copy the block to compact its size
Block compactedBlock = block.copyRegion(0, block.getPositionCount());
blocks.set(i, compactedBlock);
pagesMemorySize -= block.getRetainedSizeInBytes();
pagesMemorySize += compactedBlock.getRetainedSizeInBytes();

block.retainedBytesForEachPart((object, size) -> {
if (decrementReferenceCountMap.incrementAndGet(object) == 1) {
pagesMemorySize -= size;
}
});

compactedBlock.retainedBytesForEachPart((object, size) -> {
if (incrementReferenceCountMap.incrementAndGet(object) == 1) {
pagesMemorySize += size;
}
});
}
}
nextBlockToCompact = channels[0].size();
Expand Down Expand Up @@ -562,10 +581,12 @@ protected Page computeNext()
// TODO: This is similar to what OrderByOperator does, look into reusing this logic in OrderByOperator as well.
public Iterator<Page> getSortedPages()
{
return new AbstractIterator<Page>() {
return new AbstractIterator<Page>()
{
private int currentPosition;
private PageBuilder pageBuilder = new PageBuilder(types);
private int[] outputChannels = new int[types.size()];

{
Arrays.setAll(outputChannels, IntUnaryOperator.identity());
}
Expand Down