Skip to content

Commit 77444ab

Browse files
authored
perf(tracer): avoid unncessary context creation (#14773)
## Description Currently for a new root span we are creating 2 context objects. We create an empty one in `Tracer._start_span`, and then it gets copied when we create the root span. This is unnecessary, we can avoid this extra object allocation/GC. Benchmarks showing about a 12-15% reduction in root span creation overhead (only for starting the span, the span-finish scenario is 4-5% faster). ## Testing <!-- Describe your testing strategy or note what tests are included --> ## Risks <!-- Note any risks associated with this change, or "None" if no risks --> ## Additional Notes <!-- Any other information that would be helpful for reviewers -->
1 parent d033f37 commit 77444ab

File tree

2 files changed

+23
-21
lines changed

2 files changed

+23
-21
lines changed

.gitlab/benchmarks/bp-runner.microbenchmarks.fail-on-breach.yml

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -799,11 +799,11 @@ experiments:
799799
- name: otelspan-add-metrics
800800
thresholds:
801801
- execution_time < 344.80 ms
802-
- max_rss_usage < 562.00 MB
802+
- max_rss_usage < 600.00 MB
803803
- name: otelspan-add-tags
804804
thresholds:
805805
- execution_time < 314.00 ms
806-
- max_rss_usage < 563.50 MB
806+
- max_rss_usage < 600.00 MB
807807
- name: otelspan-get-context
808808
thresholds:
809809
- execution_time < 92.35 ms
@@ -1027,55 +1027,55 @@ experiments:
10271027
# span
10281028
- name: span-add-event
10291029
thresholds:
1030-
- execution_time < 26.20 ms
1030+
- execution_time < 22.50 ms
10311031
- max_rss_usage < 53.00 MB
10321032
- name: span-add-metrics
10331033
thresholds:
1034-
- execution_time < 98.35 ms
1034+
- execution_time < 93.50 ms
10351035
- max_rss_usage < 961.00 MB
10361036
- name: span-add-tags
10371037
thresholds:
1038-
- execution_time < 168.55 ms
1038+
- execution_time < 155.00 ms
10391039
- max_rss_usage < 962.50 MB
10401040
- name: span-get-context
10411041
thresholds:
1042-
- execution_time < 23.70 ms
1042+
- execution_time < 20.50 ms
10431043
- max_rss_usage < 53.00 MB
10441044
- name: span-is-recording
10451045
thresholds:
1046-
- execution_time < 23.90 ms
1046+
- execution_time < 20.50 ms
10471047
- max_rss_usage < 53.00 MB
10481048
- name: span-record-exception
10491049
thresholds:
1050-
- execution_time < 44.50 ms
1050+
- execution_time < 40.00 ms
10511051
- max_rss_usage < 53.00 MB
10521052
- name: span-set-status
10531053
thresholds:
1054-
- execution_time < 26.00 ms
1054+
- execution_time < 22.00 ms
10551055
- max_rss_usage < 53.00 MB
10561056
- name: span-start
10571057
thresholds:
1058-
- execution_time < 23.50 ms
1058+
- execution_time < 20.50 ms
10591059
- max_rss_usage < 53.00 MB
10601060
- name: span-start-finish
10611061
thresholds:
1062-
- execution_time < 55.50 ms
1062+
- execution_time < 52.50 ms
10631063
- max_rss_usage < 34.00 MB
10641064
- name: span-start-finish-telemetry
10651065
thresholds:
1066-
- execution_time < 58.30 ms
1066+
- execution_time < 54.50 ms
10671067
- max_rss_usage < 34.00 MB
10681068
- name: span-start-finish-traceid128
10691069
thresholds:
1070-
- execution_time < 60.05 ms
1070+
- execution_time < 55.00 ms
10711071
- max_rss_usage < 34.00 MB
10721072
- name: span-start-traceid128
10731073
thresholds:
1074-
- execution_time < 24.60 ms
1074+
- execution_time < 22.50 ms
10751075
- max_rss_usage < 53.00 MB
10761076
- name: span-update-name
10771077
thresholds:
1078-
- execution_time < 24.10 ms
1078+
- execution_time < 22.00 ms
10791079
- max_rss_usage < 53.00 MB
10801080

10811081
# telemetryaddmetric

ddtrace/_trace/tracer.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -466,17 +466,19 @@ def _start_span(
466466
child_of = new_ctx
467467

468468
parent: Optional[Span] = None
469+
context: Optional[Context] = None
470+
trace_id: Optional[int] = None
471+
parent_id: Optional[int] = None
472+
469473
if child_of is not None:
470474
if isinstance(child_of, Context):
471475
context = child_of
472476
else:
473477
context = child_of.context
474478
parent = child_of
475-
else:
476-
context = Context(is_remote=False)
477479

478-
trace_id = context.trace_id
479-
parent_id = context.span_id
480+
trace_id = context.trace_id
481+
parent_id = context.span_id
480482

481483
# The following precedence is used for a new span's service:
482484
# 1. Explicitly provided service name
@@ -493,8 +495,8 @@ def _start_span(
493495
# Update the service name based on any mapping
494496
service = config.service_mapping.get(service, service)
495497

496-
links = context._span_links if not parent else []
497-
if trace_id or links or context._baggage:
498+
links = context._span_links if not parent and context else []
499+
if trace_id or links or (context and context._baggage):
498500
# child_of a non-empty context, so either a local child span or from a remote context
499501
span = Span(
500502
name=name,

0 commit comments

Comments
 (0)