applied norm caching path from Wolfgang

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@167958 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Erik Hatcher 2005-05-03 19:01:58 +00:00
parent 2a37a3e820
commit f94ebdb41e
1 changed files with 23 additions and 8 deletions

View File

@ -886,13 +886,28 @@ public class MemoryIndex {
this.searcher = searcher; this.searcher = searcher;
} }
public byte[] norms(String fieldName) { /** performance hack: cache norms to avoid repeated expensive calculations */
if (DEBUG) System.err.println("MemoryIndexReader.norms: " + fieldName); private byte[] cachedNorms;
Info info = getInfo(fieldName); private String cachedFieldName;
int numTokens = info != null ? info.numTokens : 0; private Similarity cachedSimilarity;
byte norm = Similarity.encodeNorm(getSimilarity().lengthNorm(fieldName, numTokens));
return new byte[] {norm}; public byte[] norms(String fieldName) {
} byte[] norms = cachedNorms;
Similarity sim = getSimilarity();
if (fieldName != cachedFieldName || sim != cachedSimilarity) { // not cached?
Info info = getInfo(fieldName);
int numTokens = info != null ? info.numTokens : 0;
float n = sim.lengthNorm(fieldName, numTokens);
byte norm = Similarity.encodeNorm(n);
norms = new byte[] {norm};
cachedNorms = norms;
cachedFieldName = fieldName;
cachedSimilarity = sim;
if (DEBUG) System.err.println("MemoryIndexReader.norms: " + fieldName + ":" + n + ":" + norm + ":" + numTokens);
}
return norms;
}
public void norms(String fieldName, byte[] bytes, int offset) { public void norms(String fieldName, byte[] bytes, int offset) {
if (DEBUG) System.err.println("MemoryIndexReader.norms: " + fieldName + "*"); if (DEBUG) System.err.println("MemoryIndexReader.norms: " + fieldName + "*");