Skip to content

Convert to safe JSON without a closure per call - #746

Merged
RazerM merged 1 commit into
mainfrom
feature/json-safe-no-closure
Nov 10, 2026
Merged

RazerM merged 1 commit into
mainfrom
feature/json-safe-no-closure

Conversation

@RazerM

@RazerM RazerM commented Nov 10, 2026

Copy link
Copy Markdown
Collaborator

to_safe_json(), which LogRecord.to_dict(json_safe=True) uses, defined a nested _convert function on every call. _convert refers to itself through its closure, so each call left a function, a cell and a tuple in a reference cycle for the garbage collector. to_safe_json() now calls itself instead.

On CPython 3.14, record.to_dict(json_safe=True) takes 6.7 µs instead of 7.2 µs, and logging through a handler that exports each record that way is about 4% faster.

The recursive closure formed a reference cycle, so each conversion left
three objects for the cyclic garbage collector.
@RazerM
RazerM force-pushed the feature/json-safe-no-closure branch from da069d7 to b5de7db Compare November 10, 2026 21:56
@codspeed

codspeed Bot commented Nov 10, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ 3 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 38 untouched benchmarks


Comparing feature/json-safe-no-closure (b5de7db) with main (baad5e2)

Open in CodSpeed

@codecov

codecov Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.76%. Comparing base (baad5e2) to head (b5de7db).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #746      +/-   ##
==========================================
- Coverage   91.76%   91.76%   -0.01%     
==========================================
  Files          49       49              
  Lines        8153     8151       -2     
  Branches      622      622              
==========================================
- Hits         7482     7480       -2     
  Misses        538      538              
  Partials      133      133              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@RazerM
RazerM merged commit cbf131d into main Nov 10, 2026
51 of 52 checks passed
@RazerM
RazerM deleted the feature/json-safe-no-closure branch November 10, 2026 22:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant