speeduino icon indicating copy to clipboard operation
speeduino copied to clipboard

Performance: use cached values instead of recalculating

Open adbancroft opened this issue 1 year ago • 3 comments

In loop(), only recalculate pulse widths, injection angles and ignition angles when the inputs to those calculations have changed. Otherwise use the values from the previous iteration of loop().

Exact speed up varies dependent on tune & rpm. Testing between 3900 & 4100 RPM shows a 15% to 40%+ speed up in loop/s on Mega2560 (tested under various combinations of spark mode, fuel trims etc.).

Log comparison below is 4 cylinder, 60-2 missing tooth, full sequential fuel + fuel trims, sequential spark and RPM 1000 to 12000: avoid_calcs

adbancroft avatar Aug 06 '23 14:08 adbancroft

Quite a lot of changes for implementation of idea that seems quite easy to do by description... Though interested to hear about the tests on that. In some sence seems somewhat dangerous optimization, because of possibility of providing false sence of good loop speed, but it actually falls during transitional conditions when loop speed is actually needed. Talking about the general idea in the headline. No idea about the specific implementation.

TBAMax avatar Aug 06 '23 19:08 TBAMax

Quite a lot of changes for implementation of idea that seems quite easy to do by description...

Totally agree: there was quote a bit of prep work needed make the final implementation "simple". There are only a few commits that really make the PR: ae979dfef3c7ba965789e3920a05e7088a58e09f, 727085679d190183c466196974a9e7d17a666de8, 1129c2ae3c3c4c56e5883744be1b380fe2480f83, ca3c4dc488ee85187081222e3803a18cc87a53aa

adbancroft avatar Aug 06 '23 20:08 adbancroft

In some sence seems somewhat dangerous optimization

Yes - it's a little fragile (easy for future PRs to break it) and and the data flow/dependency flow is (possibly?) complex for maintainers who aren't familiar with the code base.

But the performance gain is big enough to justify it IMO

adbancroft avatar Aug 06 '23 20:08 adbancroft