neoremind commented on a change in pull request #91: URL: https://github.com/apache/lucene/pull/91#discussion_r617406029
########## File path: lucene/core/src/java/org/apache/lucene/util/StableMSBRadixSorter.java ########## @@ -0,0 +1,91 @@ +/* + * 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; + +/** + * Stable radix sorter for variable-length strings. It has to check data beforehand to see if stable + * sort can be enabled or not. If not applicable, it works the same way as its parent class {@link + * MSBRadixSorter}. + * + * @lucene.internal + */ +public abstract class StableMSBRadixSorter extends MSBRadixSorter { + + protected boolean useStableSort; + + public StableMSBRadixSorter(int maxLength) { + super(maxLength); + } + + /** Check if stable sort can be enabled or not. */ + protected boolean isEnableStableSort(int from, int to) { + return false; + } + + /** + * If stable sort is applicable, do some closure logic, which might update some of the internal + * variable. + */ + protected void doClosureIfStableSortEnabled() {} + + /** Assign the from-th value to to-th position in another array which used temporarily. */ + protected void assign(int from, int to) { + throw new UnsupportedOperationException("not implement"); + } + + /** Finalize assign operation, to switch array. */ + protected void finalizeAssign(int from, int to) { + throw new UnsupportedOperationException("not implement"); + } + + @Override + public void sort(int from, int to) { + checkRange(from, to); + useStableSort = isEnableStableSort(from, to); + if (useStableSort) { + doClosureIfStableSortEnabled(); + } + sort(from, to, 0, 0); + } + + @Override + protected void reorder(int from, int to, int[] startOffsets, int[] endOffsets, int k) { + if (useStableSort) { + stableReorder(from, to, startOffsets, endOffsets, k); + } else { + super.reorder(from, to, startOffsets, endOffsets, k); + } Review comment: There is no silver bullet, the stable reorder runs faster, I think the reason is that it makes less swapping, but use extra space to store docid. ########## File path: lucene/core/src/java/org/apache/lucene/util/StableMSBRadixSorter.java ########## @@ -0,0 +1,91 @@ +/* + * 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; + +/** + * Stable radix sorter for variable-length strings. It has to check data beforehand to see if stable + * sort can be enabled or not. If not applicable, it works the same way as its parent class {@link + * MSBRadixSorter}. + * + * @lucene.internal + */ +public abstract class StableMSBRadixSorter extends MSBRadixSorter { + + protected boolean useStableSort; + + public StableMSBRadixSorter(int maxLength) { + super(maxLength); + } + + /** Check if stable sort can be enabled or not. */ + protected boolean isEnableStableSort(int from, int to) { + return false; + } + + /** + * If stable sort is applicable, do some closure logic, which might update some of the internal + * variable. + */ + protected void doClosureIfStableSortEnabled() {} + + /** Assign the from-th value to to-th position in another array which used temporarily. */ + protected void assign(int from, int to) { + throw new UnsupportedOperationException("not implement"); + } + + /** Finalize assign operation, to switch array. */ + protected void finalizeAssign(int from, int to) { + throw new UnsupportedOperationException("not implement"); + } + + @Override + public void sort(int from, int to) { + checkRange(from, to); + useStableSort = isEnableStableSort(from, to); + if (useStableSort) { + doClosureIfStableSortEnabled(); + } + sort(from, to, 0, 0); + } + + @Override + protected void reorder(int from, int to, int[] startOffsets, int[] endOffsets, int k) { + if (useStableSort) { + stableReorder(from, to, startOffsets, endOffsets, k); + } else { + super.reorder(from, to, startOffsets, endOffsets, k); + } Review comment: There is no silver bullet, the stable reorder runs faster, I think the reason is that it makes less swapping, but use extra temp space to store docid. -- 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