Skip to content

Commit

Permalink
Performance optimization: HttpVersion#get is called in the critical e…
Browse files Browse the repository at this point in the history
…xecution path of the HTTP/1.1 protocol
  • Loading branch information
ok2c committed Jan 19, 2024
1 parent 2662231 commit 30b3a20
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,14 @@ public final class HttpVersion extends ProtocolVersion {
* @since 5.0
*/
public static HttpVersion get(final int major, final int minor) {
for (int i = 0; i < ALL.length; i++) {
if (ALL[i].equals(major, minor)) {
return ALL[i];
if (major == 1) {
if (minor == 1) {
return HTTP_1_1;
} else if (minor == 0) {
return HTTP_1_0;
}
} else if (major == 2 && minor == 0) {
return HTTP_2_0;
}
// argument checking is done in the constructor
return new HttpVersion(major, minor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ public void testGet() {
Assertions.assertEquals(HttpVersion.HTTP_2, HttpVersion.get(2, 0));
Assertions.assertNotEquals(HttpVersion.HTTP_2_0, HttpVersion.get(2, 1));
//
Assertions.assertSame(HttpVersion.HTTP_0_9, HttpVersion.get(0, 9));
Assertions.assertSame(HttpVersion.HTTP_1_0, HttpVersion.get(1, 0));
Assertions.assertSame(HttpVersion.HTTP_1_1, HttpVersion.get(1, 1));
Assertions.assertSame(HttpVersion.HTTP_2_0, HttpVersion.get(2, 0));
Assertions.assertSame(HttpVersion.HTTP_2, HttpVersion.get(2, 0));
Assertions.assertNotSame(HttpVersion.HTTP_2_0, HttpVersion.get(2, 1));
Assertions.assertNotSame(HttpVersion.HTTP_0_9, HttpVersion.get(0, 9));
}

@SuppressWarnings("unused")
Expand Down

0 comments on commit 30b3a20

Please sign in to comment.