Skip to content

perf(sdk): reduce span creation overhead - #5824

Open
herin049 wants to merge 1 commit into
open-telemetry:mainfrom
herin049:perf/spans
Open

herin049 wants to merge 1 commit into
open-telemetry:mainfrom
herin049:perf/spans

Conversation

@herin049

@herin049 herin049 commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Description

Reduces per-span overhead in the SDK hot path. Most performance gains where achieved by reordering branch statements, inlining a few function calls and switching from keyword to positional calling where possible.

Note: There are some very minor/subtle breaking changes with adding __slots__ on some classes (i.e. __dict__ goes away on Span objects). Likewise, the bounded attributes change does switch the default from None to sys.maxsize, but this one is extremely unlikely to break any existing code. If we're concerned about the __slots__ changes, we can remove this from the PR.

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Benchmarks (MacBook M5, CPython 3.14)

opentelemetry-sdk/benchmarks/trace/test_benchmark_trace.py

Benchmark Before After Δ abs Δ rel
start_span_with_attributes[0] 4.10 µs 3.35 µs −0.75 µs −18.3%
start_span_with_attributes[1] 5.04 µs 3.67 µs −1.37 µs −27.3%
start_span_with_attributes[10] 6.85 µs 4.60 µs −2.25 µs −32.8%
start_span_with_attributes[50] 15.50 µs 8.85 µs −6.65 µs −42.9%
start_span_with_attributes[128] 31.29 µs 16.87 µs −14.42 µs −46.1%
simple_start_span 5.92 µs 4.44 µs −1.48 µs −25.0%
simple_start_as_current_span 7.35 µs 5.85 µs −1.50 µs −20.4%
set_attribute[10] 7.96 µs 6.04 µs −1.92 µs −24.1%
read_events[10] 19.12 µs 13.29 µs −5.83 µs −30.5%
set_attribute_types[str] 251.45 ms 181.52 ms −69.93 ms −27.8%
set_attribute_types[seq_int] 710.96 ms 749.61 ms +38.66 ms +5.4%

Per span, memory drops by 56 B: the span object goes from 344 B to 224 B, and SpanContext from 112 B to 96 B.

Checklist:

See contributing.md for styleguide, changelog guidelines, and more.

  • I am a human and not a bot
  • I have run tox -e precommit
  • I have run tox -e typecheck
  • I have run the relevant tests and linting tox environments
  • This requires the following PR in contrib:

@herin049
herin049 requested a review from a team as a code owner October 25, 2026 03:19
@opentelemetry-pr-dashboard

opentelemetry-pr-dashboard Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

Pull request dashboard status

Waiting on the author · refreshed 2026-11-01 04:26 UTC

Resolve merge conflicts.

Two things need attention:

  • Required checks are failing — investigate the failures.
  • 1 review item — respond to each (e.g. link a commit, explain why not, ask a follow-up):
    • Inline threads: 1
Status above doesn't look right?
  • Just replied or pushed? Anything around or after the refresh time above may not be picked up yet — give it a few minutes.
  • Should this be with reviewers? Comment /dashboard route:reviewers to route it to them.
  • Anything wrong — including the routing? Report it with what you expected; it helps us improve the dashboard.

if resource is None:
resource = Resource.create({})
super().__init__(
name=name,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we quantify how the named arguments contribute to this? Not a fan on making things less readable.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC it does result in a >10% speedup for Span creation. I'll add benchmarks breaking it down. I agree though, switching to positional args makes it hard to read, if you'd prefer we remove it that's OK with me.

herin049 commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor Author

Contribution of positional arguments

Apple M5 Pro, CPython 3.14.5

Benchmark Baseline (existing) Optimized, keyword calls Optimized, positional calls Positional saving Time reduction
start_span_with_attributes[0] 4.10 µs 3.89 µs 3.13 µs +0.76 µs +19.6%
start_span_with_attributes[1] 5.04 µs 4.14 µs 3.38 µs +0.75 µs +18.2%
start_span_with_attributes[10] 6.85 µs 5.11 µs 4.38 µs +0.73 µs +14.4%
start_span_with_attributes[50] 15.50 µs 9.27 µs 8.55 µs +0.72 µs +7.8%
start_span_with_attributes[128] 31.29 µs 16.97 µs 16.25 µs +0.72 µs +4.2%
simple_start_span 5.92 µs 4.99 µs 4.27 µs +0.72 µs +14.4%
simple_start_as_current_span 7.35 µs 6.48 µs 5.67 µs +0.80 µs +12.4%
set_attribute[10] 7.96 µs 6.58 µs 5.81 µs +0.77 µs +11.7%
read_events[10] 19.12 µs 13.69 µs 13.05 µs +0.64 µs +4.7%
set_attribute_types[str] 251.45 ms 176.58 ms 172.61 ms +3.97 ms +2.2%
set_attribute_types[seq_int] 710.96 ms 709.95 ms 706.70 ms +3.26 ms +0.5%

@xrmx

@opentelemetry-pr-dashboard

Copy link
Copy Markdown

Hi @herin049 — just a friendly reminder that this pull request is waiting on you. The dashboard status comment has the open items and is kept current.

  • Replying is enough to hand it off — answer, explain why no change is needed, or ask a follow-up. The dashboard routes it onward once nothing on the list is waiting on you.
  • To hand it back for any other reason, including the dashboard getting this wrong, comment /dashboard route:reviewers.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

2 participants