Skip to content

Commit

Permalink
Removing unnecessary use of Math.ceil in HTTP/2 priority algorithm.
Browse files Browse the repository at this point in the history
Motivation:

We're currently using Math.ceil which isn't necessary. We should exchange for a lighter weight operation.

Modifications:

Changing the logic to just ensure that we allocate at least one byte to the child rather than always performing a ceil.

Result:

Slight performance improvement in the priority algorithm.
  • Loading branch information
nmittler committed Mar 31, 2015
1 parent 6ebcd08 commit 2faa473
Showing 1 changed file with 8 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,10 @@ private void writePendingBytes() {
}

/**
* This will allocate bytes by stream weight and priority for the entire tree rooted at {@code parent}, but does
* not write any bytes.
* This will allocate bytes by stream weight and priority for the entire tree rooted at {@code parent}, but does not
* write any bytes. The connection window is generally distributed amongst siblings according to their weight,
* however we need to ensure that the entire connection window is used (assuming streams have >= connection window
* bytes to send) and we may need some sort of rounding to accomplish this.
*
* @param parent The parent of the tree.
* @param connectionWindow The connection window this is available for use at this point in the tree.
Expand Down Expand Up @@ -282,7 +284,10 @@ private int allocateBytesForTree(Http2Stream parent, int connectionWindow) {
int weight = child.weight();
double weightRatio = weight / (double) totalWeight;

int bytesForTree = Math.min(nextConnectionWindow, (int) Math.ceil(connectionWindow * weightRatio));
// In order to make progress toward the connection window due to possible rounding errors, we make sure
// that each stream (with data to send) is given at least 1 byte toward the connection window.
int connectionWindowChunk = Math.max(1, (int) (connectionWindow * weightRatio));
int bytesForTree = Math.min(nextConnectionWindow, connectionWindowChunk);
int bytesForChild = Math.min(state.streamableBytes(), bytesForTree);

if (bytesForChild > 0) {
Expand Down

0 comments on commit 2faa473

Please sign in to comment.