performance hack: use a hash cache for all hashes that are computed by a

byte array. If this hash is used in a HashMap (which is very often the
case) then this hack eliminates a lot of re-computations of the same
hash.
pull/1/head
Michael Peter Christen 13 years ago
parent f8a0cf6d7c
commit 10da7335ea

@ -133,9 +133,14 @@ public class CitationReference implements Reference, Serializable {
return false; return false;
} }
private int hashCache = Integer.MIN_VALUE; // if this is used in a compare method many times, a cache is useful
@Override @Override
public int hashCode() { public int hashCode() {
return ByteArray.hashCode(this.urlhash()); if (this.hashCache == Integer.MIN_VALUE) {
this.hashCache = ByteArray.hashCode(this.urlhash());
}
return this.hashCache;
} }
@Override @Override

@ -200,18 +200,21 @@ public final class ImageReferenceRow extends AbstractReference implements /*Imag
@Override @Override
public ImageReferenceRow clone() { public ImageReferenceRow clone() {
final byte[] b = new byte[urlEntryRow.objectsize]; final byte[] b = new byte[urlEntryRow.objectsize];
System.arraycopy(entry.bytes(), 0, b, 0, urlEntryRow.objectsize); System.arraycopy(this.entry.bytes(), 0, b, 0, urlEntryRow.objectsize);
return new ImageReferenceRow(b); return new ImageReferenceRow(b);
} }
@Override
public String toPropertyForm() { public String toPropertyForm() {
return entry.toPropertyForm('=', true, true, false, false); return this.entry.toPropertyForm('=', true, true, false, false);
} }
@Override
public Entry toKelondroEntry() { public Entry toKelondroEntry() {
return this.entry; return this.entry;
} }
@Override
public byte[] urlhash() { public byte[] urlhash() {
return this.entry.getColBytes(col_urlhash, true); return this.entry.getColBytes(col_urlhash, true);
} }
@ -220,6 +223,7 @@ public final class ImageReferenceRow extends AbstractReference implements /*Imag
return (int) this.entry.getColLong(col_lastModified); // this is the time in MicoDateDays format return (int) this.entry.getColLong(col_lastModified); // this is the time in MicoDateDays format
} }
@Override
public long lastModified() { public long lastModified() {
return MicroDate.reverseMicroDateDays((int) this.entry.getColLong(col_lastModified)); return MicroDate.reverseMicroDateDays((int) this.entry.getColLong(col_lastModified));
} }
@ -232,6 +236,7 @@ public final class ImageReferenceRow extends AbstractReference implements /*Imag
return (int) this.entry.getColLong(col_hitcount); return (int) this.entry.getColLong(col_hitcount);
} }
@Override
public Collection<Integer> positions() { public Collection<Integer> positions() {
return new ArrayList<Integer>(0); return new ArrayList<Integer>(0);
} }
@ -271,17 +276,24 @@ public final class ImageReferenceRow extends AbstractReference implements /*Imag
return toPropertyForm(); return toPropertyForm();
} }
@Override
public boolean isOlder(final Reference other) { public boolean isOlder(final Reference other) {
if (other == null) return false; if (other == null) return false;
if (this.lastModified() < other.lastModified()) return true; if (this.lastModified() < other.lastModified()) return true;
return false; return false;
} }
private int hashCache = Integer.MIN_VALUE; // if this is used in a compare method many times, a cache is useful
@Override @Override
public int hashCode() { public int hashCode() {
return ByteArray.hashCode(this.urlhash()); if (this.hashCache == Integer.MIN_VALUE) {
this.hashCache = ByteArray.hashCode(this.urlhash());
}
return this.hashCache;
} }
@Override
public void join(Reference oe) { public void join(Reference oe) {
throw new UnsupportedOperationException(""); throw new UnsupportedOperationException("");

@ -381,9 +381,14 @@ public class ImageReferenceVars extends AbstractReference implements ImageRefere
this.wordsintext = this.wordsintext + oe.wordsintext(); this.wordsintext = this.wordsintext + oe.wordsintext();
} }
private int hashCache = Integer.MIN_VALUE; // if this is used in a compare method many times, a cache is useful
@Override @Override
public int hashCode() { public int hashCode() {
return ByteArray.hashCode(this.urlHash); if (this.hashCache == Integer.MIN_VALUE) {
this.hashCache = ByteArray.hashCode(this.urlHash);
}
return this.hashCache;
} }
public void addPosition(int position) { public void addPosition(int position) {

@ -160,12 +160,14 @@ public class DigestURI extends MultiProtocolURI implements Serializable {
this.hash = null; this.hash = null;
} }
/* (non-Javadoc) private int hashCache = Integer.MIN_VALUE; // if this is used in a compare method many times, a cache is useful
* @see java.lang.Object#hashCode()
*/
@Override @Override
public int hashCode() { public int hashCode() {
return ByteArray.hashCode(hash()); if (this.hashCache == Integer.MIN_VALUE) {
this.hashCache = ByteArray.hashCode(hash());
}
return this.hashCache;
} }
public static final int flagTypeID(final String hash) { public static final int flagTypeID(final String hash) {

@ -215,6 +215,7 @@ public final class WordReferenceRow extends AbstractReference implements WordRef
this.out = new LinkedBlockingQueue<WordReferenceRow>(); this.out = new LinkedBlockingQueue<WordReferenceRow>();
for (int i = 0; i < concurrency; i++) { for (int i = 0; i < concurrency; i++) {
this.worker[i] = new Thread() { this.worker[i] = new Thread() {
@Override
public void run() { public void run() {
String s; String s;
try { try {
@ -290,22 +291,27 @@ public final class WordReferenceRow extends AbstractReference implements WordRef
return new WordReferenceRow(b); return new WordReferenceRow(b);
} }
@Override
public String toPropertyForm() { public String toPropertyForm() {
return this.entry.toPropertyForm('=', true, true, false, false); return this.entry.toPropertyForm('=', true, true, false, false);
} }
@Override
public Entry toKelondroEntry() { public Entry toKelondroEntry() {
return this.entry; return this.entry;
} }
@Override
public byte[] urlhash() { public byte[] urlhash() {
return this.entry.getColBytes(col_urlhash, true); return this.entry.getColBytes(col_urlhash, true);
} }
@Override
public int virtualAge() { public int virtualAge() {
return (int) this.entry.getColLong(col_lastModified); // this is the time in MicoDateDays format return (int) this.entry.getColLong(col_lastModified); // this is the time in MicoDateDays format
} }
@Override
public long lastModified() { public long lastModified() {
return MicroDate.reverseMicroDateDays((int) this.entry.getColLong(col_lastModified)); return MicroDate.reverseMicroDateDays((int) this.entry.getColLong(col_lastModified));
} }
@ -314,10 +320,12 @@ public final class WordReferenceRow extends AbstractReference implements WordRef
return MicroDate.reverseMicroDateDays((int) this.entry.getColLong(col_freshUntil)); return MicroDate.reverseMicroDateDays((int) this.entry.getColLong(col_freshUntil));
} }
@Override
public int hitcount() { public int hitcount() {
return (0xff & this.entry.getColByte(col_hitcount)); return (0xff & this.entry.getColByte(col_hitcount));
} }
@Override
public Collection<Integer> positions() { public Collection<Integer> positions() {
return new ArrayList<Integer>(0); return new ArrayList<Integer>(0);
} }
@ -327,54 +335,67 @@ public final class WordReferenceRow extends AbstractReference implements WordRef
return (int) this.entry.getColLong(col_posintext); return (int) this.entry.getColLong(col_posintext);
} }
@Override
public int posinphrase() { public int posinphrase() {
return (0xff & this.entry.getColByte(col_posinphrase)); return (0xff & this.entry.getColByte(col_posinphrase));
} }
@Override
public int posofphrase() { public int posofphrase() {
return (0xff & this.entry.getColByte(col_posofphrase)); return (0xff & this.entry.getColByte(col_posofphrase));
} }
@Override
public int wordsintext() { public int wordsintext() {
return (int) this.entry.getColLong(col_wordsInText); return (int) this.entry.getColLong(col_wordsInText);
} }
@Override
public int phrasesintext() { public int phrasesintext() {
return (int) this.entry.getColLong(col_phrasesInText); return (int) this.entry.getColLong(col_phrasesInText);
} }
@Override
public byte[] getLanguage() { public byte[] getLanguage() {
return this.entry.getColBytes(col_language, true); return this.entry.getColBytes(col_language, true);
} }
@Override
public char getType() { public char getType() {
return (char) this.entry.getColByte(col_doctype); return (char) this.entry.getColByte(col_doctype);
} }
@Override
public int wordsintitle() { public int wordsintitle() {
return (0xff & this.entry.getColByte(col_wordsInTitle)); return (0xff & this.entry.getColByte(col_wordsInTitle));
} }
@Override
public int llocal() { public int llocal() {
return (0xff & this.entry.getColByte(col_llocal)); return (0xff & this.entry.getColByte(col_llocal));
} }
@Override
public int lother() { public int lother() {
return (0xff & this.entry.getColByte(col_lother)); return (0xff & this.entry.getColByte(col_lother));
} }
@Override
public int urllength() { public int urllength() {
return (0xff & this.entry.getColByte(col_urlLength)); return (0xff & this.entry.getColByte(col_urlLength));
} }
@Override
public int urlcomps() { public int urlcomps() {
return (0xff & this.entry.getColByte(col_urlComps)); return (0xff & this.entry.getColByte(col_urlComps));
} }
@Override
public Bitfield flags() { public Bitfield flags() {
return new Bitfield(this.entry.getColBytes(col_flags, false)); return new Bitfield(this.entry.getColBytes(col_flags, false));
} }
@Override
public double termFrequency() { public double termFrequency() {
return (((double) hitcount()) / ((double) (wordsintext() + wordsintitle() + 1))); return (((double) hitcount()) / ((double) (wordsintext() + wordsintitle() + 1)));
} }
@ -393,11 +414,17 @@ public final class WordReferenceRow extends AbstractReference implements WordRef
return Base64Order.enhancedCoder.equal(urlhash(), other.urlhash()); return Base64Order.enhancedCoder.equal(urlhash(), other.urlhash());
} }
private int hashCache = Integer.MIN_VALUE; // if this is used in a compare method many times, a cache is useful
@Override @Override
public int hashCode() { public int hashCode() {
return ByteArray.hashCode(urlhash()); if (this.hashCache == Integer.MIN_VALUE) {
this.hashCache = ByteArray.hashCode(urlhash());
}
return this.hashCache;
} }
@Override
public void join(final Reference oe) { public void join(final Reference oe) {
throw new UnsupportedOperationException(""); throw new UnsupportedOperationException("");

@ -31,7 +31,6 @@ import java.util.Comparator;
import java.util.Queue; import java.util.Queue;
import java.util.concurrent.BlockingQueue; import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.Semaphore;
import net.yacy.cora.document.ASCII; import net.yacy.cora.document.ASCII;
import net.yacy.cora.document.UTF8; import net.yacy.cora.document.UTF8;
@ -394,9 +393,14 @@ public class WordReferenceVars extends AbstractReference implements WordReferenc
return Base64Order.enhancedCoder.equal(this.urlHash, other.urlHash); return Base64Order.enhancedCoder.equal(this.urlHash, other.urlHash);
} }
private int hashCache = Integer.MIN_VALUE; // if this is used in a compare method many times, a cache is useful
@Override @Override
public int hashCode() { public int hashCode() {
return ByteArray.hashCode(this.urlHash); if (this.hashCache == Integer.MIN_VALUE) {
this.hashCache = ByteArray.hashCode(this.urlHash);
}
return this.hashCache;
} }
@Override @Override

@ -44,7 +44,7 @@ import net.yacy.cora.order.ByteOrder;
public class ByteArray { public class ByteArray {
private final byte[] buffer; private final byte[] buffer;
private int hash; private final int hash;
public ByteArray(final byte[] bb) { public ByteArray(final byte[] bb) {
@ -53,7 +53,7 @@ public class ByteArray {
} }
public int length() { public int length() {
return buffer.length; return this.buffer.length;
} }
public byte[] asBytes() { public byte[] asBytes() {
@ -61,7 +61,7 @@ public class ByteArray {
} }
public byte readByte(final int pos) { public byte readByte(final int pos) {
return buffer[pos]; return this.buffer[pos];
} }
public static boolean startsWith(final byte[] buffer, final byte[] pattern) { public static boolean startsWith(final byte[] buffer, final byte[] pattern) {
@ -83,11 +83,14 @@ public class ByteArray {
return order.compare(this.buffer, aoffset, b.buffer, boffset, blength); return order.compare(this.buffer, aoffset, b.buffer, boffset, blength);
} }
private int hashCache = Integer.MIN_VALUE; // if this is used in a compare method many times, a cache is useful
@Override @Override
public int hashCode() { public int hashCode() {
if (this.hash != 0) return this.hash; if (this.hashCache == Integer.MIN_VALUE) {
this.hash = hashCode(this.buffer); this.hashCache = ByteArray.hashCode(this.buffer);
return this.hash; }
return this.hashCache;
} }
/** /**
@ -104,8 +107,8 @@ public class ByteArray {
@Override @Override
public boolean equals(Object other) { public boolean equals(Object other) {
ByteArray b = (ByteArray) other; ByteArray b = (ByteArray) other;
if (buffer == null && b == null) return true; if (this.buffer == null && b == null) return true;
if (buffer == null || b == null) return false; if (this.buffer == null || b == null) return false;
if (this.buffer.length != b.buffer.length) return false; if (this.buffer.length != b.buffer.length) return false;
int l = this.buffer.length; int l = this.buffer.length;
while (--l >= 0) if (this.buffer[l] != b.buffer[l]) return false; while (--l >= 0) if (this.buffer[l] != b.buffer[l]) return false;

@ -100,9 +100,14 @@ public class MediaSnippet implements Comparable<MediaSnippet>, Comparator<MediaS
if ((this.attr == null) || (this.attr.length() == 0)) this.attr = "_"; if ((this.attr == null) || (this.attr.length() == 0)) this.attr = "_";
} }
private int hashCache = Integer.MIN_VALUE; // if this is used in a compare method many times, a cache is useful
@Override @Override
public int hashCode() { public int hashCode() {
return ByteArray.hashCode(this.href.hash()); if (this.hashCache == Integer.MIN_VALUE) {
this.hashCache = ByteArray.hashCode(this.href.hash());
}
return this.hashCache;
} }
@Override @Override

@ -103,9 +103,13 @@ public class ResultEntry implements Comparable<ResultEntry>, Comparator<ResultEn
if ((p = this.alternative_urlname.indexOf('?')) > 0) this.alternative_urlname = this.alternative_urlname.substring(0, p); if ((p = this.alternative_urlname.indexOf('?')) > 0) this.alternative_urlname = this.alternative_urlname.substring(0, p);
} }
} }
private int hashCache = Integer.MIN_VALUE; // if this is used in a compare method many times, a cache is useful
@Override @Override
public int hashCode() { public int hashCode() {
return ByteArray.hashCode(this.urlentry.hash()); if (this.hashCache == Integer.MIN_VALUE) {
this.hashCache = ByteArray.hashCode(this.urlentry.hash());
}
return this.hashCache;
} }
@Override @Override
public boolean equals(final Object obj) { public boolean equals(final Object obj) {

@ -400,9 +400,14 @@ public class TextSnippet implements Comparable<TextSnippet>, Comparator<TextSnip
return o1.compareTo(o2); return o1.compareTo(o2);
} }
private int hashCache = Integer.MIN_VALUE; // if this is used in a compare method many times, a cache is useful
@Override @Override
public int hashCode() { public int hashCode() {
return ByteArray.hashCode(this.urlhash); if (this.hashCache == Integer.MIN_VALUE) {
this.hashCache = ByteArray.hashCode(this.urlhash);
}
return this.hashCache;
} }
@Override @Override

Loading…
Cancel
Save