lucene-java-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ehatc...@apache.org
Subject svn commit: r167958 - /lucene/java/trunk/contrib/memory/src/java/org/apache/lucene/index/memory/MemoryIndex.java
Date Tue, 03 May 2005 19:01:58 GMT
Author: ehatcher
Date: Tue May  3 12:01:58 2005
New Revision: 167958

URL: http://svn.apache.org/viewcvs?rev=167958&view=rev
Log:
applied norm caching path from Wolfgang

Modified:
    lucene/java/trunk/contrib/memory/src/java/org/apache/lucene/index/memory/MemoryIndex.java

Modified: lucene/java/trunk/contrib/memory/src/java/org/apache/lucene/index/memory/MemoryIndex.java
URL: http://svn.apache.org/viewcvs/lucene/java/trunk/contrib/memory/src/java/org/apache/lucene/index/memory/MemoryIndex.java?rev=167958&r1=167957&r2=167958&view=diff
==============================================================================
--- lucene/java/trunk/contrib/memory/src/java/org/apache/lucene/index/memory/MemoryIndex.java
(original)
+++ lucene/java/trunk/contrib/memory/src/java/org/apache/lucene/index/memory/MemoryIndex.java
Tue May  3 12:01:58 2005
@@ -886,14 +886,29 @@
 			this.searcher = searcher;
 		}
 		
-		public byte[] norms(String fieldName) {
-			if (DEBUG) System.err.println("MemoryIndexReader.norms: " + fieldName);
-			Info info = getInfo(fieldName);
-			int numTokens = info != null ? info.numTokens : 0;
-			byte norm = Similarity.encodeNorm(getSimilarity().lengthNorm(fieldName, numTokens));
-			return new byte[] {norm};
-		}
-	
+                /** performance hack: cache norms to avoid repeated expensive calculations
*/
+                private byte[] cachedNorms;
+                private String cachedFieldName;
+                private Similarity cachedSimilarity;
+	    
+                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) {
 			if (DEBUG) System.err.println("MemoryIndexReader.norms: " + fieldName + "*");
 			byte[] norms = norms(fieldName);



Mime
View raw message