Changeset 3c5f9d0 for router


Ignore:
Timestamp:
Dec 13, 2015 4:48:04 PM (6 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
9594660
Parents:
2155347
Message:

RouterInfo?: Optimize writing to avoid extra copy;
eliminate caching previously enabled for routers with high memory limits
Log tweak on sig verify fail
DataHelper?.writeLong() to write(byte) conversion
DatabaseEntry?: Remove deprecated, unused setRoutingKey()

File:
1 edited

Legend:

Unmodified
Added
Removed
  • router/java/src/net/i2p/data/router/RouterInfo.java

    r2155347 r3c5f9d0  
    7979    /** should we cache the byte and string versions _byteified ? **/
    8080    private boolean _shouldCache;
    81     /** maybe we should check if we are floodfill? */
    82     private static final boolean CACHE_ALL = SystemVersion.getMaxMemory() > 128*1024*1024l;
     81    /**
     82     * Maybe we should check if we are floodfill?
     83     * If we do bring this back, don't do on ARM or Android
     84     */
     85    private static final boolean CACHE_ALL = false; // SystemVersion.getMaxMemory() > 128*1024*1024l;
    8386
    8487    public static final String PROP_NETWORK_ID = "netId";
     
    309312    protected byte[] getBytes() throws DataFormatException {
    310313        if (_byteified != null) return _byteified;
    311         if (_identity == null) throw new DataFormatException("Router identity isn't set?!");
    312 
    313         //long before = Clock.getInstance().now();
    314314        ByteArrayOutputStream out = new ByteArrayOutputStream(2*1024);
    315315        try {
     316            writeDataBytes(out);
     317        } catch (IOException ioe) {
     318            throw new DataFormatException("IO Error getting bytes", ioe);
     319        }
     320        byte data[] = out.toByteArray();
     321        if (CACHE_ALL || _shouldCache)
     322            _byteified = data;
     323        return data;
     324    }
     325
     326    /**
     327     * Write out the raw payload of the routerInfo, excluding the signature.  This
     328     * caches the data in memory if possible.
     329     *
     330     * @throws DataFormatException if the data is somehow b0rked (missing props, etc)
     331     * @throws IOException
     332     * @since 0.9.24
     333     */
     334    private void writeDataBytes(OutputStream out) throws DataFormatException, IOException {
     335        if (_identity == null) throw new DataFormatException("Missing identity");
     336        if (_published < 0) throw new DataFormatException("Invalid published date: " + _published);
     337
    316338            _identity.writeBytes(out);
    317339            // avoid thrashing objects
     
    321343            if (sz <= 0 || isHidden()) {
    322344                // Do not send IP address to peers in hidden mode
    323                 DataHelper.writeLong(out, 1, 0);
     345                out.write((byte) 0);
    324346            } else {
    325                 DataHelper.writeLong(out, 1, sz);
     347                out.write((byte) sz);
    326348                for (RouterAddress addr : _addresses) {
    327349                    addr.writeBytes(out);
     
    333355            //         at the moment, and may not be later.
    334356            int psz = _peers == null ? 0 : _peers.size();
    335             DataHelper.writeLong(out, 1, psz);
     357            out.write((byte) psz);
    336358            if (psz > 0) {
    337359                Collection<Hash> peers = _peers;
     
    346368            }
    347369            DataHelper.writeProperties(out, _options);
    348         } catch (IOException ioe) {
    349             throw new DataFormatException("IO Error getting bytes", ioe);
    350         }
    351         byte data[] = out.toByteArray();
    352         //if (_log.shouldLog(Log.DEBUG)) {
    353         //    long after = Clock.getInstance().now();
    354         //    _log.debug("getBytes()  took " + (after - before) + "ms");
    355         //}
    356         if (CACHE_ALL || _shouldCache)
    357             _byteified = data;
    358         return data;
    359370    }
    360371
     
    488499        if (!_isValid) {
    489500            Log log = I2PAppContext.getGlobalContext().logManager().getLog(RouterInfo.class);
    490             // TODO change to warn
    491             //if (log.shouldWarn()) {
    492                 log.error("Sig verify fail: " + toString(), new Exception("from"));
    493             //}
     501            if (log.shouldWarn()) {
     502                log.warn("Sig verify fail: " + toString(), new Exception("from"));
     503            } else {
     504                log.error("RI Sig verify fail: " + _identity.getHash());
     505            }
    494506        }
    495507    }
     
    588600     */
    589601    public void writeBytes(OutputStream out) throws DataFormatException, IOException {
    590         if (_identity == null) throw new DataFormatException("Missing identity");
    591         if (_published < 0) throw new DataFormatException("Invalid published date: " + _published);
    592602        if (_signature == null) throw new DataFormatException("Signature is null");
    593         //if (!isValid())
    594         //    throw new DataFormatException("Data is not valid");
    595         ByteArrayOutputStream baos = new ByteArrayOutputStream(2048);
    596         baos.write(getBytes());
    597         _signature.writeBytes(baos);
    598 
    599         byte data[] = baos.toByteArray();
    600         //_log.debug("Writing routerInfo [len=" + data.length + "]: " + toString());
    601         out.write(data);
     603        writeDataBytes(out);
     604        _signature.writeBytes(out);
    602605    }
    603606   
Note: See TracChangeset for help on using the changeset viewer.