You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@lucene.apache.org by GitBox <gi...@apache.org> on 2021/06/01 13:58:11 UTC

[GitHub] [lucene] bruno-roustant commented on a change in pull request #163: LUCENE-9983: Stop sorting determinize powersets unnecessarily

bruno-roustant commented on a change in pull request #163:
URL: https://github.com/apache/lucene/pull/163#discussion_r643116749



##########
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;

Review comment:
       Could we rename "changed" to "keysChanged" as this only tracks keys and not values?

##########
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:
       I find strange that this method is called externally by Operations. Since the hash is cached and we track when it needs to be recomputed with "changed", could we make it private and change the signature to "private int getOrComputeHash()"?

##########
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:
       According to the stats you shared @zhaih in the Jira issue, there are 3 times more get (inc/dec) & removal than entry additions. If there are less than 2M states then IntIntWormMap should be more efficient.
   I suggest that you benchmark with IntIntHashMap and IntIntWormMap (with your existing tests) and take the most appropriate.

##########
File path: lucene/core/src/java/org/apache/lucene/util/automaton/Operations.java
##########
@@ -676,7 +677,7 @@ public static Automaton determinize(Automaton a, int maxDeterminizedStates) {
     // a.writeDot("/l/la/lucene/core/detin.dot");
 
     // Same initial values and state will always have the same hashCode
-    FrozenIntSet initialset = new FrozenIntSet(new int[] {0}, 683, 0);
+    FrozenIntSet initialset = new FrozenIntSet(new int[] {0}, BitMixer.mix(0) + 1, 0);

Review comment:
       Why not simply 0 as initial hash code?

##########
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.

##########
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);

Review comment:
       @dweiss ideally here I would use inner.indexRemove(keyIndex). Does this miss in HPPC?




-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org