Batch and error check allocations #3224
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I was looking into optimization opportunities and found some allocation requests (mallocs and PyMem_News) right next to each other, and I thought surely calling the function once will be faster than calling it twice. It is also simpler to check if one buffer failed to allocate, and simpler to free one buffer at the end, rather than 2 or 3.
Looking through the code I also saw that the malloc statements in the blur routines weren't error checked at all, so I fixed that.
Running very favorable microbenchmarks I saw very small performance increases, like 1-3%, or no impact. I'm unsure if running a tight loop over identical calls reduces the execution time burden of malloc / PyMem_New, so maybe performance would be affected differently in a more realistic scenario?
Anyways since this is a small thing I'm not trying to pitch this as a performance PR, instead this is a code quality PR. We want our code to be the best it can be, and I think that reducing the number of allocation calls helps that goal.