Skip to content

Commit

Permalink
PARQUET-580: Switch int[] initialization in IntList to be lazy
Browse files Browse the repository at this point in the history
Noticed that for a dataset that we were trying to import that had a lot of columns (few thousand) that weren't being used, we ended up allocating a lot of unnecessary int arrays (each 64K in size). Heap footprint for all those int[]s turned out to be around 2GB or so (and results in some jobs OOMing). This seems unnecessary for columns that might not be used. The changes in this PR switch over to initialize the int[] only when it being used for the first time.

Also wondering if 64K is the right size to start off with. Wondering if a potential improvement is if we could allocate these int[]s in IntList in a way that slowly ramps up their size. So rather than create arrays of size 64K at a time (which is potentially wasteful if there are only a few hundred bytes), we could create say a 4K int[], then when it fills up an 8K[] and so on till we reach 64K (at which point the behavior is the same as the current implementation). If this sounds like a reasonable idea, I can update this PR to do that as well. Wasn't sure if there was some historical context around that..

Author: Piyush Narang <pnarang@twitter.com>

Closes apache#339 from piyushnarang/master and squashes the following commits:

3ecc577 [Piyush Narang] Remove redundant IntList ctor
f7dfd5f [Piyush Narang] Switch int[] initialization in IntList to be lazy
  • Loading branch information
Piyush Narang authored and rdblue committed Jan 6, 2017
1 parent 5e708d6 commit 6937da9
Showing 1 changed file with 12 additions and 9 deletions.
Expand Up @@ -58,7 +58,7 @@ public IntIterator(int[][] slabs, int count) {
}

/**
* @return wether there is a next value
* @return whether there is a next value
*/
public boolean hasNext() {
return current < count;
Expand All @@ -76,16 +76,12 @@ public int next() {
}

private List<int[]> slabs = new ArrayList<int[]>();

// Lazy initialize currentSlab only when needed to save on memory in cases where items might
// not be added
private int[] currentSlab;
private int currentSlabPos;

/**
* construct an empty list
*/
public IntList() {
initSlab();
}

private void initSlab() {
currentSlab = new int[SLAB_SIZE];
currentSlabPos = 0;
Expand All @@ -95,10 +91,13 @@ private void initSlab() {
* @param i value to append to the end of the list
*/
public void add(int i) {
if (currentSlabPos == currentSlab.length) {
if (currentSlab == null) {
initSlab();
} else if (currentSlabPos == currentSlab.length) {
slabs.add(currentSlab);
initSlab();
}

currentSlab[currentSlabPos] = i;
++ currentSlabPos;
}
Expand All @@ -108,6 +107,10 @@ public void add(int i) {
* @return an IntIterator on the content
*/
public IntIterator iterator() {
if (currentSlab == null) {
initSlab();
}

int[][] itSlabs = slabs.toArray(new int[slabs.size() + 1][]);
itSlabs[slabs.size()] = currentSlab;
return new IntIterator(itSlabs, SLAB_SIZE * slabs.size() + currentSlabPos);
Expand Down

0 comments on commit 6937da9

Please sign in to comment.