Changeset c48aca8


Ignore:
Timestamp:
Sep 2, 2012 12:31:08 PM (7 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
c965a3d
Parents:
4360284
Message:

ByteCache?: Move all 16/32 byte users to SimpleByteCache?;
increase SimpleByteCache? default size.

Files:
10 edited

Legend:

Unmodified
Added
Removed
  • core/java/src/net/i2p/crypto/CryptixAESEngine.java

    r4360284 rc48aca8  
    2222import net.i2p.data.DataHelper;
    2323import net.i2p.data.SessionKey;
    24 import net.i2p.util.ByteCache;
    2524import net.i2p.util.Log;
     25import net.i2p.util.SimpleByteCache;
    2626
    2727/**
     
    3838    // keys are now cached in the SessionKey objects
    3939    //private CryptixAESKeyCache _cache;
    40    
    41     private static final ByteCache _prevCache = ByteCache.getInstance(16, 16);
    4240   
    4341/**** see comments for main() below
     
    167165        if (length % 16 != 0) numblock++;
    168166
    169         ByteArray prevA = _prevCache.acquire();
    170         byte prev[] = prevA.getData();
    171         ByteArray curA = _prevCache.acquire();
    172         byte cur[] = curA.getData();
     167        byte prev[] = SimpleByteCache.acquire(16);
     168        byte cur[] = SimpleByteCache.acquire(16);
    173169        System.arraycopy(iv, ivOffset, prev, 0, 16);
    174170       
     
    191187         */
    192188       
    193         _prevCache.release(prevA);
    194         _prevCache.release(curA);
     189        SimpleByteCache.release(prev);
     190        SimpleByteCache.release(cur);
    195191    }
    196192   
  • core/java/src/net/i2p/data/SDSCache.java

    r4360284 rc48aca8  
    9999
    100100    /**
     101     *  WARNING - If the SDS is found in the cache, the passed-in
     102     *  byte array will be returned to the SimpleByteCache for reuse.
     103     *  Do NOT save a reference to the passed-in data, or use or modify it,
     104     *  after this call.
     105     *
    101106     *  @param data non-null, the byte array for the SimpleDataStructure
    102107     *  @return the cached value if available, otherwise
  • core/java/src/net/i2p/util/ByteCache.java

    r4360284 rc48aca8  
    1414 * should be held onto as long as the  data referenced in it is needed.
    1515 *
     16 * For small arrays where the management of valid bytes in ByteArray
     17 * and prezeroing isn't required, use SimpleByteArray instead.
     18 *
    1619 * Heap size control - survey of usage (April 2010) :
    1720 *
    1821 *  <pre>
    1922        Size    Max     MaxMem  From
    20 
    21         16      16      256     CryptixAESEngine
    22         16      32      512     BloomFilterIVValidator
    23         16      64      1K      UDP PacketBuilder
    24         16      128     2K      tunnel HopProcessor
    25         16      128     2K      tunnel TrivialPreprocessor
    26         16      128     2K      tunnel InboundEndpointProcessor
    27         16      128     2K      tunnel OutboundGatewayProcessor
    28 
    29         32      64      2K      UDP PacketBuilder
    30         32      128     4K      tunnel TrivialPreprocessor
    3123
    3224        1K      32      32K     tunnel TrivialPreprocessor
  • core/java/src/net/i2p/util/SimpleByteCache.java

    r4360284 rc48aca8  
    1919    private static final Map<Integer, SimpleByteCache> _caches = new ConcurrentHashMap(8);
    2020
    21     private static final int DEFAULT_SIZE = 16;
     21    private static final int DEFAULT_SIZE = 64;
    2222
    2323    /** up to this, use ABQ to minimize object churn and for performance; above this, use LBQ for two locks */
  • router/java/src/net/i2p/router/transport/udp/PacketBuilder.java

    r4360284 rc48aca8  
    1717import net.i2p.data.SessionKey;
    1818import net.i2p.data.Signature;
    19 import net.i2p.util.ByteCache;
    2019import net.i2p.util.Addresses;
    2120import net.i2p.util.Log;
     21import net.i2p.util.SimpleByteCache;
    2222
    2323/**
     
    103103    private final UDPTransport _transport;
    104104   
    105     private static final ByteCache _ivCache = ByteCache.getInstance(64, UDPPacket.IV_SIZE);
    106     private static final ByteCache _hmacCache = ByteCache.getInstance(64, Hash.HASH_LENGTH);
    107 
    108105    /**
    109106     *  For debugging and stats only - does not go out on the wire.
     
    608605        // ok, now the full data is in there, but we also need to encrypt
    609606        // the signature, which means we need the IV
    610         ByteArray iv = _ivCache.acquire();
    611         _context.random().nextBytes(iv.getData());
     607        byte[] iv = SimpleByteCache.acquire(UDPPacket.IV_SIZE);
     608        _context.random().nextBytes(iv);
    612609       
    613610        int encrWrite = Signature.SIGNATURE_BYTES + 8;
    614611        int sigBegin = off - encrWrite;
    615         _context.aes().encrypt(data, sigBegin, data, sigBegin, state.getCipherKey(), iv.getData(), encrWrite);
     612        _context.aes().encrypt(data, sigBegin, data, sigBegin, state.getCipherKey(), iv, encrWrite);
    616613       
    617614        // pad up so we're on the encryption boundary
     
    621618        authenticate(packet, ourIntroKey, ourIntroKey, iv);
    622619        setTo(packet, to, state.getSentPort());
    623         _ivCache.release(iv);
     620        SimpleByteCache.release(iv);
    624621        packet.setMessageType(TYPE_CREAT);
    625622        return packet;
     
    12911288     */
    12921289    private void authenticate(UDPPacket packet, SessionKey cipherKey, SessionKey macKey) {
    1293         ByteArray iv = _ivCache.acquire();
    1294         _context.random().nextBytes(iv.getData());
     1290        byte[] iv = SimpleByteCache.acquire(UDPPacket.IV_SIZE);
     1291        _context.random().nextBytes(iv);
    12951292        authenticate(packet, cipherKey, macKey, iv);
    1296         _ivCache.release(iv);
     1293        SimpleByteCache.release(iv);
    12971294    }
    12981295   
     
    13091306     * @param iv IV to deliver
    13101307     */
    1311     private void authenticate(UDPPacket packet, SessionKey cipherKey, SessionKey macKey, ByteArray iv) {
     1308    private void authenticate(UDPPacket packet, SessionKey cipherKey, SessionKey macKey, byte[] iv) {
    13121309        long before = System.currentTimeMillis();
    13131310        int encryptOffset = packet.getPacket().getOffset() + UDPPacket.IV_SIZE + UDPPacket.MAC_SIZE;
    13141311        int encryptSize = packet.getPacket().getLength() - UDPPacket.IV_SIZE - UDPPacket.MAC_SIZE - packet.getPacket().getOffset();
    13151312        byte data[] = packet.getPacket().getData();
    1316         _context.aes().encrypt(data, encryptOffset, data, encryptOffset, cipherKey, iv.getData(), encryptSize);
     1313        _context.aes().encrypt(data, encryptOffset, data, encryptOffset, cipherKey, iv, encryptSize);
    13171314       
    13181315        // ok, now we need to prepare things for the MAC, which requires reordering
     
    13201317        System.arraycopy(data, encryptOffset, data, off, encryptSize);
    13211318        off += encryptSize;
    1322         System.arraycopy(iv.getData(), 0, data, off, UDPPacket.IV_SIZE);
     1319        System.arraycopy(iv, 0, data, off, UDPPacket.IV_SIZE);
    13231320        off += UDPPacket.IV_SIZE;
    13241321        DataHelper.toLong(data, off, 2, encryptSize ^ PROTOCOL_VERSION);
     
    13271324        int hmacLen = encryptSize + UDPPacket.IV_SIZE + 2;
    13281325        //Hash hmac = _context.hmac().calculate(macKey, data, hmacOff, hmacLen);
    1329         ByteArray ba = _hmacCache.acquire();
    1330         _context.hmac().calculate(macKey, data, hmacOff, hmacLen, ba.getData(), 0);
     1326        byte[] ba = SimpleByteCache.acquire(Hash.HASH_LENGTH);
     1327        _context.hmac().calculate(macKey, data, hmacOff, hmacLen, ba, 0);
    13311328       
    13321329        if (_log.shouldLog(Log.DEBUG))
    13331330            _log.debug("Authenticating " + packet.getPacket().getLength() +
    1334                        "\nIV: " + Base64.encode(iv.getData()) +
    1335                        "\nraw mac: " + Base64.encode(ba.getData()) +
     1331                       "\nIV: " + Base64.encode(iv) +
     1332                       "\nraw mac: " + Base64.encode(ba) +
    13361333                       "\nMAC key: " + macKey);
    13371334        // ok, now lets put it back where it belongs...
    13381335        System.arraycopy(data, hmacOff, data, encryptOffset, encryptSize);
    13391336        //System.arraycopy(hmac.getData(), 0, data, hmacOff, UDPPacket.MAC_SIZE);
    1340         System.arraycopy(ba.getData(), 0, data, hmacOff, UDPPacket.MAC_SIZE);
    1341         System.arraycopy(iv.getData(), 0, data, hmacOff + UDPPacket.MAC_SIZE, UDPPacket.IV_SIZE);
    1342         _hmacCache.release(ba);
     1337        System.arraycopy(ba, 0, data, hmacOff, UDPPacket.MAC_SIZE);
     1338        System.arraycopy(iv, 0, data, hmacOff + UDPPacket.MAC_SIZE, UDPPacket.IV_SIZE);
     1339        SimpleByteCache.release(ba);
    13431340        long timeToAuth = System.currentTimeMillis() - before;
    13441341        _context.statManager().addRateData("udp.packetAuthTime", timeToAuth, timeToAuth);
  • router/java/src/net/i2p/router/tunnel/BloomFilterIVValidator.java

    r4360284 rc48aca8  
    66import net.i2p.router.util.DecayingBloomFilter;
    77import net.i2p.router.util.DecayingHashSet;
    8 import net.i2p.util.ByteCache;
     8import net.i2p.util.SimpleByteCache;
    99
    1010/**
     
    1616    private final RouterContext _context;
    1717    private final DecayingBloomFilter _filter;
    18     private final ByteCache _ivXorCache = ByteCache.getInstance(32, HopProcessor.IV_LENGTH);
    1918   
    2019    /**
     
    5857   
    5958    public boolean receiveIV(byte ivData[], int ivOffset, byte payload[], int payloadOffset) {
    60         ByteArray buf = _ivXorCache.acquire();
    61         DataHelper.xor(ivData, ivOffset, payload, payloadOffset, buf.getData(), 0, HopProcessor.IV_LENGTH);
    62         boolean dup = _filter.add(buf.getData());
    63         _ivXorCache.release(buf);
     59        byte[] buf = SimpleByteCache.acquire(HopProcessor.IV_LENGTH);
     60        DataHelper.xor(ivData, ivOffset, payload, payloadOffset, buf, 0, HopProcessor.IV_LENGTH);
     61        boolean dup = _filter.add(buf);
     62        SimpleByteCache.release(buf);
    6463        if (dup) _context.statManager().addRateData("tunnel.duplicateIV", 1);
    6564        return !dup; // return true if it is OK, false if it isn't
  • router/java/src/net/i2p/router/tunnel/HopProcessor.java

    r4360284 rc48aca8  
    33import net.i2p.I2PAppContext;
    44import net.i2p.data.Hash;
    5 import net.i2p.util.ByteCache;
    65import net.i2p.util.Log;
    76
     
    3029    static final boolean USE_DOUBLE_IV_ENCRYPTION = true;
    3130    static final int IV_LENGTH = 16;
    32     private static final ByteCache _cache = ByteCache.getInstance(128, IV_LENGTH);
    3331   
    3432    /** @deprecated unused */
  • router/java/src/net/i2p/router/tunnel/InboundEndpointProcessor.java

    r4360284 rc48aca8  
    44import net.i2p.data.Hash;
    55import net.i2p.router.RouterContext;
    6 import net.i2p.util.ByteCache;
    76import net.i2p.util.Log;
     7import net.i2p.util.SimpleByteCache;
    88
    99/**
     
    2222   
    2323    static final boolean USE_ENCRYPTION = HopProcessor.USE_ENCRYPTION;
    24     private static final ByteCache _cache = ByteCache.getInstance(128, HopProcessor.IV_LENGTH);
    2524   
    2625    /** @deprecated unused */
     
    5554        }
    5655       
    57         ByteArray ba = _cache.acquire();
    58         byte iv[] = ba.getData(); //new byte[HopProcessor.IV_LENGTH];
     56        byte iv[] = SimpleByteCache.acquire(HopProcessor.IV_LENGTH);
    5957        System.arraycopy(orig, offset, iv, 0, iv.length);
    6058        //if (_config.getLength() > 1)
     
    6563            if (_log.shouldLog(Log.WARN))
    6664                _log.warn("Invalid IV, dropping at IBEP " + _config);
    67             _cache.release(ba);
     65            SimpleByteCache.release(iv);
    6866            return false;
    6967        }
     
    7371            decrypt(_context, _config, iv, orig, offset, length);
    7472       
    75         _cache.release(ba);
     73        SimpleByteCache.release(iv);
    7674       
    7775        if (_config.getLength() > 0) {
     
    9290    private void decrypt(RouterContext ctx, TunnelCreatorConfig cfg, byte iv[], byte orig[], int offset, int length) {
    9391        //Log log = ctx.logManager().getLog(OutboundGatewayProcessor.class);
    94         ByteArray ba = _cache.acquire();
    95         byte cur[] = ba.getData(); // new byte[HopProcessor.IV_LENGTH]; // so we dont malloc
     92        byte cur[] = SimpleByteCache.acquire(HopProcessor.IV_LENGTH);
    9693        for (int i = cfg.getLength()-2; i >= 0; i--) { // dont include the endpoint, since that is the creator
    9794            OutboundGatewayProcessor.decrypt(ctx, iv, orig, offset, length, cur, cfg.getConfig(i));
     
    10198            //}
    10299        }
    103         _cache.release(ba);
     100        SimpleByteCache.release(cur);
    104101    }
    105102   
  • router/java/src/net/i2p/router/tunnel/OutboundGatewayProcessor.java

    r4360284 rc48aca8  
    44import net.i2p.data.Base64;
    55import net.i2p.data.ByteArray;
    6 import net.i2p.util.ByteCache;
    76import net.i2p.util.Log;
     7import net.i2p.util.SimpleByteCache;
    88
    99/**
     
    1919       
    2020    static final boolean USE_ENCRYPTION = HopProcessor.USE_ENCRYPTION;
    21     private static final ByteCache _cache = ByteCache.getInstance(128, HopProcessor.IV_LENGTH);
    2221
    2322    public OutboundGatewayProcessor(I2PAppContext ctx, TunnelCreatorConfig cfg) {
     
    3635     */
    3736    public void process(byte orig[], int offset, int length) {
    38         ByteArray ba = _cache.acquire();
    39         byte iv[] = ba.getData(); // new byte[HopProcessor.IV_LENGTH];
     37        byte iv[] = SimpleByteCache.acquire(HopProcessor.IV_LENGTH);
    4038        //_context.random().nextBytes(iv);
    4139        //System.arraycopy(iv, 0, orig, offset, HopProcessor.IV_LENGTH);
     
    5048        if (_log.shouldLog(Log.DEBUG))
    5149            _log.debug("finished processing the preprocessed data");
    52         _cache.release(ba);
     50        SimpleByteCache.release(iv);
    5351    }
    5452   
     
    5957    private void decrypt(I2PAppContext ctx, TunnelCreatorConfig cfg, byte iv[], byte orig[], int offset, int length) {
    6058        Log log = ctx.logManager().getLog(OutboundGatewayProcessor.class);
    61         ByteArray ba = _cache.acquire();
    62         byte cur[] = ba.getData(); // new byte[HopProcessor.IV_LENGTH]; // so we dont malloc
     59        byte cur[] = SimpleByteCache.acquire(HopProcessor.IV_LENGTH);
    6360        for (int i = cfg.getLength()-1; i >= 1; i--) { // dont include hop 0, since that is the creator
    6461            decrypt(ctx, iv, orig, offset, length, cur, cfg.getConfig(i));
     
    6865            }
    6966        }
    70         _cache.release(ba);
     67        SimpleByteCache.release(cur);
    7168    }
    7269   
  • router/java/src/net/i2p/router/tunnel/TrivialPreprocessor.java

    r4360284 rc48aca8  
    1010import net.i2p.util.ByteCache;
    1111import net.i2p.util.Log;
     12import net.i2p.util.SimpleByteCache;
    1213
    1314/**
     
    3435    protected static final ByteCache _dataCache = ByteCache.getInstance(32, PREPROCESSED_SIZE);
    3536
    36     private static final ByteCache _ivCache = ByteCache.getInstance(128, IV_SIZE);
    37     private static final ByteCache _hashCache = ByteCache.getInstance(128, Hash.HASH_LENGTH);
    38    
    3937    public TrivialPreprocessor(RouterContext ctx) {
    4038        _context = ctx;
     
    6462     */
    6563    protected void preprocess(byte fragments[], int fragmentLength) {
    66         ByteArray ivBuf = _ivCache.acquire();
    67         byte iv[] = ivBuf.getData(); // new byte[IV_SIZE];
     64        byte iv[] = SimpleByteCache.acquire(IV_SIZE);
    6865        _context.random().nextBytes(iv);
    6966       
     
    7168        System.arraycopy(iv, 0, fragments, fragmentLength, IV_SIZE);
    7269       
    73         ByteArray hashBuf = _hashCache.acquire();
     70        byte[] hashBuf = SimpleByteCache.acquire(Hash.HASH_LENGTH);
    7471        //Hash h = _context.sha().calculateHash(fragments, 0, fragmentLength + IV_SIZE);
    75         _context.sha().calculateHash(fragments, 0, fragmentLength + IV_SIZE, hashBuf.getData(), 0);
     72        _context.sha().calculateHash(fragments, 0, fragmentLength + IV_SIZE, hashBuf, 0);
    7673       
    7774        //Hash h = _context.sha().calculateHash(target, 0, offset + IV_SIZE);
     
    9289        offset += IV_SIZE;
    9390        //System.arraycopy(h.getData(), 0, fragments, offset, 4);
    94         System.arraycopy(hashBuf.getData(), 0, fragments, offset, 4);
     91        System.arraycopy(hashBuf, 0, fragments, offset, 4);
    9592        offset += 4;
    9693        //_log.debug("before pad  : " + Base64.encode(target));
    9794       
    98         _hashCache.release(hashBuf);
    99         _ivCache.release(ivBuf);
     95        SimpleByteCache.release(hashBuf);
     96        SimpleByteCache.release(iv);
    10097       
    10198        // fits in a single message, so may be smaller than the full size
Note: See TracChangeset for help on using the changeset viewer.