From 32d8b471e4e2ffdc7c73b08e894e2f36a8035907 Mon Sep 17 00:00:00 2001 From: Mark Colley <39366189+M-Colley@users.noreply.github.com> Date: Sat, 28 Nov 2026 10:47:52 +0000 Subject: [PATCH] perf(serializer): skip cycle memoization for atomic types Fixes #8091 Signed-off-by: Mark Colley <39366189+M-Colley@users.noreply.github.com> --- sentry_sdk/serializer.py | 14 +++++++++++ tests/test_serializer.py | 53 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/sentry_sdk/serializer.py b/sentry_sdk/serializer.py index f4867f39b0..dc60053e31 100644 --- a/sentry_sdk/serializer.py +++ b/sentry_sdk/serializer.py @@ -30,6 +30,10 @@ # Bytes are technically not strings in Python 3, but we can serialize them serializable_str_types = (str, bytes, bytearray, memoryview) +# Exact built-in atomic types cannot hold references or form cycles, so +# cycle-detection memoization can be skipped for them. +_ATOMIC_TYPES = (str, int, float, bool, type(None)) + # Maximum length of JSON-serialized event payloads that can be safely sent # before the server may reject the event due to its size. This is not intended @@ -199,6 +203,16 @@ def _serialize_node( self.path.append(segment) try: + if type(obj) in _ATOMIC_TYPES: + return self._serialize_node_impl( + obj, + is_databag=is_databag, + is_request_body=is_request_body, + should_repr_strings=should_repr_strings, + remaining_depth=remaining_depth, + remaining_breadth=remaining_breadth, + ) + with self.memo.memoize(obj) as result: if result: return CYCLE_MARKER diff --git a/tests/test_serializer.py b/tests/test_serializer.py index f1483aba8d..c0be3949e1 100644 --- a/tests/test_serializer.py +++ b/tests/test_serializer.py @@ -219,3 +219,56 @@ def __iter__(self): assert result["custom"].startswith( ".Custom object at" ) + + +def test_atomic_values_skip_memo(monkeypatch): + from sentry_sdk.serializer import Memo + + memoized_types = [] + orig_memoize = Memo.memoize + + def counting_memoize(self, obj): + memoized_types.append(type(obj)) + return orig_memoize(self, obj) + + monkeypatch.setattr(Memo, "memoize", counting_memoize) + + class CustomStr(str): + def __sentry_repr__(self): + return "custom-str" + + cyclic_list = [] + cyclic_list.append(cyclic_list) + + payload = { + "extra": { + "s": "hello", + "i": 42, + "f": 3.14, + "inf": float("inf"), + "b": True, + "n": None, + "sub": CustomStr("abc"), + "nested": [1, "two", None, {"k": False}], + "cycle": cyclic_list, + } + } + + result = serialize(payload) + + assert result["extra"]["s"] == "hello" + assert result["extra"]["i"] == 42 + assert result["extra"]["f"] == 3.14 + assert result["extra"]["inf"] == "inf" + assert result["extra"]["b"] is True + assert result["extra"]["n"] is None + assert result["extra"]["sub"] == "custom-str" + assert result["extra"]["nested"] == [1, "two", None, {"k": False}] + assert result["extra"]["cycle"] == [""] + + for atomic_type in (str, int, float, bool, type(None)): + assert atomic_type not in memoized_types + + assert CustomStr in memoized_types + assert dict in memoized_types + assert list in memoized_types