dstroy0 / InputHandler

Arduino input handler
https://dstroy0.github.io/InputHandler/
GNU General Public License v3.0
1 stars 0 forks source link

Factor static recomputes to compute once #41

Closed dstroy0 closed 2 years ago

dstroy0 commented 2 years ago

Compute static equations at runtime once instead of n times, assign result to private class member.

Reduce unnecessary overhead.

dstroy0 commented 2 years ago

Most of the overhead is going to be in readCommandFromBuffer and associated helper functions. The max number of data pointers is determined in begin(), and the type match flag array is allocated there as well instead of every iteration of readCommandFromBuffer.

dstroy0 commented 2 years ago

I've been through all the helper functions and public methods that readCommandFromBuffer touches and condensed local parameters into objects that make sense and pass them by reference wherever possible.

dstroy0 commented 2 years ago

the maximum number of parameters passed to a function is now 5, _calcCmdMemcmpRanges is only iterated over inside of addCommand, I think that is acceptable. Internally I tried to limit it to two or three parameters and pass by reference. Having structs like that makes it easier to compare and see if I am duplicating something already there.

dstroy0 commented 2 years ago

Do you have any suggestions? I think I've found everything obvious.

2bndy5 commented 2 years ago

Based on the issue title, I think you could make use of macro functions.

#define rf24_max(a, b) (a > b ? a : b)

But, I'm not sure if this is the best suggestion...

disclaimer: I'm not well trained in C - I know C++ better than C.

dstroy0 commented 2 years ago

I'll look for candidates to turn into function-like macros. Great suggestion. If you see anything let me know.

dstroy0 commented 2 years ago

I think it's a great suggestion because we're doing a ton of repetitive things here and function like macros will probably end up being the fewest total instructions, so it will likely increase performance if we can implement them for even a few things.

dstroy0 commented 2 years ago

Like maybe for a fake iterator like Linux Kernel does?


#define list_for_each_safe(pos, n, head) \
for (pos = (head)->next, n = pos->next; pos != (head); \
     pos = n, n = pos->next)

That might be good.

dstroy0 commented 2 years ago

If I do some iterators and it either increases speed or decreases pgm space I'll consider it a win.

dstroy0 commented 2 years ago

I'm still working on this but it's on the back burner.