opentelemetry-sdk: remove unnecessary dict in set_attribute method#5274
Merged
lzchen merged 3 commits intoJun 8, 2026
Merged
Conversation
set_attribute was unnecessarily creating a dict to set the attribute via set_attributes. removed that call and instead put the code from set_attributes directly into set_attribute. there is a small amount of time saved from doing this, the memory footprint is essentially the same: Ran: ``` pytest opentelemetry-sdk/benchmarks/trace/test_benchmark_trace.py::test_set_attribute \ --memray --trace-python-allocators \ --benchmark-min-rounds=10 --benchmark-columns=mean,median,ops ``` And got the following results: | attrs | Before mean (µs) | After mean (µs) | Time saved | Before mem (KiB) | After mem (KiB) | Mem saved | |------|-----------------|----------------|-----------|-----------------|----------------|----------| | 1 | 22.7 | 21.6 | 5% | 176.8 | 174.6 | 1.2 | | 10 | 39.9 | 34.4 | 14% | 356.0 | 408.9 | -52.9 | | 50 | 117.6 | 90.4 | 23% | 190.9 | 240.1 | -49.2 | | 128 | 266.3 | 199.3 | 25% | 115.3 | 139.9 | -24.6 | Signed-off-by: Alex Boten <223565+codeboten@users.noreply.github.com>
7 tasks
MikeGoldsmith
approved these changes
Jun 5, 2026
Member
There was a problem hiding this comment.
I like the change. I wasn't expecting to see memory usage to grow inline with more attributes though. Any thoughts @codeboten?
Contributor
Author
The memory usage recorded was the result of the benchmarks running more rounds through the test (because of the time savings 🤦🏻 ) re-ran it with an exact number of rounds and the memory is essentially the same:
|
lzchen
approved these changes
Jun 8, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
set_attributewas unnecessarily creating a dict to set the attribute viaset_attributes. removed that call and instead put the code fromset_attributesdirectly intoset_attribute. there is a small amount of time saved from doing this, the memory footprint is essentially the same:Type of change
Please delete options that are not relevant.
How Has This Been Tested?
Ran:
And got the following results:
Does This PR Require a Contrib Repo Change?
Checklist: