Changeset fd4e4fbc


Ignore:
Timestamp:
Jan 9, 2011 1:04:06 AM (9 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
a51ae64e
Parents:
9a63be8
Message:
  • Data Structures: More caching improvements, don't cache where we shouldn't
Files:
3 edited

Legend:

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

    r9a63be8 rfd4e4fbc  
    2424    }
    2525   
    26     /** Calculate the SHA-256 has of the source
     26    /**
     27     * Calculate the SHA-256 hash of the source and cache the result.
    2728     * @param source what to hash
    2829     * @return hash of the source
     
    3132        return calculateHash(source, 0, source.length);
    3233    }
     34
     35    /**
     36     * Calculate the hash and cache the result.
     37     */
    3338    public final Hash calculateHash(byte[] source, int start, int len) {
    3439        Sha256Standalone digest = acquireGnu();
     
    4045    }
    4146   
     47    /**
     48     * Use this if you only need the data, not a Hash object.
     49     * Does not cache.
     50     */
    4251    public final void calculateHash(byte[] source, int start, int len, byte out[], int outOffset) {
    4352        Sha256Standalone digest = acquireGnu();
  • router/java/src/net/i2p/data/i2np/I2NPMessageImpl.java

    r9a63be8 rfd4e4fbc  
    2121import net.i2p.data.Hash;
    2222import net.i2p.util.Log;
     23import net.i2p.util.SimpleByteCache;
    2324
    2425/**
     
    7374     *  readMessage in the implemented message type
    7475     *
     76     *<pre>
    7577     *  Specifically:
    7678     *    1 byte type (if caller didn't read already, as specified by the type param
     
    8082     *    1 byte checksum
    8183     *    size bytes of payload (read by readMessage() in implementation)
     84     *</pre>
    8285     *
    8386     *  @param type the message type or -1 if we should read it here
    8487     *  @param buffer temp buffer to use
     88     *  @return total length of the message
    8589     */
    8690    public int readBytes(InputStream in, int type, byte buffer[]) throws I2NPMessageException, IOException {
     
    111115            }
    112116           
    113             Hash calc = _context.sha().calculateHash(buffer, 0, size);
     117            byte[] calc = SimpleByteCache.acquire(Hash.HASH_LENGTH);
     118            _context.sha().calculateHash(buffer, 0, size, calc, 0);
    114119            //boolean eq = calc.equals(h);
    115             boolean eq = DataHelper.eq(checksum, 0, calc.getData(), 0, CHECKSUM_LENGTH);
     120            boolean eq = DataHelper.eq(checksum, 0, calc, 0, CHECKSUM_LENGTH);
     121            SimpleByteCache.release(calc);
    116122            if (!eq)
    117123                throw new I2NPMessageException("Hash does not match for " + getClass().getName());
     
    124130            //if (time > 50)
    125131            //    _context.statManager().addRateData("i2np.readTime", time, time);
    126             return size + Hash.HASH_LENGTH + 1 + 4 + DataHelper.DATE_LENGTH;
     132            return CHECKSUM_LENGTH + 1 + 2 + 4 + DataHelper.DATE_LENGTH + size;
    127133        } catch (DataFormatException dfe) {
    128134            throw new I2NPMessageException("Error reading the message header", dfe);
    129135        }
    130136    }
     137
     138    /**
     139     *  Read the header, then read the rest into buffer, then call
     140     *  readMessage in the implemented message type
     141     *
     142     *<pre>
     143     *  Specifically:
     144     *    1 byte type (if caller didn't read already, as specified by the type param
     145     *    4 byte ID
     146     *    8 byte expiration
     147     *    2 byte size
     148     *    1 byte checksum
     149     *    size bytes of payload (read by readMessage() in implementation)
     150     *</pre>
     151     *
     152     *  @param type the message type or -1 if we should read it here
     153     *  @return total length of the message
     154     */
    131155    public int readBytes(byte data[], int type, int offset) throws I2NPMessageException, IOException {
    132156        int cur = offset;
     
    154178                                           + " wanted=" + size + "]: " + getClass().getName());
    155179
    156         Hash calc = _context.sha().calculateHash(data, cur, size);
    157         //boolean eq = calc.equals(h);
    158         boolean eq = DataHelper.eq(hdata, 0, calc.getData(), 0, CHECKSUM_LENGTH);
     180        byte[] calc = SimpleByteCache.acquire(Hash.HASH_LENGTH);
     181        _context.sha().calculateHash(data, cur, size, calc, 0);
     182        boolean eq = DataHelper.eq(hdata, 0, calc, 0, CHECKSUM_LENGTH);
     183        SimpleByteCache.release(calc);
    159184        if (!eq)
    160185            throw new I2NPMessageException("Hash does not match for " + getClass().getName());
     
    232257            int writtenLen = writeMessageBody(buffer, prefixLen);
    233258            int payloadLen = writtenLen - prefixLen;
    234             Hash h = _context.sha().calculateHash(buffer, prefixLen, payloadLen);
     259            byte[] h = SimpleByteCache.acquire(Hash.HASH_LENGTH);
     260            _context.sha().calculateHash(buffer, prefixLen, payloadLen, h, 0);
    235261
    236262            int off = 0;
     
    243269            DataHelper.toLong(buffer, off, 2, payloadLen);
    244270            off += 2;
    245             System.arraycopy(h.getData(), 0, buffer, off, CHECKSUM_LENGTH);
     271            System.arraycopy(h, 0, buffer, off, CHECKSUM_LENGTH);
     272            SimpleByteCache.release(h);
    246273
    247274            //long time = _context.clock().now() - start;
  • router/java/src/net/i2p/router/tunnel/FragmentHandler.java

    r9a63be8 rfd4e4fbc  
    1616import net.i2p.util.ByteCache;
    1717import net.i2p.util.Log;
     18import net.i2p.util.SimpleByteCache;
    1819import net.i2p.util.SimpleTimer;
    1920
     
    242243            _log.debug("endpoint IV: " + Base64.encode(preV, validLength - HopProcessor.IV_LENGTH, HopProcessor.IV_LENGTH));
    243244       
    244         Hash v = _context.sha().calculateHash(preV, 0, validLength);
     245        byte[] v = SimpleByteCache.acquire(Hash.HASH_LENGTH);
     246        _context.sha().calculateHash(preV, 0, validLength, v, 0);
    245247        _validateCache.release(ba);
    246248       
    247         boolean eq = DataHelper.eq(v.getData(), 0, preprocessed, offset + HopProcessor.IV_LENGTH, 4);
     249        boolean eq = DataHelper.eq(v, 0, preprocessed, offset + HopProcessor.IV_LENGTH, 4);
    248250        if (!eq) {
    249251            if (_log.shouldLog(Log.WARN)) {
    250252                _log.warn("Corrupt tunnel message - verification fails: " + Base64.encode(preprocessed, offset+HopProcessor.IV_LENGTH, 4)
    251                            + " != " + Base64.encode(v.getData(), 0, 4));
     253                           + " != " + Base64.encode(v, 0, 4));
    252254                _log.warn("No matching endpoint: # pad bytes: " + (paddingEnd-(HopProcessor.IV_LENGTH+4)-1)
    253255                           + " offset=" + offset + " length=" + length + " paddingEnd=" + paddingEnd + ' '
     
    255257            }
    256258        }
     259        SimpleByteCache.release(v);
    257260       
    258261        if (eq) {
Note: See TracChangeset for help on using the changeset viewer.