Skip to content

feat: optimize apply_abi_formatters_to_dict #3671

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/3671.performance.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
optimize web3._utils.rpc_abi.apply_abi_formatters_to_dict
9 changes: 5 additions & 4 deletions web3/_utils/rpc_abi.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,12 @@ def apply_abi_formatters_to_dict(
fields = list(abi_dict.keys() & data.keys())
formatted_values = map_abi_data(
normalizers,
[abi_dict[field] for field in fields],
[data[field] for field in fields],
(abi_dict[field] for field in fields),
(data[field] for field in fields),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 less list creations per call

)
formatted_dict = dict(zip(fields, formatted_values))
return dict(data, **formatted_dict)
formatted_dict = data.copy()
formatted_dict.update(zip(fields, formatted_values))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of fully initializing a formatted_dict and then initializing a second new dict with it, we can just init one copy of the original dict and update it from a zip

return formatted_dict


@to_dict
Expand Down