8000 bpo-39943: Use calloc-based functions, not malloc+memset by petdance · Pull Request #19152 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-39943: Use calloc-based functions, not malloc+memset #19152

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 1 commit into from
Mar 25, 2020
Merged

bpo-39943: Use calloc-based functions, not malloc+memset #19152

merged 1 commit into from
Mar 25, 2020

Conversation

petdance
Copy link
Contributor
@petdance petdance commented Mar 25, 2020

This consolidates a number of malloc+memset function calls to use their calloc-based equivalents.

https://bugs.python.org/issue39943

@vstinner
Copy link
Member

Once I ran some microbenchmarks and it wasn't clear if calloc() was faster than malloc()+memset(0). It should, but I failed to measure such speedup. Well, the change is correct ;-)

https://bugs.python.org/issue21233#msg217253

Sadly, pymalloc allocator has no efficient of calloc yet: it's just malloc+memset(0) in internally. But it should help for allocations larger than 512 bytes (not using pymalloc).

@petdance
Copy link
Contributor Author

My concern wasn't speed, but in DRY and minimization of code. Still, a speedup would be nice if it happens.

@petdance petdance deleted the calloc branch March 25, 2020 14:48
@benjaminp
Copy link
Contributor

If the compiler sees the memset, it may be able to generate more efficient zeroing instructions inline. I imagine it would be hard to prove any different in CPython, though.

I like calloc because it provides a way to avoid some integer overflows.

@petdance
Copy link
Contributor Author

The memsets are still there. They're just inside the *calloc functions.

@vstinner
Copy link
Member

The memsets are still there. They're just inside the *calloc functions.

For memory allocations larger than 512 bytes, libc malloc() is used and the libc is free to avoid memset if it knows that the memory is already initialized with zeros. I expect that memory allocated by mmap() doesn't provide uninitialized memory for example.

This change should not make slower, but it's ok if it doesn't make Python faster neither. I expect that in some cases, it may be faster. But I don't bother enough to run exhaustive microbenchmarks ;-)

I like calloc because it provides a way to avoid some integer overflows.

Sure, that's a cool enhancement since we had many security issues related to integer overflow in the past :-/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants
0