Skip to content

Conversation

@mrxz
Copy link
Contributor

@mrxz mrxz commented Feb 27, 2024

Description:
The implementation of throttleLeadingAndTrailing was very inefficient when many updates took place in short succession. Virtually all invocations would result in clearing a timer, allocating a new function and creating a new timer. This impacted both the performance and memory usage of setAttribute.

Using the performance/set-attribute and performance/animation-set-attribute benchmarks showed a clear performance benefit (when congested) cutting the duration in half (~9.16µs/op -> ~4.04µs/op) and reduced memory consumption:
image

Instead of allocating a new function for each timer, one is created in advance. In case calls take place within the minimumInterval only one timer is created and re-used until it expires ensuring at most one timer per interval (if needed).

Changes proposed:

  • Pre-allocate timeout callback function
  • Avoid clearing and recreating timeout and instead create and re-use one timeout per interval (if needed)
@mrxz mrxz force-pushed the optimize-throttle-leading-and-trailing branch from aae9b6c to 3449ca7 Compare February 27, 2024 12:52
}, minimumInterval - sinceLastTime);
// Inside minimum interval, create timer if needed.
if (typeof deferTimer === 'undefined') {
deferTimer = setTimeout(timerExpired, minimumInterval - sinceLastTime);
Copy link
Member

@dmarcos dmarcos Feb 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can do a single line

deferTimer = deferTimer || setTimeout(timerExpired, minimumInterval - sinceLastTime);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@mrxz mrxz force-pushed the optimize-throttle-leading-and-trailing branch from 3449ca7 to 4c45497 Compare February 27, 2024 22:25
@dmarcos
Copy link
Member

dmarcos commented Mar 18, 2024

Thanks!

@dmarcos dmarcos merged commit fe238e7 into aframevr:master Mar 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants