Skip to content

Commit

Permalink
Streamlining the discharge method (micro-optimization)
Browse files Browse the repository at this point in the history
  • Loading branch information
lemire committed May 4, 2016
1 parent 26dbdd0 commit f869651
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 48 deletions.
Expand Up @@ -115,26 +115,31 @@ public boolean next() {
* @return how many written
*/
public long discharge(BitmapStorage container, long max) {
long index = 0;
while ((index < max) && (size() > 0)) {
// first run
long pl = getRunningLength();
if (index + pl > max) {
pl = max - index;
}
container.addStreamOfEmptyWords(getRunningBit(), pl);
index += pl;
int pd = getNumberOfLiteralWords();
if (pd + index > max) {
pd = (int) (max - index);
}
writeLiteralWords(pd, container);
discardFirstWords(pl + pd);
index += pd;
}
return index;
long index = 0;
while (true) {
if (index + getRunningLength() > max) {
final int offset = (int) (max - index);
container.addStreamOfEmptyWords(getRunningBit(), offset);
this.brlw.runningLength -= offset;
return max;
}
container.addStreamOfEmptyWords(getRunningBit(), getRunningLength());
index += getRunningLength();
if (getNumberOfLiteralWords() + index > max) {
final int offset =(int) (max - index);
writeLiteralWords(offset, container);
this.brlw.runningLength = 0;
this.brlw.numberOfLiteralWords -= offset;
this.literalWordStartPosition += offset;
return max;
}
writeLiteralWords(getNumberOfLiteralWords(), container);
index += getNumberOfLiteralWords();
if(!next()) break;
}
return index;
}

/**
* Write out up to max words (negated), returns how many were written
*
Expand Down
Expand Up @@ -72,17 +72,17 @@ public void discardFirstWords(int x) {
}

@Override
public void discardLiteralWords(int x) {
this.literalWordStartPosition += x;
this.brlw.NumberOfLiteralWords -= x;
if (this.brlw.NumberOfLiteralWords == 0) {
if (!this.iterator.hasNext()) {
return;
}
this.brlw.reset(this.iterator.next());
this.literalWordStartPosition = this.iterator.literalWords();
}
}
public void discardLiteralWords(int x) {
this.literalWordStartPosition += x;
this.brlw.NumberOfLiteralWords -= x;
if (this.brlw.NumberOfLiteralWords == 0) {
if (!this.iterator.hasNext()) {
return;
}
this.brlw.reset(this.iterator.next());
this.literalWordStartPosition = this.iterator.literalWords();
}
}


@Override
Expand All @@ -100,26 +100,32 @@ public void discardRunningWords() {
* @return how many written
*/
public int discharge(BitmapStorage32 container, int max) {
int index = 0;
while ((index < max) && (size() > 0)) {
// first run
int pl = getRunningLength();
if (index + pl > max) {
pl = max - index;
}
container.addStreamOfEmptyWords(getRunningBit(), pl);
index += pl;
int pd = getNumberOfLiteralWords();
if (pd + index > max) {
pd = max - index;
}
writeLiteralWords(pd, container);
discardFirstWords(pl + pd);
index += pd;
int index = 0;
while (true) {
if (index + getRunningLength() > max) {
final int offset = max - index;
container.addStreamOfEmptyWords(getRunningBit(), offset);
this.brlw.RunningLength -= offset;
return max;
}
return index;
container.addStreamOfEmptyWords(getRunningBit(), getRunningLength());
index += getRunningLength();
if (getNumberOfLiteralWords() + index > max) {
final int offset = max - index;
writeLiteralWords(offset, container);
this.brlw.RunningLength = 0;
this.brlw.NumberOfLiteralWords -= offset;
this.literalWordStartPosition += offset;
return max;
}
writeLiteralWords(getNumberOfLiteralWords(), container);
index += getNumberOfLiteralWords();
if(!next()) break;
}
return index;
}


/**
* Write out up to max words (negated), returns how many were written
*
Expand Down Expand Up @@ -311,6 +317,6 @@ public IteratingBufferedRunningLengthWord32 clone()
private final Buffer32 buffer;
private int literalWordStartPosition;
private EWAHIterator32 iterator;

}

0 comments on commit f869651

Please sign in to comment.