-
-
Notifications
You must be signed in to change notification settings - Fork 475
perf(core): Drop per-instance lock from SentryId and SpanId #5645
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 |
|---|---|---|
|
|
@@ -6,7 +6,6 @@ | |
| import io.sentry.ObjectReader; | ||
| import io.sentry.ObjectWriter; | ||
| import io.sentry.SentryUUID; | ||
| import io.sentry.util.LazyEvaluator; | ||
| import io.sentry.util.StringUtils; | ||
| import io.sentry.util.UUIDStringUtils; | ||
| import java.io.IOException; | ||
|
|
@@ -19,19 +18,15 @@ public final class SentryId implements JsonSerializable { | |
| public static final SentryId EMPTY_ID = | ||
| new SentryId(StringUtils.PROPER_NIL_UUID.replace("-", "")); | ||
|
|
||
| private final @NotNull LazyEvaluator<String> lazyStringValue; | ||
| private volatile @Nullable String value; | ||
|
Contributor
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 don't like the name of this field
Member
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. l: Still true, or was this comment from a previous version?
Contributor
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. Yes still true. |
||
| private final @Nullable UUID uuid; | ||
|
|
||
| public SentryId() { | ||
| this((UUID) null); | ||
| } | ||
|
|
||
| public SentryId(@Nullable UUID uuid) { | ||
| if (uuid != null) { | ||
| this.lazyStringValue = | ||
| new LazyEvaluator<>(() -> normalize(UUIDStringUtils.toSentryIdString(uuid))); | ||
| } else { | ||
| this.lazyStringValue = new LazyEvaluator<>(SentryUUID::generateSentryId); | ||
| } | ||
| this.uuid = uuid; | ||
| } | ||
|
|
||
| public SentryId(final @NotNull String sentryIdString) { | ||
|
|
@@ -42,29 +37,43 @@ public SentryId(final @NotNull String sentryIdString) { | |
| + "or 36 characters long (completed UUID). Received: " | ||
| + sentryIdString); | ||
| } | ||
| if (normalized.length() == 36) { | ||
| this.lazyStringValue = new LazyEvaluator<>(() -> normalize(normalized)); | ||
| } else { | ||
| this.lazyStringValue = new LazyEvaluator<>(() -> normalized); | ||
| this.uuid = null; | ||
| this.value = normalized.length() == 36 ? normalized.replace("-", "") : normalized; | ||
| } | ||
|
|
||
| private @NotNull String getValue() { | ||
| String result = value; | ||
| if (result == null) { | ||
| synchronized (this) { | ||
|
Contributor
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. using synchronized here still provides thread safety without allocating any extra objects. |
||
| result = value; | ||
| if (result == null) { | ||
| result = | ||
| uuid != null | ||
| ? normalize(UUIDStringUtils.toSentryIdString(uuid)) | ||
| : SentryUUID.generateSentryId(); | ||
| value = result; | ||
| } | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return lazyStringValue.getValue(); | ||
| return getValue(); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(final @Nullable Object o) { | ||
| if (this == o) return true; | ||
| if (o == null || getClass() != o.getClass()) return false; | ||
| SentryId sentryId = (SentryId) o; | ||
| return lazyStringValue.getValue().equals(sentryId.lazyStringValue.getValue()); | ||
| return getValue().equals(sentryId.getValue()); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return lazyStringValue.getValue().hashCode(); | ||
| return getValue().hashCode(); | ||
| } | ||
|
|
||
| private @NotNull String normalize(@NotNull String uuidString) { | ||
|
|
||
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.
I remember we intentionally replaced all synchronized calls a while ago to support Java Virtual threads, maybe @adinauer can shed some light on this?
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.
Oh, looks like it works fine with Java 24+: https://mikemybytes.com/2025/04/09/java24-thread-pinning-revisited/#synchronized-revisited
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.
That is interesting. But is that only relevant if we are using virtual threads here?