-
Notifications
You must be signed in to change notification settings - Fork 18
Use JDK UUID#fromString for Java >= 15 #15
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
base: main
Are you sure you want to change the base?
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 |
|---|---|---|
|
|
@@ -48,21 +48,21 @@ | |
| <dependency> | ||
| <groupId>junit</groupId> | ||
| <artifactId>junit</artifactId> | ||
| <version>4.13.1</version> | ||
| <version>4.13.2</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>org.openjdk.jmh</groupId> | ||
| <artifactId>jmh-core</artifactId> | ||
| <version>1.21</version> | ||
| <version>1.32</version> | ||
|
Owner
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. Note to self: we should probably pull this out into a property so we don't have to change the version in three different places. Let's definitely leave that for a separate effort, though.
Author
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. I can do that, no problem. Will extract when I get the chance to open it again on IntelliJ. |
||
| <scope>test</scope> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>org.openjdk.jmh</groupId> | ||
| <artifactId>jmh-generator-annprocess</artifactId> | ||
| <version>1.21</version> | ||
| <version>1.32</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
@@ -110,7 +110,7 @@ | |
| <path> | ||
| <groupId>org.openjdk.jmh</groupId> | ||
| <artifactId>jmh-generator-annprocess</artifactId> | ||
| <version>1.21</version> | ||
| <version>1.32</version> | ||
| </path> | ||
| </annotationProcessorPaths> | ||
| </configuration> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,6 +41,7 @@ | |
| public class FastUUID { | ||
|
|
||
| private static final boolean USE_JDK_UUID_TO_STRING; | ||
| private static final boolean USE_JDK_UUID_FROM_STRING; | ||
|
|
||
| static { | ||
| int majorVersion = 0; | ||
|
|
@@ -53,6 +54,7 @@ public class FastUUID { | |
| } | ||
|
|
||
| USE_JDK_UUID_TO_STRING = majorVersion >= 9; | ||
| USE_JDK_UUID_FROM_STRING = majorVersion >= 15; | ||
| } | ||
|
|
||
| private static final int UUID_STRING_LENGTH = 36; | ||
|
|
@@ -107,6 +109,35 @@ private FastUUID() { | |
| * described in {@link UUID#toString()} | ||
| */ | ||
| public static UUID parseUUID(final CharSequence uuidSequence) { | ||
| if (USE_JDK_UUID_FROM_STRING && uuidSequence instanceof String) { | ||
| // OpenJDK 15 and newer use a faster method for parsing UUIDs | ||
| return UUID.fromString((String) uuidSequence); | ||
| } | ||
|
|
||
| return parseUUIDInternal(uuidSequence); | ||
| } | ||
|
|
||
| /** | ||
| * Parses a UUID from the given string. The string must represent a UUID as described in | ||
| * {@link UUID#toString()}. | ||
| * | ||
| * @param uuidString the string from which to parse a UUID | ||
| * | ||
| * @return the UUID represented by the given string | ||
| * | ||
| * @throws IllegalArgumentException if the given string does not conform to the string representation as | ||
| * described in {@link UUID#toString()} | ||
| */ | ||
| public static UUID parseUUID(final String uuidString) { | ||
|
Owner
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. I think I'm missing something: why do we need this if we also have
Author
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. Because JDK's method only works with So we can:
Owner
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. Ah—I think I see where you're going here. If I can reword it to make sure we're on the same page, I think you're saying that you want to add a If I'm understanding that right, I'd gently push back that I think it's okay to continue using fast-uuid's parser even under Java 15. My rationale is that if somebody is working with non- As you say, using Does that make sense? Am I missing your point?
Author
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. It's more of a compile-time optimization and maybe being able to deal with JVMs where If you upgrade from existing code to this, your compiler will link your calls that use String-typed references to the new method. So String uuidString = someMethodThatReturnsString();
// this will now link directly to the String method instead of the CharSequence one without the user having to change any code
UUID id = FastUUID.parseUUID(uuidString);
CharSequence uuidChars = someMethodThatReturnsCharSequence();
// this will still link to the CharSequence method
UUID idFromCharSequence = FastUUID.parseUUID(uuidChars);If Providing the overload with String allows us to skip that step without the user changing any code because upon compilation, they'd link to the new method if the reference is String-typed. A CharSequence reference to a CharSequence non-String object will still just use the existing parser just fine. There is no conversion involved. I see this as a win-win change. The only downside is that For someone using fast-uuid indirectly, the type-check to String inside the CharSequence overload still allows for the performance boost in Java 15 even without recompilation. |
||
| if (USE_JDK_UUID_FROM_STRING) { | ||
| // OpenJDK 15 and newer use a faster method for parsing UUIDs | ||
| return UUID.fromString(uuidString); | ||
| } | ||
|
|
||
| return parseUUIDInternal(uuidString); | ||
| } | ||
|
|
||
| private static UUID parseUUIDInternal(final CharSequence uuidSequence) { | ||
| if (uuidSequence.length() != UUID_STRING_LENGTH || | ||
| uuidSequence.charAt(8) != '-' || | ||
| uuidSequence.charAt(13) != '-' || | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.