Skip to content

Commit

Permalink
impr: Slightly speed up serializing scope (#4661)
Browse files Browse the repository at this point in the history
Use a double checked lock for getting the span when building the
trace context.
  • Loading branch information
philipphofmann authored Dec 23, 2024
1 parent 15cfa41 commit 0970e44
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- Improve compiler error message for missing Swift declarations due to APPLICATION_EXTENSION_API_ONLY (#4603)
- Mask screenshots for errors (#4623)
- Slightly speed up serializing scope (#4661)

### Features

Expand Down
14 changes: 7 additions & 7 deletions Sources/Sentry/SentryScope.m
Original file line number Diff line number Diff line change
Expand Up @@ -465,9 +465,14 @@ - (void)clearAttachments
}

NSDictionary *traceContext = nil;
@synchronized(_spanLock) {
traceContext = [self buildTraceContext:_span];
id<SentrySpan> span = nil;

if (self.span != nil) {
@synchronized(_spanLock) {
span = self.span;
}
}
traceContext = [self buildTraceContext:span];
serializedData[@"traceContext"] = traceContext;

NSDictionary *context = [self context];
Expand Down Expand Up @@ -606,8 +611,6 @@ - (SentryEvent *__nullable)applyToEvent:(SentryEvent *)event
}
}

// We don't need call synchronized(_spanLock) here because we get a copy of the span in the
// _spanLock above.
newContext[@"trace"] = [self buildTraceContext:span];

event.context = newContext;
Expand All @@ -619,9 +622,6 @@ - (void)addObserver:(id<SentryScopeObserver>)observer
[self.observers addObject:observer];
}

/**
* Make sure to call this inside @c synchronized(_spanLock) caus this method isn't thread safe.
*/
- (NSDictionary *)buildTraceContext:(nullable id<SentrySpan>)span
{
if (span != nil) {
Expand Down

0 comments on commit 0970e44

Please sign in to comment.