FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Avoid allocating functions and use at most one timer per interval in throttleLeadingAndTrailing by mrxz · Pull Request #5476 · aframevr/aframe · GitHub

Avoid allocating functions and use at most one timer per interval in throttleLeadingAndTrailing - #5476

Merged
dmarcos merged 1 commit into
aframevr:masterfrom
mrxz:optimize-throttle-leading-and-trailing
Mar 18, 2024
Merged

Avoid allocating functions and use at most one timer per interval in throttleLeadingAndTrailing#5476
dmarcos merged 1 commit into
aframevr:masterfrom
mrxz:optimize-throttle-leading-and-trailing

Conversation

mrxz commented Feb 27, 2024
edited
Loading

Copy link
Copy Markdown
Contributor

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:

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 force-pushed the optimize-throttle-leading-and-trailing branch from aae9b6c to 3449ca7 Compare February 27, 2024 12:52
Comment thread src/utils/index.js Outdated
Comment thread src/utils/index.js Outdated
}, minimumInterval - sinceLastTime);
// Inside minimum interval, create timer if needed.
if (typeof deferTimer === 'undefined') {
deferTimer = setTimeout(timerExpired, minimumInterval - sinceLastTime);

dmarcos Feb 27, 2024
edited
Loading

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

can do a single line

deferTimer = deferTimer || setTimeout(timerExpired, minimumInterval - sinceLastTime);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Done

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

dmarcos commented Mar 18, 2024

Copy link
Copy Markdown
Member

Thanks!

dmarcos merged commit fe238e7 into aframevr:master Mar 18, 2024
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants


Back | FazBrowse Home | New Git URL