Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid creating flush ByteBuf/Segment when not necessary. #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 63 additions & 1 deletion kcp-netty/src/main/java/io/jpower/kcp/netty/Kcp.java
Original file line number Diff line number Diff line change
Expand Up @@ -1359,7 +1359,10 @@ public void update(long current) {
} else {
tsFlush = this.current + interval;
}
flush();

if(checkFlushRequired()) {
flush();
}
}

/**
Expand Down Expand Up @@ -1428,6 +1431,65 @@ public boolean checkFlush() {
return false;
}

/**
* Checks if flush should be called this update.
* @return boolean
*/
private boolean checkFlushRequired() {
if (ackcount > 0) {
return true;
}

// TODO: this is not fully accurate
if (probe != 0) {
return true;
}

// move data from snd_queue to snd_buf
if (sndQueue.size() > 0) {
// no cwnd yet should run flush
if(cwnd == 0) {
return true;
}

// calculate window size
int cwnd0 = Math.min(sndWnd, rmtWnd);
if (!nocwnd) {
cwnd0 = Math.min(this.cwnd, cwnd0);
}

if(itimediff(sndNxt, sndUna + cwnd0) < 0) {
return true;
}
}

if (sndBuf.size() > 0) {
long current = this.current;

// calculate resent
int resent = fastresend > 0 ? fastresend : Integer.MAX_VALUE;
int rtomin = nodelay ? 0 : (rxRto >> 3);

// flush data segments
int change = 0;
boolean lost = false;
for (Iterator<Segment> itr = sndBufItr.rewind(); itr.hasNext(); ) {
Segment segment = itr.next();
if (segment.xmit == 0) {
return true;
} else if (itimediff(current, segment.resendts) >= 0) {
return true;
} else if (segment.fastack >= resent) {
if (segment.xmit <= fastlimit || fastlimit <= 0) {
return true;
}
}
}
}

return false;
}

private void incrXmit(Segment seg) {
if (++seg.xmit > maxSegXmit) {
maxSegXmit = seg.xmit;
Expand Down