aREST
aREST copied to clipboard
Response time is slow with large numbers of variables/functions
My applications has a large number of variables (~40) and functions (~15). As a result, response times to queries are 10-15ms on an Arduino Mega running at 16 MHz. I propose using qsort() and bsearch() to speed up the response time.
Really interesting, I never thought about that many functions :) Have you actually tried modifying the code with qsort() and bsearch() ?
Not yet -- it turned out to be more complicated than it looked like at first once I started thinking about it. In particular, you have to make sure to either trim off the parameters and trailing matter before you search or write a custom comparison function. It also requires associating names and pointers together in a small struct, so that qsort() does the right thing. Therefore, I'm plucking lower hanging fruit before I dive into that.
The big one I noticed is that it doesn't stop searching once it's found the items it's looking for. The master branch checks every single member of every array, every time. That alone is a big speed boost. I got a lot of the speed boost I needed from that combined with the following steps:
a) Renamed by variables and functions so that each group started with a different character (i_ for integers, d_ for floats, f_ for functions) to make skipping certainly wrong options faster. b) Optimizing the ordering of items so that the most used ones come first. c) Reordering the library code so functions are checked first.
With those changes, I knocked off between half and two-thirds of the lag. I think the only part that really makes sense to turn into a PR is the change that allows the library to skip past the rest once the desired items.
Ok, a little more poking and prodding, and I've found another slow down. It takes a few hundred microseconds per character for the read loop inside handle_proto to execute. The only reasonable explanation I can think of is that the line answer = answer + c;
is taking a ridiculously long time to execute. I think I am going to have to spend some time digging into WString and some time toggling pins to figure out why.