Skip to content

Commit 32d8b47

Browse files
committed
perf(serializer): skip cycle memoization for atomic types
Fixes #8091 Signed-off-by: Mark Colley <39366189+M-Colley@users.noreply.github.com>
1 parent 566beb0 commit 32d8b47

2 files changed

Lines changed: 67 additions & 0 deletions

File tree

‎sentry_sdk/serializer.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
# Bytes are technically not strings in Python 3, but we can serialize them
3131
serializable_str_types = (str, bytes, bytearray, memoryview)
3232

33+
# Exact built-in atomic types cannot hold references or form cycles, so
34+
# cycle-detection memoization can be skipped for them.
35+
_ATOMIC_TYPES = (str, int, float, bool, type(None))
36+
3337

3438
# Maximum length of JSON-serialized event payloads that can be safely sent
3539
# before the server may reject the event due to its size. This is not intended
@@ -199,6 +203,16 @@ def _serialize_node(
199203
self.path.append(segment)
200204

201205
try:
206+
if type(obj) in _ATOMIC_TYPES:
207+
return self._serialize_node_impl(
208+
obj,
209+
is_databag=is_databag,
210+
is_request_body=is_request_body,
211+
should_repr_strings=should_repr_strings,
212+
remaining_depth=remaining_depth,
213+
remaining_breadth=remaining_breadth,
214+
)
215+
202216
with self.memo.memoize(obj) as result:
203217
if result:
204218
return CYCLE_MARKER

‎tests/test_serializer.py‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,3 +219,56 @@ def __iter__(self):
219219
assert result["custom"].startswith(
220220
"<tests.test_serializer.test_serialize_local_vars.<locals>.Custom object at"
221221
)
222+
223+
224+
def test_atomic_values_skip_memo(monkeypatch):
225+
from sentry_sdk.serializer import Memo
226+
227+
memoized_types = []
228+
orig_memoize = Memo.memoize
229+
230+
def counting_memoize(self, obj):
231+
memoized_types.append(type(obj))
232+
return orig_memoize(self, obj)
233+
234+
monkeypatch.setattr(Memo, "memoize", counting_memoize)
235+
236+
class CustomStr(str):
237+
def __sentry_repr__(self):
238+
return "custom-str"
239+
240+
cyclic_list = []
241+
cyclic_list.append(cyclic_list)
242+
243+
payload = {
244+
"extra": {
245+
"s": "hello",
246+
"i": 42,
247+
"f": 3.14,
248+
"inf": float("inf"),
249+
"b": True,
250+
"n": None,
251+
"sub": CustomStr("abc"),
252+
"nested": [1, "two", None, {"k": False}],
253+
"cycle": cyclic_list,
254+
}
255+
}
256+
257+
result = serialize(payload)
258+
259+
assert result["extra"]["s"] == "hello"
260+
assert result["extra"]["i"] == 42
261+
assert result["extra"]["f"] == 3.14
262+
assert result["extra"]["inf"] == "inf"
263+
assert result["extra"]["b"] is True
264+
assert result["extra"]["n"] is None
265+
assert result["extra"]["sub"] == "custom-str"
266+
assert result["extra"]["nested"] == [1, "two", None, {"k": False}]
267+
assert result["extra"]["cycle"] == ["<cyclic>"]
268+
269+
for atomic_type in (str, int, float, bool, type(None)):
270+
assert atomic_type not in memoized_types
271+
272+
assert CustomStr in memoized_types
273+
assert dict in memoized_types
274+
assert list in memoized_types

0 commit comments

Comments
 (0)