Changeset 2b14d32


Ignore:
Timestamp:
Sep 17, 2012 9:32:05 PM (9 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
2c4910e
Parents:
259c28f
Message:

use ByteCache? for chunks in/out

Location:
apps/i2psnark/java/src/org/klomp/snark
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • apps/i2psnark/java/src/org/klomp/snark/DataLoader.java

    r259c28f r2b14d32  
    11package org.klomp.snark;
     2
     3import net.i2p.data.ByteArray;
    24
    35/**
     
    1113   *  @return bytes or null for errors
    1214   */
    13     public byte[] loadData(int piece, int begin, int length);
     15    public ByteArray loadData(int piece, int begin, int length);
    1416}
  • apps/i2psnark/java/src/org/klomp/snark/Message.java

    r259c28f r2b14d32  
    2424import java.io.IOException;
    2525
    26 // Used to queue outgoing connections
    27 // sendMessage() should be used to translate them to wire format.
     26import net.i2p.data.ByteArray;
     27import net.i2p.util.ByteCache;
     28
     29/**
     30 * Used to queue outgoing connections
     31 * sendMessage() should be used to translate them to wire format.
     32 */
    2833class Message
    2934{
     
    7075  //SimpleTimer.TimedEvent expireEvent;
    7176 
     77  private static final int BUFSIZE = PeerState.PARTSIZE;
     78  private static final ByteCache _cache = ByteCache.getInstance(16, BUFSIZE);
     79
    7280  /** Utility method for sending a message through a DataStream. */
    7381  void sendMessage(DataOutputStream dos) throws IOException
     
    8088      }
    8189
     90    ByteArray ba;
    8291    // Get deferred data
    8392    if (data == null && dataLoader != null) {
    84         data = dataLoader.loadData(piece, begin, length);
    85         if (data == null)
     93        ba = dataLoader.loadData(piece, begin, length);
     94        if (ba == null)
    8695            return;  // hmm will get retried, but shouldn't happen
     96        data = ba.getData();
     97    } else {
     98        ba = null;
    8799    }
    88100
     
    140152    if (type == BITFIELD || type == PIECE || type == EXTENSION)
    141153      dos.write(data, off, len);
     154
     155    // Was pulled from cache in Storage.getPiece() via dataLoader
     156    if (ba != null && ba.getData().length == BUFSIZE)
     157        _cache.release(ba, false);
    142158  }
    143159
  • apps/i2psnark/java/src/org/klomp/snark/PartialPiece.java

    r259c28f r2b14d32  
    1010import net.i2p.I2PAppContext;
    1111import net.i2p.crypto.SHA1;
     12import net.i2p.data.ByteArray;
     13import net.i2p.util.ByteCache;
    1214import net.i2p.util.Log;
    1315import net.i2p.util.SecureFile;
     
    4345    private final File tempDir;
    4446
     47    private static final int BUFSIZE = PeerState.PARTSIZE;
     48    private static final ByteCache _cache = ByteCache.getInstance(16, BUFSIZE);
     49
    4550    // Any bigger than this, use temp file instead of heap
    4651    private static final int MAX_IN_MEM = 128 * 1024;
     
    155160        } else {
    156161            int read = 0;
    157             byte[] buf = new byte[Math.min(pclen, 16384)];
     162            int buflen = Math.min(pclen, BUFSIZE);
     163            ByteArray ba;
     164            byte[] buf;
     165            if (buflen == BUFSIZE) {
     166                ba = _cache.acquire();
     167                buf = ba.getData();
     168            } else {
     169                ba = null;
     170                buf = new byte[buflen];
     171            }
    158172            synchronized (this) {
    159173                if (raf == null)
     
    168182                }
    169183            }
     184            if (ba != null)
     185                _cache.release(ba, false);
    170186            if (read < pclen)
    171187                throw new IOException();
     
    183199        } else {
    184200            // read in fully before synching on raf
    185             byte[] tmp = new byte[len];
     201            ByteArray ba;
     202            byte[] tmp;
     203            if (len == BUFSIZE) {
     204                ba = _cache.acquire();
     205                tmp = ba.getData();
     206            } else {
     207                ba = null;
     208                tmp = new byte[len];
     209            }
    186210            din.readFully(tmp);
    187211            synchronized (this) {
     
    191215                raf.write(tmp);
    192216            }
     217            if (ba != null)
     218                _cache.release(ba, false);
    193219        }
    194220    }
     
    209235        } else {
    210236            int read = 0;
    211             byte[] buf = new byte[Math.min(len, 16384)];
     237            int buflen = Math.min(len, BUFSIZE);
     238            ByteArray ba;
     239            byte[] buf;
     240            if (buflen == BUFSIZE) {
     241                ba = _cache.acquire();
     242                buf = ba.getData();
     243            } else {
     244                ba = null;
     245                buf = new byte[buflen];
     246            }
    212247            synchronized (this) {
    213248                if (raf == null)
     
    221256                }
    222257            }
     258            if (ba != null)
     259                _cache.release(ba, false);
    223260        }
    224261    }
  • apps/i2psnark/java/src/org/klomp/snark/PeerConnectionOut.java

    r259c28f r2b14d32  
    438438  void sendPiece(int piece, int begin, int length, DataLoader loader)
    439439  {
     440    /****
    440441      boolean sendNow = false;
    441442      // are there any cases where we should?
     
    448449        return;
    449450      }
     451    ****/
    450452
    451453      // queue a fake message... set everything up,
  • apps/i2psnark/java/src/org/klomp/snark/PeerCoordinator.java

    r259c28f r2b14d32  
    3636
    3737import net.i2p.I2PAppContext;
     38import net.i2p.data.ByteArray;
    3839import net.i2p.data.DataHelper;
    3940import net.i2p.data.Destination;
     
    875876   * @throws RuntimeException on IOE getting the data
    876877   */
    877   public byte[] gotRequest(Peer peer, int piece, int off, int len)
     878  public ByteArray gotRequest(Peer peer, int piece, int off, int len)
    878879  {
    879880    if (halted)
  • apps/i2psnark/java/src/org/klomp/snark/PeerListener.java

    r259c28f r2b14d32  
    2222
    2323import java.util.List;
     24
     25import net.i2p.data.ByteArray;
    2426
    2527/**
     
    115117   * is not available (which is a protocol error).
    116118   */
    117   byte[] gotRequest(Peer peer, int piece, int off, int len);
     119  ByteArray gotRequest(Peer peer, int piece, int off, int len);
    118120
    119121  /**
  • apps/i2psnark/java/src/org/klomp/snark/PeerState.java

    r259c28f r2b14d32  
    2828
    2929import net.i2p.I2PAppContext;
     30import net.i2p.data.ByteArray;
    3031import net.i2p.util.Log;
    3132
     
    246247   *  @since 0.8.2
    247248   */
    248   public byte[] loadData(int piece, int begin, int length) {
    249     byte[] pieceBytes = listener.gotRequest(peer, piece, begin, length);
     249  public ByteArray loadData(int piece, int begin, int length) {
     250    ByteArray pieceBytes = listener.gotRequest(peer, piece, begin, length);
    250251    if (pieceBytes == null)
    251252      {
     
    257258
    258259    // More sanity checks
    259     if (length != pieceBytes.length)
     260    if (length != pieceBytes.getData().length)
    260261      {
    261262        // XXX - Protocol error-> disconnect?
  • apps/i2psnark/java/src/org/klomp/snark/Storage.java

    r259c28f r2b14d32  
    3535
    3636import net.i2p.crypto.SHA1;
     37import net.i2p.data.ByteArray;
     38import net.i2p.util.ByteCache;
    3739import net.i2p.util.Log;
    3840import net.i2p.util.SecureFile;
     
    8082
    8183  private static final boolean _isWindows = SystemVersion.isWindows();
     84
     85  private static final int BUFSIZE = PeerState.PARTSIZE;
     86  private static final ByteCache _cache = ByteCache.getInstance(16, BUFSIZE);
    8287
    8388  /**
     
    900905   * the storage doesn't contain the piece yet.
    901906   */
    902   public byte[] getPiece(int piece, int off, int len) throws IOException
     907  public ByteArray getPiece(int piece, int off, int len) throws IOException
    903908  {
    904909    if (!bitfield.get(piece))
     
    906911
    907912    //Catch a common place for OOMs esp. on 1MB pieces
     913    ByteArray rv;
    908914    byte[] bs;
    909915    try {
    910       bs = new byte[len];
     916        // Will be restored to cache in Message.sendMessage()
     917        if (len == BUFSIZE)
     918            rv = _cache.acquire();
     919        else
     920            rv = new ByteArray(new byte[len]);
    911921    } catch (OutOfMemoryError oom) {
    912922      if (_log.shouldLog(Log.WARN))
     
    914924      return null;
    915925    }
     926    bs = rv.getData();
    916927    getUncheckedPiece(piece, bs, off, len);
    917     return bs;
     928    return rv;
    918929  }
    919930
Note: See TracChangeset for help on using the changeset viewer.