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/05/31 22:44:27 UTC

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

mikemccand commented on a change in pull request #163:
URL: https://github.com/apache/lucene/pull/163#discussion_r642695443



##########
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:
       Can you add a `// nocommit` here?  We can't take (external) dependencies in Lucene's `core` ... but hopefully we can fork the one HPPC class we need here (`IntIntHashMap`?).

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

Review comment:
       @dweiss suggested upgrading the hash to `long` to further reduce chance of false collisions.

##########
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:
       Oh I see, we are still sorting on `freeze`, but not when accumulating the powerset.  Curious this is so much faster :)  I would expect it to be slower on the adversarial regexp, I think, because we pay the sort price on every freeze now, instead on insert/delete into this map?  Confusing :)




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