jpountz commented on code in PR #14714: URL: https://github.com/apache/lucene/pull/14714#discussion_r2111945886
########## lucene/core/src/java/org/apache/lucene/search/TopScoreDocCollector.java: ########## @@ -73,23 +65,22 @@ public ScoreMode scoreMode() { public LeafCollector getLeafCollector(LeafReaderContext context) throws IOException { final int docBase = context.docBase; final ScoreDoc after = this.after; - final float afterScore; + final int afterScore; Review Comment: Does it actually help to track scores as sortable ints rather than floats? I had assumed we'd only encode them if they're between the after score and the top score? ########## lucene/core/src/java/org/apache/lucene/search/DocScoreEncoder.java: ########## @@ -0,0 +1,81 @@ +/* + * 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.search; + +import org.apache.lucene.util.NumericUtils; + +/** + * An encoder do encode (doc, score) pair as a long whose sort order is same as {@code (o1, o2) -> + * Float.compare(o1.score, o2.score)).thenComparing(Comparator.comparingInt((ScoreDoc o) -> + * o.doc).reversed())} + * + * <p>Note that negative score is allowed but relationship between two codes encoded by negative + * scores is undefined. The only thing guaranteed is codes encoded from negative scores are smaller + * than codes encoded from non-negative scores. + */ +class DocScoreEncoder { + + static final long LEAST_COMPETITIVE_CODE = encode(Integer.MAX_VALUE, Float.NEGATIVE_INFINITY); + private static final int POS_INF_TO_SORTABLE_INT = scoreToSortableInt(Float.POSITIVE_INFINITY); + + static long encode(int docId, float score) { + return encodeIntScore(docId, scoreToSortableInt(score)); + } + + static long encodeIntScore(int docId, int score) { + return (((long) score) << 32) | (~docId & 0xFFFFFFFFL); + } + + static float toScore(long value) { + return sortableIntToScore(toIntScore(value)); + } + + static int toIntScore(long value) { + return (int) (value >>> 32); + } + + static int docId(long value) { + return (int) ~value; + } + + static int nextUp(int intScore) { + assert intScore <= POS_INF_TO_SORTABLE_INT; + int nextUp = Math.min(POS_INF_TO_SORTABLE_INT, intScore + 1); + assert nextUp == scoreToSortableInt(Math.nextUp(sortableIntToScore(intScore))); + return nextUp; + } + + /** + * Score is non-negative float so wo use floatToRawIntBits instead of {@link + * NumericUtils#floatToSortableInt}. We do not assert score >= 0 here to allow pass negative float + * to indicate totally non-competitive, e.g. {@link #LEAST_COMPETITIVE_CODE}. + */ Review Comment: This is a bit too subtle to my taste, could we either not have to deal with negative scores at all, or use NumericUtils#floatToSortableInt? FWIW, I believe that `LEAST_COMPETITIVE_CODE` could use a score of 0 since Integer.MAX_VALUE is not an allowed doc ID? -- 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. To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org 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