Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .changelog/5288.changed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
opentelemetry-api: remove unnecessary copy in iterator
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ def __delitem__(self, key: str) -> None:

def __iter__(self): # type: ignore
with self._lock:
return iter(self._dict.copy()) # type: ignore
return iter(list(self._dict)) # type: ignore

def __len__(self) -> int:
return len(self._dict)
Expand Down
23 changes: 23 additions & 0 deletions opentelemetry-sdk/benchmarks/trace/test_benchmark_trace.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# Copyright The OpenTelemetry Authors
# SPDX-License-Identifier: Apache-2.0

import tracemalloc
from functools import lru_cache

import pytest

from opentelemetry.attributes import BoundedAttributes
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import (
ReadableSpan,
Expand Down Expand Up @@ -192,3 +194,24 @@ def benchmark_read_links():
span.end()

benchmark(benchmark_read_links)


@pytest.mark.parametrize("num_attrs", [1, 10, 50, 128])
def test_bounded_attribute_iterator(benchmark, num_attrs):
attrs = BoundedAttributes(
attributes={f"key{i}": f"value{i}" for i in range(num_attrs)}
)

peaks = []
for _ in range(200):
tracemalloc.start()
list(attrs)
_, peak = tracemalloc.get_traced_memory()
tracemalloc.stop()
peaks.append(peak)
benchmark.extra_info["mean_alloc_bytes"] = sum(peaks) / len(peaks)

def benchmark_iter():
list(attrs)

benchmark(benchmark_iter)
Loading