mikemccand commented on a change in pull request #163: URL: https://github.com/apache/lucene/pull/163#discussion_r643866949
########## File path: lucene/core/build.gradle ########## @@ -20,6 +20,8 @@ apply plugin: 'java-library' description = 'Lucene core library' dependencies { + implementation 'com.carrotsearch:hppc' Review comment: > @bruno-roustant came up with some clever new hashing improvements recently - these are not published as a public revision but you can get them from the repository and compile it locally. See this for details: > > https://issues.carrot2.org/browse/HPPC-176 Whoa, this looks new "worm" hashing looks great! That is frequently a great tradeoff (slower put, faster get)? Hmm why hasn't this been "published in a public revision" yet :) ########## File path: lucene/core/src/java/org/apache/lucene/util/automaton/StateSet.java ########## @@ -0,0 +1,107 @@ +/* + * 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.lucene.util.automaton; + +import com.carrotsearch.hppc.BitMixer; +import com.carrotsearch.hppc.IntIntHashMap; +import com.carrotsearch.hppc.cursors.IntCursor; +import java.util.Arrays; + +/** A thin wrapper of {@link com.carrotsearch.hppc.IntIntHashMap} */ +final class StateSet extends IntSet { + + private final IntIntHashMap inner; + private int hashCode; + private boolean changed; + private int[] arrayCache = new int[0]; + + StateSet(int capacity) { + inner = new IntIntHashMap(capacity); + } + + // Adds this state to the set + void incr(int num) { + if (inner.addTo(num, 1) == 1) { + changed = true; + } + } + + // Removes this state from the set, if count decrs to 0 + void decr(int num) { + assert inner.containsKey(num); + int keyIndex = inner.indexOf(num); + int count = inner.indexGet(keyIndex) - 1; + if (count == 0) { + inner.remove(num); + changed = true; + } else { + inner.indexReplace(keyIndex, count); + } + } + + void computeHash() { + if (changed == false) { + return; + } + hashCode = inner.size(); + for (IntCursor cursor : inner.keys()) { + hashCode += BitMixer.mix(cursor.value); + } + } + + /** + * Create a snapshot of this int set associated with a given state. The snapshot will not retain + * any frequency information about the elements of this set, only existence. + * + * <p>It is the caller's responsibility to ensure that the hashCode and data are up to date via + * the {@link #computeHash()} method before calling this method. + * + * @param state the state to associate with the frozen set. + * @return A new FrozenIntSet with the same values as this set. + */ + FrozenIntSet freeze(int state) { + if (changed == false) { + assert arrayCache != null; Review comment: Hmm do we actually fall inside this `if`? I would think we shouldn't ever hit this -- we should't call `freeze` unless something had in fact changed? Or, if we are, something else might be wrong? Or maybe I am simply confused ;) ########## File path: lucene/core/src/java/org/apache/lucene/util/automaton/StateSet.java ########## @@ -0,0 +1,107 @@ +/* + * 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.lucene.util.automaton; + +import com.carrotsearch.hppc.BitMixer; +import com.carrotsearch.hppc.IntIntHashMap; +import com.carrotsearch.hppc.cursors.IntCursor; +import java.util.Arrays; + +/** A thin wrapper of {@link com.carrotsearch.hppc.IntIntHashMap} */ +final class StateSet extends IntSet { + + private final IntIntHashMap inner; + private int hashCode; + private boolean changed; + private int[] arrayCache = new int[0]; + + StateSet(int capacity) { + inner = new IntIntHashMap(capacity); + } + + // Adds this state to the set + void incr(int num) { + if (inner.addTo(num, 1) == 1) { + changed = true; + } + } + + // Removes this state from the set, if count decrs to 0 + void decr(int num) { + assert inner.containsKey(num); + int keyIndex = inner.indexOf(num); + int count = inner.indexGet(keyIndex) - 1; + if (count == 0) { + inner.remove(num); + changed = true; + } else { + inner.indexReplace(keyIndex, count); + } + } + + void computeHash() { Review comment: Yeah this is odd -- `+1` to make the hash code a privately maintained thing. ########## File path: lucene/core/src/java/org/apache/lucene/util/automaton/StateSet.java ########## @@ -0,0 +1,107 @@ +/* + * 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.lucene.util.automaton; + +import com.carrotsearch.hppc.BitMixer; +import com.carrotsearch.hppc.IntIntHashMap; +import com.carrotsearch.hppc.cursors.IntCursor; +import java.util.Arrays; + +/** A thin wrapper of {@link com.carrotsearch.hppc.IntIntHashMap} */ +final class StateSet extends IntSet { + + private final IntIntHashMap inner; + private int hashCode; + private boolean changed; + private int[] arrayCache = new int[0]; + + StateSet(int capacity) { + inner = new IntIntHashMap(capacity); + } + + // Adds this state to the set + void incr(int num) { + if (inner.addTo(num, 1) == 1) { + changed = true; + } + } + + // Removes this state from the set, if count decrs to 0 + void decr(int num) { + assert inner.containsKey(num); + int keyIndex = inner.indexOf(num); + int count = inner.indexGet(keyIndex) - 1; + if (count == 0) { + inner.remove(num); + changed = true; + } else { + inner.indexReplace(keyIndex, count); + } + } + + void computeHash() { + if (changed == false) { + return; + } + hashCode = inner.size(); + for (IntCursor cursor : inner.keys()) { + hashCode += BitMixer.mix(cursor.value); + } + } + + /** + * Create a snapshot of this int set associated with a given state. The snapshot will not retain + * any frequency information about the elements of this set, only existence. + * + * <p>It is the caller's responsibility to ensure that the hashCode and data are up to date via + * the {@link #computeHash()} method before calling this method. + * + * @param state the state to associate with the frozen set. + * @return A new FrozenIntSet with the same values as this set. + */ + FrozenIntSet freeze(int state) { + if (changed == false) { + assert arrayCache != null; + return new FrozenIntSet(arrayCache, hashCode, state); + } + return new FrozenIntSet(getArray(), hashCode, state); + } + + @Override + int[] getArray() { + if (changed == false) { + assert arrayCache != null; + return arrayCache; + } + changed = false; + arrayCache = inner.keys().toArray(); + // we need to sort this array since "equals" method depend on this + Arrays.sort(arrayCache); Review comment: > How many states are we manipulating? If it's not too many, then indeed maybe a simple array of state-counters is enough? > If array[i] is the counter for state i, then incr(i) / decr(i) are 0(1), hash is still O(n) (skipping 0 values), freeze does not need to be sorted. That's a neat idea! The tricky thing is that these are usually very sparsely occupied sets (each DFA state is "typically" just a few NFA states, I think), so we would have to pay the O(N) cost to walk that array and turn it into the frozen (sparse `int[]`) representation. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org For additional commands, e-mail: issues-h...@lucene.apache.org