-
Notifications
You must be signed in to change notification settings - Fork 214
use iteration in set ops, wrap compressed sketch and unpack in iterator #644
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
Changes from 3 commits
adff890
a0f1fd6
2698f0d
29895b3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.apache.datasketches.theta; | ||
|
||
import static org.apache.datasketches.theta.PreambleUtil.extractEntryBitsV4; | ||
import static org.apache.datasketches.theta.PreambleUtil.extractNumEntriesBytesV4; | ||
import static org.apache.datasketches.theta.PreambleUtil.extractPreLongs; | ||
import static org.apache.datasketches.theta.PreambleUtil.extractSeedHash; | ||
import static org.apache.datasketches.theta.PreambleUtil.extractThetaLongV4; | ||
import static org.apache.datasketches.theta.PreambleUtil.wholeBytesToHoldBits; | ||
|
||
import org.apache.datasketches.memory.Memory; | ||
import org.apache.datasketches.memory.WritableMemory; | ||
import org.apache.datasketches.thetacommon.ThetaUtil; | ||
|
||
/** | ||
* An off-heap (Direct), compact, compressed, read-only sketch. It is not empty, not a single item and ordered. | ||
* | ||
* <p>This sketch can only be associated with a Serialization Version 4 format binary image.</p> | ||
* | ||
* <p>This implementation uses data in a given Memory that is owned and managed by the caller. | ||
* This Memory can be off-heap, which if managed properly will greatly reduce the need for | ||
* the JVM to perform garbage collection.</p> | ||
*/ | ||
class DirectCompactCompressedSketch extends DirectCompactSketch { | ||
/** | ||
* Construct this sketch with the given memory. | ||
* @param mem Read-only Memory object with the order bit properly set. | ||
*/ | ||
DirectCompactCompressedSketch(final Memory mem) { | ||
super(mem); | ||
} | ||
|
||
/** | ||
* Wraps the given Memory, which must be a SerVer 4 compressed CompactSketch image. | ||
* Must check the validity of the Memory before calling. The order bit must be set properly. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. again |
||
* @param srcMem <a href="{@docRoot}/resources/dictionary.html#mem">See Memory</a> | ||
* @param seedHash The update seedHash. | ||
* <a href="{@docRoot}/resources/dictionary.html#seedHash">See Seed Hash</a>. | ||
* @return this sketch | ||
*/ | ||
static DirectCompactCompressedSketch wrapInstance(final Memory srcMem, final short seedHash) { | ||
ThetaUtil.checkSeedHashes((short) extractSeedHash(srcMem), seedHash); | ||
return new DirectCompactCompressedSketch(srcMem); | ||
} | ||
|
||
//Sketch Overrides | ||
|
||
@Override | ||
public CompactSketch compact(final boolean dstOrdered, final WritableMemory dstMem) { | ||
if (dstMem != null) { | ||
mem_.copyTo(0, dstMem, 0, getCurrentBytes()); | ||
return new DirectCompactSketch(dstMem); | ||
} | ||
return CompactSketch.heapify(mem_); | ||
} | ||
|
||
@Override | ||
public int getCurrentBytes() { | ||
final int preLongs = extractPreLongs(mem_); | ||
final int entryBits = extractEntryBitsV4(mem_); | ||
final int numEntriesBytes = extractNumEntriesBytesV4(mem_); | ||
return preLongs * Long.BYTES + numEntriesBytes + wholeBytesToHoldBits(getRetainedEntries() * entryBits); | ||
} | ||
|
||
@Override | ||
public int getRetainedEntries(final boolean valid) { //compact is always valid | ||
final int preLongs = extractPreLongs(mem_); | ||
final int numEntriesBytes = extractNumEntriesBytesV4(mem_); | ||
int offsetBytes = preLongs > 1 ? 16 : 8; | ||
int numEntries = 0; | ||
for (int i = 0; i < numEntriesBytes; i++) { | ||
numEntries |= Byte.toUnsignedInt(mem_.getByte(offsetBytes++)) << (i << 3); | ||
} | ||
return numEntries; | ||
} | ||
|
||
@Override | ||
public long getThetaLong() { | ||
final int preLongs = extractPreLongs(mem_); | ||
return (preLongs > 1) ? extractThetaLongV4(mem_) : Long.MAX_VALUE; | ||
} | ||
|
||
@Override | ||
public boolean isEmpty() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public boolean isOrdered() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public HashIterator iterator() { | ||
return new MemoryCompactCompressedHashIterator( | ||
mem_, | ||
(extractPreLongs(mem_) > 1 ? 16 : 8) + extractNumEntriesBytesV4(mem_), | ||
extractEntryBitsV4(mem_), | ||
getRetainedEntries() | ||
); | ||
} | ||
|
||
//restricted methods | ||
|
||
@Override | ||
long[] getCache() { | ||
final int numEntries = getRetainedEntries(); | ||
final long[] cache = new long[numEntries]; | ||
int i = 0; | ||
HashIterator it = iterator(); | ||
while (it.next()) { | ||
cache[i++] = it.get(); | ||
} | ||
return cache; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -288,7 +288,7 @@ else if (curCount_ < 0 && sketchInEntries > 0) { | |
else { //On the heap, allocate a HT | ||
hashTable_ = new long[1 << lgArrLongs_]; | ||
} | ||
moveDataToTgt(sketchIn.getCache(), curCount_); | ||
moveDataToTgt(sketchIn); | ||
} //end of state 5 | ||
|
||
//state 7 | ||
|
@@ -434,8 +434,6 @@ long getThetaLong() { | |
private void performIntersect(final Sketch sketchIn) { | ||
// curCount and input data are nonzero, match against HT | ||
assert curCount_ > 0 && !empty_; | ||
final long[] cacheIn = sketchIn.getCache(); | ||
final int arrLongsIn = cacheIn.length; | ||
final long[] hashTable; | ||
if (wmem_ != null) { | ||
final int htLen = 1 << lgArrLongs_; | ||
|
@@ -448,27 +446,16 @@ private void performIntersect(final Sketch sketchIn) { | |
final long[] matchSet = new long[ min(curCount_, sketchIn.getRetainedEntries(true)) ]; | ||
|
||
int matchSetCount = 0; | ||
if (sketchIn.isOrdered()) { | ||
//ordered compact, which enables early stop | ||
for (int i = 0; i < arrLongsIn; i++ ) { | ||
final long hashIn = cacheIn[i]; | ||
//if (hashIn <= 0L) continue; //<= 0 should not happen | ||
if (hashIn >= thetaLong_) { | ||
break; //early stop assumes that hashes in input sketch are ordered! | ||
} | ||
HashIterator it = sketchIn.iterator(); | ||
while (it.next()) { | ||
final long hashIn = it.get(); | ||
if (hashIn < thetaLong_) { | ||
final int foundIdx = hashSearch(hashTable, lgArrLongs_, hashIn); | ||
if (foundIdx == -1) { continue; } | ||
matchSet[matchSetCount++] = hashIn; | ||
} | ||
} | ||
else { | ||
//either unordered compact or hash table | ||
for (int i = 0; i < arrLongsIn; i++ ) { | ||
final long hashIn = cacheIn[i]; | ||
if (hashIn <= 0L || hashIn >= thetaLong_) { continue; } | ||
final int foundIdx = hashSearch(hashTable, lgArrLongs_, hashIn); | ||
if (foundIdx == -1) { continue; } | ||
matchSet[matchSetCount++] = hashIn; | ||
if (foundIdx != -1) { | ||
matchSet[matchSetCount++] = hashIn; | ||
} | ||
} else { | ||
if (sketchIn.isOrdered()) { break; } // early stop | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This won't change over the course of the intersection. Should it be a flag pre-computed in advance to avoid a function call every time? Not sure if the JVM can easily determine there's no chance of a side-effect from other code. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Generally I prefer not to introduce aliases, but if you think this can help, I can do that. Perhaps we could try measuring the effect one day. |
||
} | ||
} | ||
//reduce effective array size to minimum | ||
|
@@ -515,6 +502,32 @@ private void moveDataToTgt(final long[] arr, final int count) { | |
assert tmpCnt == count : "Intersection Count Check: got: " + tmpCnt + ", expected: " + count; | ||
} | ||
|
||
private void moveDataToTgt(final Sketch sketch) { | ||
int count = sketch.getRetainedEntries(); | ||
int tmpCnt = 0; | ||
if (wmem_ != null) { //Off Heap puts directly into mem | ||
final int preBytes = CONST_PREAMBLE_LONGS << 3; | ||
final int lgArrLongs = lgArrLongs_; | ||
final long thetaLong = thetaLong_; | ||
HashIterator it = sketch.iterator(); | ||
while (it.next()) { | ||
final long hash = it.get(); | ||
if (continueCondition(thetaLong, hash)) { continue; } | ||
hashInsertOnlyMemory(wmem_, lgArrLongs, hash, preBytes); | ||
tmpCnt++; | ||
} | ||
} else { //On Heap. Assumes HT exists and is large enough | ||
HashIterator it = sketch.iterator(); | ||
while (it.next()) { | ||
final long hash = it.get(); | ||
if (continueCondition(thetaLong_, hash)) { continue; } | ||
hashInsertOnly(hashTable_, lgArrLongs_, hash); | ||
tmpCnt++; | ||
} | ||
} | ||
assert tmpCnt == count : "Intersection Count Check: got: " + tmpCnt + ", expected: " + count; | ||
} | ||
|
||
private void hardReset() { | ||
resetCommon(); | ||
if (wmem_ != null) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than "properly set" maybe just say it must be ordered and with the bit set to true?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this refers to memory byte order. Perhaps can be rephrased for clarity.