Skip to content

Commit

Permalink
Merge remote-tracking branch 'bertm/no-intern-byte-array' into next
Browse files Browse the repository at this point in the history
  • Loading branch information
ArneBab committed Nov 30, 2024
2 parents c873c45 + f3edebf commit 99536f7
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 78 deletions.
29 changes: 7 additions & 22 deletions src/freenet/keys/ClientCHK.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,7 @@ public void writeRawBinaryKey(DataOutputStream dos) throws IOException {
dos.write(routingKey);
dos.write(cryptoKey);
}

static byte[] lastExtra;


public byte[] getExtra() {
return getExtra(cryptoAlgorithm, compressionAlgorithm, controlDocument);
}
Expand All @@ -177,32 +175,19 @@ public static byte[] getExtra(byte cryptoAlgorithm, short compressionAlgorithm,
extra[2] = (byte) (controlDocument ? 2 : 0);
extra[3] = (byte) (compressionAlgorithm >> 8);
extra[4] = (byte) compressionAlgorithm;
byte[] last = lastExtra;
// No synchronization required IMHO
if(Arrays.equals(last, extra)) return last;
assert(extra.length == EXTRA_LENGTH);
lastExtra = extra;
return extra;
}

public static byte getCryptoAlgorithmFromExtra(byte[] extra) {
return extra[1];
}

static HashSet<ByteArrayWrapper> standardExtras = new HashSet<ByteArrayWrapper>();
static {
for(byte cryptoAlgorithm = Key.ALGO_AES_PCFB_256_SHA256; cryptoAlgorithm <= Key.ALGO_AES_CTR_256_SHA256; cryptoAlgorithm++) {
for(short compressionAlgorithm = -1; compressionAlgorithm <= (short)(COMPRESSOR_TYPE.countCompressors()); compressionAlgorithm++) {
standardExtras.add(new ByteArrayWrapper(getExtra(cryptoAlgorithm, compressionAlgorithm, true)));
standardExtras.add(new ByteArrayWrapper(getExtra(cryptoAlgorithm, compressionAlgorithm, false)));
}
}
}


/**
* @return the provided argument
* @deprecated mutable data cannot safely be interned
*/
@Deprecated
public static byte[] internExtra(byte[] extra) {
for(ByteArrayWrapper baw : standardExtras) {
if(Arrays.equals(baw.get(), extra)) return baw.get();
}
return extra;
}

Expand Down
9 changes: 5 additions & 4 deletions src/freenet/keys/ClientSSK.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,12 @@ protected static byte[] getExtraBytes(byte cryptoAlgorithm) {
extra[4] = (byte) KeyBlock.HASH_SHA256;
return extra;
}

static final byte[] STANDARD_EXTRA = getExtraBytes(Key.ALGO_AES_PCFB_256_SHA256);


/**
* @return the provided argument
* @deprecated mutable data cannot safely be interned
*/
public static byte[] internExtra(byte[] buf) {
if(Arrays.equals(buf, STANDARD_EXTRA)) return STANDARD_EXTRA;
return buf;
}

Expand Down
35 changes: 7 additions & 28 deletions src/freenet/keys/FreenetURI.java
Original file line number Diff line number Diff line change
Expand Up @@ -223,35 +223,14 @@ public FreenetURI(FreenetURI uri) {
this.suggestedEdition = uri.suggestedEdition;
if(logDEBUG) Logger.debug(this, "Copied: "+toString()+" from "+uri.toString(), new Exception("debug"));
}

boolean noCacheURI = false;

/** Optimize for memory. */

/**
* @return this FreenetURI instance
* @deprecated mutable data cannot safely be interned
*/
@Deprecated
public FreenetURI intern() {
boolean changedAnything = false;
byte[] x = extra;
if(keyType.equals("CHK"))
x = ClientCHK.internExtra(x);
else
x = ClientSSK.internExtra(x);
if(x != extra) changedAnything = true;
String[] newMetaStr = null;
if(metaStr != null) {
newMetaStr = new String[metaStr.length];
for(int i=0;i<metaStr.length;i++) {
newMetaStr[i] = metaStr[i].intern();
if(metaStr[i] != newMetaStr[i]) changedAnything = true;
}
}
String dn = docName == null ? null : docName.intern();
if(dn != docName) changedAnything = true;
if(!changedAnything) {
noCacheURI = true;
return this;
}
FreenetURI u = new FreenetURI(keyType, dn, newMetaStr, routingKey, cryptoKey, extra, suggestedEdition);
u.noCacheURI = true;
return u;
return this;
}

public FreenetURI(String keyType, String docName) {
Expand Down
42 changes: 18 additions & 24 deletions src/freenet/support/ByteArrayWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,24 @@
*/
public class ByteArrayWrapper implements Comparable<ByteArrayWrapper> {

private final byte[] buf;
private int hashCode;
private final byte[] data;
private final int hashCode;

public static final Comparator<ByteArrayWrapper> FAST_COMPARATOR = new Comparator<ByteArrayWrapper>() {

@Override
public int compare(ByteArrayWrapper o1, ByteArrayWrapper o2) {
if(o1.hashCode > o2.hashCode) return 1;
if(o1.hashCode < o2.hashCode) return -1;
return o1.compareTo(o2);
}

};
public static final Comparator<ByteArrayWrapper> FAST_COMPARATOR = Comparator
.comparingInt(ByteArrayWrapper::hashCode)
.thenComparing(Comparator.naturalOrder());

public ByteArrayWrapper(byte[] data) {
buf = data;
hashCode = Fields.hashCode(buf);
this.data = Arrays.copyOf(data, data.length);
this.hashCode = Arrays.hashCode(this.data);
}

@Override
public boolean equals(Object o) {
if(o instanceof ByteArrayWrapper) {
ByteArrayWrapper b = (ByteArrayWrapper) o;
if(b.buf == buf) return true;
return Arrays.equals(b.buf, buf);
public boolean equals(Object other) {
if (this == other) return true;
if (other instanceof ByteArrayWrapper) {
ByteArrayWrapper b = (ByteArrayWrapper) other;
return this.hashCode == b.hashCode && Arrays.equals(this.data, b.data);
}
return false;
}
Expand All @@ -46,14 +39,15 @@ public int hashCode() {
return hashCode;
}

/** DO NOT MODIFY THE RETURNED DATA! */
public byte[] get() {
return buf;
return Arrays.copyOf(data, data.length);
}

@Override
public int compareTo(ByteArrayWrapper arg) {
if(this == arg) return 0;
return Fields.compareBytes(buf, arg.buf);
public int compareTo(ByteArrayWrapper other) {
if (this == other) {
return 0;
}
return Fields.compareBytes(this.data, other.data);
}
}

0 comments on commit 99536f7

Please sign in to comment.