Skip to content

Use PyString::from_fmt to avoid intermediate String allocations#14248

Merged
reaperhulk merged 1 commit intopyca:mainfrom
alex:use-pystring-from-fmt
Feb 14, 2026
Merged

Use PyString::from_fmt to avoid intermediate String allocations#14248
reaperhulk merged 1 commit intopyca:mainfrom
alex:use-pystring-from-fmt

Conversation

@alex
Copy link
Member

@alex alex commented Feb 2, 2026

Replace format! + String with PyString::from_fmt + format_args! in repr methods and one PyString::new call, writing directly to a Python string buffer (1 alloc) instead of going through format!() -> String -> PyString (2 allocs + copy). This optimization is only effective on Python 3.14+.

Replace `format!` + String with `PyString::from_fmt` + `format_args!`
in __repr__ methods and one PyString::new call, writing directly to
a Python string buffer (1 alloc) instead of going through
format!() -> String -> PyString (2 allocs + copy). This optimization
is only effective on Python 3.14+.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@reaperhulk reaperhulk merged commit 862eaf3 into pyca:main Feb 14, 2026
67 checks passed
@alex alex deleted the use-pystring-from-fmt branch February 14, 2026 22:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants