Skip to content

Optimize response formatter application hot path - #3858

Open
yabirgb wants to merge 1 commit into
ApeWorX:mainfrom
yabirgb:format
Open

Optimize response formatter application hot path#3858
yabirgb wants to merge 1 commit into
ApeWorX:mainfrom
yabirgb:format

Conversation

@yabirgb

@yabirgb yabirgb commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

Avoid allocating a nested helper function on every _apply_response_formatters call and replace repeated formatter membership checks plus indexed lookups with single dict.get() lookups.

This reduces overhead on the common no-formatter path while preserving the existing non-mutating response behavior, result formatting, subscription result formatting, and error formatting.

codex wrote this benchmark https://gist.github.com/yabirgb/e29c583a71f52069522f6fb2d326dd03

that gives

~/Documents/web3.py clean-deps !1 ?3 > uv run python benchs/benchmark_apply_response_formatters.py --number 10000 --repeat 2
warning: No `requires-python` value found in the workspace. Defaulting to `>=3.13`.
number=10000 repeat=2
case           legacy_best_s  current_best_s  speedup  legacy_mean_s  current_mean_s
----------------------------------------------------------------------------------
no_formatter          0.0030          0.0022     1.38x         0.0032          0.0024
result                0.0054          0.0041     1.32x         0.0057          0.0041
subscription          0.0073          0.0067     1.09x         0.0074          0.0074
error                 0.0082          0.0056     1.46x         0.0083          0.0059

Avoid allocating a nested helper function on every
_apply_response_formatters call and replace repeated formatter membership
checks plus indexed lookups with single dict.get() lookups.

This reduces overhead on the common no-formatter path while preserving the
existing non-mutating response behavior, result formatting, subscription
result formatting, and error formatting.
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