Skip to content
Snippets Groups Projects
Commit 13eff752 authored by akwizgran's avatar akwizgran
Browse files

Keep a stack of free segments to reduce allocations.

parent 337c1c8a
No related branches found
No related tags found
No related merge requests found
package net.sf.briar.transport; package net.sf.briar.transport;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry;
import net.sf.briar.api.FormatException; import net.sf.briar.api.FormatException;
import net.sf.briar.api.transport.Segment; import net.sf.briar.api.transport.Segment;
...@@ -15,6 +17,7 @@ class IncomingErrorCorrectionLayerImpl implements IncomingErrorCorrectionLayer { ...@@ -15,6 +17,7 @@ class IncomingErrorCorrectionLayerImpl implements IncomingErrorCorrectionLayer {
private final int n, k; private final int n, k;
private final Map<Long, Integer> discardCounts; private final Map<Long, Integer> discardCounts;
private final Map<Long, Segment[]> segmentSets; private final Map<Long, Segment[]> segmentSets;
private final ArrayList<Segment> freeSegments;
IncomingErrorCorrectionLayerImpl(IncomingEncryptionLayer in, IncomingErrorCorrectionLayerImpl(IncomingEncryptionLayer in,
ErasureDecoder decoder, int n, int k) { ErasureDecoder decoder, int n, int k) {
...@@ -24,24 +27,37 @@ class IncomingErrorCorrectionLayerImpl implements IncomingErrorCorrectionLayer { ...@@ -24,24 +27,37 @@ class IncomingErrorCorrectionLayerImpl implements IncomingErrorCorrectionLayer {
this.k = k; this.k = k;
discardCounts = new HashMap<Long, Integer>(); discardCounts = new HashMap<Long, Integer>();
segmentSets = new HashMap<Long, Segment[]>(); segmentSets = new HashMap<Long, Segment[]>();
freeSegments = new ArrayList<Segment>();
} }
public boolean readFrame(Frame f, FrameWindow window) throws IOException, public boolean readFrame(Frame f, FrameWindow window) throws IOException,
InvalidDataException { InvalidDataException {
// Free any segment sets that have been removed from the window // Free any segment sets that have been removed from the window
Iterator<Long> it = segmentSets.keySet().iterator(); Iterator<Entry<Long, Segment[]>> it = segmentSets.entrySet().iterator();
while(it.hasNext()) if(!window.contains(it.next())) it.remove(); while(it.hasNext()) {
Entry<Long, Segment[]> e = it.next();
if(!window.contains(e.getKey())) {
it.remove();
for(Segment s : e.getValue()) if(s != null) freeSegments.add(s);
}
}
// Free any discard counts that are no longer too high for the window // Free any discard counts that are no longer too high for the window
Iterator<Long> it1 = discardCounts.keySet().iterator(); Iterator<Long> it1 = discardCounts.keySet().iterator();
while(it1.hasNext()) if(!window.isTooHigh(it1.next())) it1.remove(); while(it1.hasNext()) if(!window.isTooHigh(it1.next())) it1.remove();
// FIXME: Unnecessary allocation // Grab a free segment, or allocate one if necessary
Segment s = new SegmentImpl(); Segment s;
int free = freeSegments.size();
if(free == 0) s = new SegmentImpl();
else s = freeSegments.remove(free - 1);
// Read segments until a frame can be decoded // Read segments until a frame can be decoded
while(true) { while(true) {
// Read segments until a segment in the window is returned // Read segments until a segment in the window is returned
long frameNumber; long frameNumber;
while(true) { while(true) {
if(!in.readSegment(s)) return false; if(!in.readSegment(s)) {
freeSegments.add(s);
return false;
}
frameNumber = s.getSegmentNumber() / n; frameNumber = s.getSegmentNumber() / n;
if(window.contains(frameNumber)) break; if(window.contains(frameNumber)) break;
if(window.isTooHigh(frameNumber)) countDiscard(frameNumber); if(window.isTooHigh(frameNumber)) countDiscard(frameNumber);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment