Skip to content

[mypyc] feat: new primitive for int.bit_length - #19673

Merged
JukkaL merged 34 commits into
python:masterfrom
BobTheBuidler:bit-length
Oct 14, 2025
Merged

JukkaL merged 34 commits into
python:masterfrom
BobTheBuidler:bit-length

Conversation

@BobTheBuidler

@BobTheBuidler BobTheBuidler commented Aug 16, 2025 •

Copy link
Copy Markdown
Contributor

This PR adds a new primitive for int.bit_length and is already ready for review.

Comment thread mypyc/lib-rt/int_ops.c Resolved
Comment thread mypyc/lib-rt/int_ops.c Resolved

@JukkaL JukkaL left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for the PR! bit_length can be useful in low-level code, where a generic method call can really slow things down.

Comment thread mypyc/lib-rt/int_ops.c Resolved
Comment thread mypyc/lib-rt/int_ops.c Resolved
Comment thread mypyc/test-data/run-integers.test Resolved
@BobTheBuidler

Copy link
Copy Markdown
Contributor Author

I'm not entirely sure how to proceed here, I can't think of any reason our use of getattr would lead to segfault behavior where there was none before

@BobTheBuidler

Copy link
Copy Markdown
Contributor Author

@JukkaL This one I could definitely use some help with though. I have no idea why 1eb2c7e might cause segfault behavior and I'm inclined to say maybe it's a non-issue and we can mark this test as 64-bit only?

@JukkaL JukkaL left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think I found an issue. Also can you measure the perf difference using a micro-benchmark?

Comment thread mypyc/lib-rt/int_ops.c Resolved
Comment thread mypyc/test-data/run-integers.test Resolved
@BobTheBuidler

BobTheBuidler commented Sep 9, 2025 •

Copy link
Copy Markdown
Contributor Author

Also can you measure the perf difference using a micro-benchmark?

Will this benchmark suffice? I can deploy this commit hash and compare against 1.18 which I already have benchmarked here

@BobTheBuidler

Copy link
Copy Markdown
Contributor Author

Also can you measure the perf difference using a micro-benchmark?

Will this benchmark suffice? I can deploy this commit hash and compare against 1.18 which I already have benchmarked here

1.18 vs this branch

There was a ~6% increase in speed for various permutations of the int_to_big_endian benchmark which includes not only a call to bit_length but a decent bit of other stuff as well

@JukkaL JukkaL left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks, looks good now!

@JukkaL
JukkaL merged commit 8e57622 into python:master Oct 14, 2025
@BobTheBuidler
BobTheBuidler deleted the bit-length branch October 14, 2025 12:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants