Changeset e7c8d28


Ignore:
Timestamp:
Jul 22, 2013 11:07:37 PM (7 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
f551873
Parents:
dff357a
Message:
  • HashComparator?:
    • Don't pollute Hash cache with hashes
    • cleanups, less object churn
File:
1 edited

Legend:

Unmodified
Added
Removed
  • router/java/src/net/i2p/router/tunnel/pool/TunnelPeerSelector.java

    rdff357a re7c8d28  
    529529     *     d((H(l+h), h) - d(H(r+h), h)
    530530     */
    531     private static class HashComparator implements Comparator {
    532         private Hash _hash;
    533 
     531    private static class HashComparator implements Comparator<Hash> {
     532        private final Hash _hash, tmp;
     533        private final byte[] data;
     534
     535        /** not thread safe */
    534536        private HashComparator(Hash h) {
    535537            _hash = h;
    536         }
    537         public int compare(Object l, Object r) {
    538             byte[] data = new byte[2*Hash.HASH_LENGTH];
     538            tmp = new Hash(new byte[Hash.HASH_LENGTH]);
     539            data = new byte[2*Hash.HASH_LENGTH];
    539540            System.arraycopy(_hash.getData(), 0, data, Hash.HASH_LENGTH, Hash.HASH_LENGTH);
    540             System.arraycopy(((Hash) l).getData(), 0, data, 0, Hash.HASH_LENGTH);
    541             Hash lh = SHA256Generator.getInstance().calculateHash(data);
    542             System.arraycopy(((Hash) r).getData(), 0, data, 0, Hash.HASH_LENGTH);
    543             Hash rh = SHA256Generator.getInstance().calculateHash(data);
    544             BigInteger ll = HashDistance.getDistance(_hash, lh);
    545             BigInteger rr = HashDistance.getDistance(_hash, rh);
     541        }
     542
     543        public int compare(Hash l, Hash r) {
     544            System.arraycopy(l.getData(), 0, data, 0, Hash.HASH_LENGTH);
     545            byte[] tb = tmp.getData();
     546            // don't use caching version of calculateHash()
     547            SHA256Generator.getInstance().calculateHash(data, 0, 2*Hash.HASH_LENGTH, tb, 0);
     548            BigInteger ll = HashDistance.getDistance(_hash, tmp);
     549            System.arraycopy(r.getData(), 0, data, 0, Hash.HASH_LENGTH);
     550            SHA256Generator.getInstance().calculateHash(data, 0, 2*Hash.HASH_LENGTH, tb, 0);
     551            BigInteger rr = HashDistance.getDistance(_hash, tmp);
    546552            return ll.compareTo(rr);
    547553        }
Note: See TracChangeset for help on using the changeset viewer.