Skip to content

[mypyc] Add primitive for int to ascii conversion#21036

Merged
p-sawicki merged 2 commits intopython:masterfrom
p-sawicki:int-to-ascii-bytes
Mar 18, 2026
Merged

[mypyc] Add primitive for int to ascii conversion#21036
p-sawicki merged 2 commits intopython:masterfrom
p-sawicki:int-to-ascii-bytes

Conversation

@p-sawicki
Copy link
Collaborator

Added a primitive to convert ints into bytes objects suitable for bytes format strings, eg. b'%d' % my_int. Works the same as the existing CPyTagged_Str primitive used for formatting str objects, except it returns a bytes object instead of str.

Currently only the bare %d specifier is supported, other integer specifiers and %d with options go through the regular CPython handling of format strings.

Results in around 23% reduced runtime in this microbenchmark:

import time

t0 = time.time()
for i in range(10_000_000):
    val = b'%d' % i
print(time.time() - t0)

Results:

setup runtime (s)
interpreted 0.90
mypyc master 0.92
mypyc PR 0.69

Copy link
Collaborator

@JukkaL JukkaL left a comment

Choose a reason for hiding this comment

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

Nice! I saw a 1.7x perf improvement when I did the benchmark compuation in a function (to avoid slow globals access).

@p-sawicki p-sawicki merged commit 0183a21 into python:master Mar 18, 2026
17 checks passed
@p-sawicki p-sawicki deleted the int-to-ascii-bytes branch March 18, 2026 15:36
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.

2 participants