Speed up orjson buffer trimming (#181522)

This commit is contained in:
Erik Montnemery
2026-09-07 12:13:54 +02:00
committed by GitHub
parent 9c0d3609ab
commit c9678e447b
+7 -5
View File
@@ -122,10 +122,11 @@ def cached_json_bytes(data: Any) -> bytes:
orjson over-allocates the returned bytes buffer and does not shrink it: the
logical length is set but the capacity is rounded up to a power of two (at
least a few KiB), so bytes cached for the lifetime of a long-lived object
retain several KiB of unused buffer.
retain several KiB of unused buffer. Copy them into a right-sized buffer.
"""
# Drop orjson's over-allocated slack with help of a memoryview.
return bytes(memoryview(json_bytes(data)))
# The empty second join item is load-bearing: it forces a copy into a
# right-sized buffer; a single-item join returns the input unchanged.
return b"".join((json_bytes(data), b""))
def cached_json_fragment(data: Any) -> orjson.Fragment:
@@ -134,8 +135,9 @@ def cached_json_fragment(data: Any) -> orjson.Fragment:
Wraps the same right-sized bytes as cached_json_bytes; the body is inlined
rather than calling it to avoid an extra function call on this hot path.
"""
# Drop orjson's over-allocated slack with help of a memoryview.
return orjson.Fragment(bytes(memoryview(json_bytes(data))))
# The empty second join item is load-bearing: it forces a copy into a
# right-sized buffer; a single-item join returns the input unchanged.
return orjson.Fragment(b"".join((json_bytes(data), b"")))
def json_dumps(data: Any) -> str: