Distributed Peer-to-Peer Web Search Engine and Intranet Search Appliance
Java decentralizedintranet-searchintranet-search-enginep2pp2p-networkprivacyprivacy-protectionsearch-enginesearchenginewebweb-search-enginewebsearchwebseearch-engine
enhanced kelondroCollection performance
orbiter5d2cd2652965
diff --git a/source/de/anomic/kelondro/kelondroAbstractOrder.java b/source/de/anomic/kelondro/kelondroAbstractOrder.javaindex 0f30537d3..613ff48fe 100644--- a/source/de/anomic/kelondro/kelondroAbstractOrder.java+++ b/source/de/anomic/kelondro/kelondroAbstractOrder.java@@ -74,20 +74,6 @@ public abstract class kelondroAbstractOrder implements kelondroOrder {throw new IllegalArgumentException("Object type or Object type combination not supported: a=" + a + ", b=" + b);}- public int compare(byte[] a, byte[] b, int boffset, int blength) {- int l = Math.min(a.length, blength);- byte[] bb = new byte[l];- System.arraycopy(b, boffset, bb, 0, l);- return compare(a, bb);- }-- public int compare(byte[] a, int aoffset, int alength, byte[] b, int boffset, int blength) {- int l = Math.min(alength, blength);- byte[] aa = new byte[l];- System.arraycopy(a, aoffset, aa, 0, l);- return compare(aa, b, boffset, blength);- }-public byte[] zero() {return zero;}diff --git a/source/de/anomic/kelondro/kelondroBase64Order.java b/source/de/anomic/kelondro/kelondroBase64Order.javaindex d87c663fe..2ace58c4f 100644--- a/source/de/anomic/kelondro/kelondroBase64Order.java+++ b/source/de/anomic/kelondro/kelondroBase64Order.java@@ -260,35 +260,39 @@ public class kelondroBase64Order extends kelondroAbstractOrder implements kelond}public final int compare(byte[] a, byte[] b) {- return (asc) ? compare0(a, b) : compare0(b, a);+ return (asc) ? compare0(a, 0, a.length, b, 0, b.length) : compare0(b, 0, b.length, a, 0, a.length);}- public final int compare0(byte[] a, byte[] b) {- if (zero == null) return compares(a, b);+ public final int compare(byte[] a, int aoffset, int alength, byte[] b, int boffset, int blength) {+ return (asc) ? compare0(a, aoffset, alength, b, boffset, blength) : compare0(b, boffset, blength, a, aoffset, alength);+ }++ public final int compare0(byte[] a, int aoffset, int alength, byte[] b, int boffset, int blength) {+ if (zero == null) return compares(a, aoffset, alength, b, boffset, blength);// we have an artificial start point. check all combinations- int az = compares(a, zero); // -1 if a < z; 0 if a == z; 1 if a > z- int bz = compares(b, zero); // -1 if b < z; 0 if b == z; 1 if b > z+ int az = compares(a, aoffset, alength, zero, 0, zero.length); // -1 if a < z; 0 if a == z; 1 if a > z+ int bz = compares(b, boffset, blength, zero, 0, zero.length); // -1 if b < z; 0 if b == z; 1 if b > zif ((az == 0) && (bz == 0)) return 0;if (az == 0) return -1;if (bz == 0) return 1;- if (az == bz) return compares(a, b);+ if (az == bz) return compares(a, aoffset, alength, b, boffset, blength);return bz;}- public final int compares(byte[] a, byte[] b) {+ public final int compares(byte[] a, int aoffset, int alength, byte[] b, int boffset, int blength) {int i = 0;- final int al = a.length;- final int bl = b.length;+ final int al = Math.min(alength, a.length - aoffset);+ final int bl = Math.min(blength, b.length - boffset);final int len = (al > bl) ? bl : al;while (i < len) {- if (ahpla[a[i]] > ahpla[b[i]]) return 1;- if (ahpla[a[i]] < ahpla[b[i]]) return -1;+ if (ahpla[a[i + aoffset]] > ahpla[b[i + boffset]]) return 1;+ if (ahpla[a[i + aoffset]] < ahpla[b[i + boffset]]) return -1;// else the bytes are equal and it may go on yet undecidedi++;}// check if we have a zero-terminated equality- if ((i == al) && (i < bl) && (b[i] == 0)) return 0;- if ((i == bl) && (i < al) && (a[i] == 0)) return 0;+ if ((i == al) && (i < bl) && (b[i + boffset] == 0)) return 0;+ if ((i == bl) && (i < al) && (a[i + aoffset] == 0)) return 0;// no, decide by lengthif (al > bl) return 1;if (al < bl) return -1;diff --git a/source/de/anomic/kelondro/kelondroCollection.java b/source/de/anomic/kelondro/kelondroCollection.javaindex c0090e39b..e269a051b 100644--- a/source/de/anomic/kelondro/kelondroCollection.java+++ b/source/de/anomic/kelondro/kelondroCollection.java@@ -41,7 +41,6 @@package de.anomic.kelondro;-import java.util.Comparator;import java.util.Iterator;public class kelondroCollection {@@ -215,7 +214,8 @@ public class kelondroCollection {if (this.order == null) return iterativeSearch(a);// check if a re-sorting make sense- if (this.chunkcount - this.sortbound > 3000) sort();+ if (this.chunkcount - this.sortbound > 800) sort();+ //if ((this.chunkcount - this.sortbound) / (this.chunkcount + 1) * 100 > 20) sort();// first try to find in sorted areaint p = iterativeSearch(a);@@ -260,7 +260,7 @@ public class kelondroCollection {public void sort() {if (this.sortbound == this.chunkcount) return; // this is already sorted- System.out.println("SORT");+ //System.out.println("SORT");if (this.sortbound > 1) qsort(0, this.sortbound, this.chunkcount);else qsort(0, this.chunkcount);this.sortbound = this.chunkcount;@@ -384,16 +384,14 @@ public class kelondroCollection {public int compare(byte[] a, int chunknumber) {assert (chunknumber < chunkcount);int l = Math.min(a.length, chunksize);- return this.order.compare(a, chunkcache, chunknumber * chunksize, l);+ return this.order.compare(a, 0, a.length, chunkcache, chunknumber * chunksize, l);}public int compare(int i, int j) {// this can be enhancedassert (i < chunkcount);assert (j < chunkcount);- byte[] a = new byte[chunksize];- System.arraycopy(chunkcache, i * chunksize, a, 0, chunksize);- return compare(a, j);+ return this.order.compare(chunkcache, i * chunksize, chunksize, chunkcache, j * chunksize, chunksize);}public static void main(String[] args) {@@ -423,7 +421,7 @@ public class kelondroCollection {long start = System.currentTimeMillis();long t, d = 0;byte[] w;- for (long k = 0; k < 100000; k++) {+ for (long k = 0; k < 200000; k++) {t = System.currentTimeMillis();w = ("a" + Long.toString((t % 13775) + k)).getBytes();if (c.get(w) == null) c.add(w); else d++;diff --git a/source/de/anomic/kelondro/kelondroNaturalOrder.java b/source/de/anomic/kelondro/kelondroNaturalOrder.javaindex 7af4f7962..fa171a929 100644--- a/source/de/anomic/kelondro/kelondroNaturalOrder.java+++ b/source/de/anomic/kelondro/kelondroNaturalOrder.java@@ -132,27 +132,53 @@ public class kelondroNaturalOrder extends kelondroAbstractOrder implements kelon// two arrays are also equal if one array is a subset of the other's array// with filled-up char(0)-valuespublic final int compare(byte[] a, byte[] b) {- return (asc) ? compare0(a, b) : compare0(b, a);+ return (asc) ? compare0(a, 0, a.length, b, 0, b.length) : compare0(b, 0, b.length, a, 0, a.length);}- public final int compare0(byte[] a, byte[] b) {- if (zero == null) return compares(a, b);+ public final int compare(byte[] a, int aoffset, int alength, byte[] b, int boffset, int blength) {+ return (asc) ? compare0(a, aoffset, alength, b, boffset, blength) : compare0(b, boffset, blength, a, aoffset, alength);+ }++ public final int compare0(byte[] a, int aoffset, int alength, byte[] b, int boffset, int blength) {+ if (zero == null) return compares(a, aoffset, alength, b, boffset, blength);// we have an artificial start point. check all combinations- int az = compares(a, zero); // -1 if a < z; 0 if a == z; 1 if a > z- int bz = compares(b, zero); // -1 if b < z; 0 if b == z; 1 if b > z+ int az = compares(a, aoffset, alength, zero, 0, zero.length); // -1 if a < z; 0 if a == z; 1 if a > z+ int bz = compares(b, boffset, blength, zero, 0, zero.length); // -1 if b < z; 0 if b == z; 1 if b > zif ((az == 0) && (bz == 0)) return 0;if (az == 0) return -1;if (bz == 0) return 1;- if (az == bz) return compares(a, b);+ if (az == bz) return compares(a, aoffset, alength, b, boffset, blength);return bz;}-+public static final boolean equal(byte[] a, byte[] b) {if ((a == null) && (b == null)) return true;if ((a == null) || (b == null)) return false;- return compares(a, b) == 0;+ return compares(a, 0, a.length, b, 0, b.length) == 0;}-++ public static final int compares(byte[] a, int aoffset, int alength, byte[] b, int boffset, int blength) {+ int i = 0;+ final int al = Math.min(alength, a.length - aoffset);+ final int bl = Math.min(blength, b.length - boffset);+ final int len = (al > bl) ? bl : al;+ while (i < len) {+ if (a[i + aoffset] > b[i + boffset]) return 1;+ if (a[i + aoffset] < b[i + boffset]) return -1;+ // else the bytes are equal and it may go on yet undecided+ i++;+ }+ // check if we have a zero-terminated equality+ if ((i == al) && (i < bl) && (b[i + boffset] == 0)) return 0;+ if ((i == bl) && (i < al) && (a[i + aoffset] == 0)) return 0;+ // no, decide by length+ if (al > bl) return 1;+ if (al < bl) return -1;+ // no, they are equal+ return 0;+ }++ /*public static final int compares(byte[] a, byte[] b) {int i = 0;final int al = a.length;@@ -173,7 +199,7 @@ public class kelondroNaturalOrder extends kelondroAbstractOrder implements kelon// no, they are equalreturn 0;}-+ */public static void main(String[] args) {byte[] t = new byte[12];for (int i = 0; i < 12; i++) t[i] = (byte) 255;diff --git a/source/de/anomic/kelondro/kelondroOrder.java b/source/de/anomic/kelondro/kelondroOrder.javaindex 27deaff6b..e7aa90682 100644--- a/source/de/anomic/kelondro/kelondroOrder.java+++ b/source/de/anomic/kelondro/kelondroOrder.java@@ -61,8 +61,6 @@ public interface kelondroOrder extends Comparator {public int compare(byte[] a, byte[] b);- public int compare(byte[] a, byte[] b, int boffset, int blength);-public int compare(byte[] a, int aoffset, int alength, byte[] b, int boffset, int blength);public byte[] zero(); // returns the zero point of the Ordering; null if not defined