-
Notifications
You must be signed in to change notification settings - Fork 46
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
Remove indify string concatenation on Inet::getInet4Address #93
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -377,7 +377,6 @@ public static <T extends InetAddress> T[] getAllAddressesByNameAndType(String ho | |
* @return the address (not {@code null}) | ||
*/ | ||
public static Inet4Address getInet4Address(int s1, int s2, int s3, int s4) { | ||
byte[] bytes = new byte[4]; | ||
Assert.checkMinimumParameter("s1", 0, s1); | ||
Assert.checkMaximumParameter("s1", 255, s1); | ||
Assert.checkMinimumParameter("s2", 0, s2); | ||
|
@@ -386,18 +385,47 @@ public static Inet4Address getInet4Address(int s1, int s2, int s3, int s4) { | |
Assert.checkMaximumParameter("s3", 255, s3); | ||
Assert.checkMinimumParameter("s4", 0, s4); | ||
Assert.checkMaximumParameter("s4", 255, s4); | ||
byte[] bytes = new byte[4]; | ||
bytes[0] = (byte) s1; | ||
bytes[1] = (byte) s2; | ||
bytes[2] = (byte) s3; | ||
bytes[3] = (byte) s4; | ||
// pre-compute the digits required | ||
int digitsForS1 = s1 < 10 ? 1 : s1 < 100 ? 2 : 3; | ||
int digitsForS2 = s2 < 10 ? 1 : s2 < 100 ? 2 : 3; | ||
int digitsForS3 = s3 < 10 ? 1 : s3 < 100 ? 2 : 3; | ||
int digitsForS4 = s4 < 10 ? 1 : s4 < 100 ? 2 : 3; | ||
byte[] hostBytes = new byte[3 + digitsForS1 + digitsForS2 + digitsForS3 + digitsForS4]; | ||
// use encodeUnsignedByte to encode s1,s2,s3,s4 into hostBytes | ||
encodeUnsignedByte(s1, hostBytes, 0, digitsForS1); | ||
hostBytes[digitsForS1] = '.'; | ||
encodeUnsignedByte(s2, hostBytes, digitsForS1 + 1, digitsForS2); | ||
hostBytes[digitsForS1 + digitsForS2 + 1] = '.'; | ||
encodeUnsignedByte(s3, hostBytes, digitsForS1 + digitsForS2 + 2, digitsForS3); | ||
hostBytes[digitsForS1 + digitsForS2 + digitsForS3 + 2] = '.'; | ||
encodeUnsignedByte(s4, hostBytes, digitsForS1 + digitsForS2 + digitsForS3 + 3, digitsForS4); | ||
String hostName = new String(hostBytes, 0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. explained at #93 (comment) |
||
try { | ||
return (Inet4Address) InetAddress.getByAddress(s1 + "." + s2 + "." + s3 + "." + s4, bytes); | ||
return (Inet4Address) InetAddress.getByAddress(hostName, bytes); | ||
} catch (UnknownHostException e) { | ||
// not possible | ||
throw new IllegalStateException(e); | ||
} | ||
} | ||
|
||
private static void encodeUnsignedByte(int value, byte[] bytes, int offset, int digits) { | ||
assert value >= 0 && value <= 255 && digits >= 1 && digits <= 3; | ||
if (digits == 3) { | ||
bytes[offset + 2] = (byte) ('0' + (value % 10)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dmlloyd FYI I didn't used the manual "strength reduction" to bit-shifts because:
|
||
value /= 10; | ||
} | ||
if (digits == 2) { | ||
bytes[offset + 1] = (byte) ('0' + (value % 10)); | ||
value /= 10; | ||
} | ||
bytes[offset] = (byte) ('0' + (value % 10)); | ||
} | ||
|
||
/** | ||
* Get an IPv6 address from eight integer segments. Each segment must be between 0 and 65535 ({@code 0xffff}). | ||
* | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could avoid this (and some redundant branching) with something like:
and then:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The redundant branching are there to avoid data dependency among
encodeUnsignedByte
calls, which could be indipendent from each others thanks to the upfront estimation. But is a minor: I'm opened to change it if you prefer it.for
there's an opened JDK bug (I've provided the reproducer some time ago) which show that it perform very bad, still, see https://bugs.openjdk.org/browse/JDK-8295496
quoting it
That's why I didn't used it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Additionally, I have sized correctly the byte array and not using any
cnt
variable to simplify the life of the JDK stub generator and the bound checks generation, there, which proved to be the reason behind the bad performance of https://bugs.openjdk.org/browse/JDK-8295496