v923z / micropython-ulab

a numpy-like fast vector module for micropython, circuitpython, and their derivatives
https://micropython-ulab.readthedocs.io/en/latest
MIT License
432 stars 117 forks source link

Make optimize's bisect,fmin,newton use C stack instead of heap #536

Closed dpgeorge closed 2 years ago

dpgeorge commented 2 years ago

This is a simple change to reduce the use of heap allocation in optimize.bisect, optimize.fmin and optimize.newton.

v923z commented 2 years ago

@dpgeorge Thanks! I see that my use of the heap is a bit sloppy and ad-hoc. I'll try to watch out for this in the future.

dpgeorge commented 2 years ago

I did review all the memory allocations in ulab and these were the ones that could be eliminated.

v923z commented 2 years ago

Actually, now that you mention it, things like https://github.com/v923z/micropython-ulab/blob/2b4292abcc4f22acf242b2b4656a65cd124f1a5e/code/ndarray.c#L910 could be taken out, because ULAB_MAX_DIMS is known at compile time. I don't see any reason for dynamically allocating the memory. N.B., my comment is not a hint at anything.

dpgeorge commented 2 years ago

Yes, that does look like a temporary shape array that can be allocated on the C stack.

v923z commented 2 years ago

OK, thanks for the confirmation! I'll try to weed these instances out.