memory.c cleanup and optimisation#1157
Conversation
Oh, come on... Why did you have to bring that up again?
That is absolutely WRONG. You reversed the logic AGAIN. Not once, not twice, but THREE times. So, because it seems it's not clear enough, here we go again. Let's take the array from your "Test logic":
This benchmark is not completely relevant. You gain a 80% speed boost only when the count is lower than the stack allocated buffer size. For all the other cases, it's actually slightly worse, because of the branch you introduce with the Currently, this function is only used for SOC surfaces and surface nodes. Which has barely any usage. So this optimization trick doesn't even matter. I'm tired of arguing over the most pointless stuff and explaining it again and again. |
Low hanging fruit optimisations for the memory data structures in
memory.cMostly generic performance improvements:
calloccalls formalloccalls where it's safenode + bufferreallocinstead of fullcalloc+memcpy+free.Revised
growing_array_moveThere's been a lot of discussion about this in the past, and I have decided this PR is a good opportunity to push this more formally.
You can find my final message on this topic here: #924 (comment).
The TLDR is that the confusion is mostly just a disagreement on what the parameter
toshould refer to. There are two ways one might imaginetoshould be:toshould be relative to the array before any operation is performed.toshould be relative to the array after collapsing the removed block.Currently without this PR, it uses the logic where
tois relative to the array after collapsing the removed block.This PR switches to the logic that
toshould be relative to the array before any operation is performed.Because it's more intuitive. It's what a developer should be able to expect from an API perspective.
Developers shouldn't have to mentally simulate where
towill be during the internal operation of the function.I have added a version of the function that treats
toas the destination index before any operations.This version also has a new optimisation which uses a stack buffer for smaller, faster operations but uses dynamic memory when dealing with larger blocks.
The return type was also changed to boolean, which will allow callers to check if the move succeeded.
I've tested both the performance and the logic: