Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor INP attribution code to fix errors on Windows 10 #495

Merged
merged 2 commits into from
Jun 11, 2024

Conversation

brendankenny
Copy link
Member

@brendankenny brendankenny commented Jun 11, 2024

UPDATE (2024-06-20): while intended to just be an internal refactor, the changes in this PR do appear to fix the source of errors being thrown for some users on Windows 10. The cause of these errors is still unknown.


I wanted to do this while the code was still fresh in my mind. There are no expected functional changes here, just code simplification.

Currently, previousRenderTimes is used for easier lookup into event-timing-entry groups in pendingEntriesGroupMap and for quick event grouping by render time, but it's also not quite the same set of render times in pendingEntriesGroupMap (the times and groups grow together as events come in, but the map actually contains all the events referenceable by previousRenderTimes and groups with events from longestInteractionList). The results in more bookkeeping and (speaking as someone recently getting their head around this code :) it can make some of this logic harder to follow because effects aren't always collocated.

This PR proposes getting rid of previousRenderTimes so that there are only two main data structures, an array tracking LoAFs and an array tracking groups of event timing entries. I think this makes it a little simpler to understand how events are grouped as they come in and which event groups are being preserved during cleanup, without a runtime or clarity tradeoff elsewhere.

latestProcessingEnd and the entry WeakMap are still here as secondary data sources because they're convenient ways to skip extra work and they're both only ever updated in a single place (the WeakMap now maps directly to entry groups, though).

@@ -35,12 +35,13 @@ interface pendingEntriesGroup {
startTime: DOMHighResTimeStamp;
processingStart: DOMHighResTimeStamp;
processingEnd: DOMHighResTimeStamp;
renderTime: DOMHighResTimeStamp;
Copy link
Member Author

Choose a reason for hiding this comment

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

technically the code below could use group.entries[0].startTime + group.entries[0].duration and not add a new property here, but it seemed better to put this here as a convenience

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, new property is better I think.

src/attribution/onINP.ts Outdated Show resolved Hide resolved
Copy link
Member

@philipwalton philipwalton left a comment

Choose a reason for hiding this comment

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

Thanks @brendankenny! Overall these changes LGTM andI think do simplify things.

@@ -35,12 +35,13 @@ interface pendingEntriesGroup {
startTime: DOMHighResTimeStamp;
processingStart: DOMHighResTimeStamp;
processingEnd: DOMHighResTimeStamp;
renderTime: DOMHighResTimeStamp;
Copy link
Member

Choose a reason for hiding this comment

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

Yeah, new property is better I think.

// 2) occur after the most recently-processed event entry.
const loafsToKeep: Set<PerformanceLongAnimationFrameTiming> = new Set();
pendingEntriesGroupMap.forEach((group) => {
for (let i = 0; i < pendingEntriesGroups.length; i++) {
Copy link
Member

Choose a reason for hiding this comment

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

Didn't like a forEach() right above a regular for loop, eh? :)

You could alternatively do something like this to keep it one line (and which is common in the Google JS style guide):

for (let i = 0, group; group = pendingEntriesGroups[i]; i++) {

@brendankenny brendankenny merged commit 9ef3307 into main Jun 11, 2024
8 checks passed
@brendankenny brendankenny deleted the remove-some-things branch June 11, 2024 21:26
@philipwalton philipwalton changed the title Small simplification refactor of INP attribution Refactor INP attribution code to fix errors on Windows 10 Jun 20, 2024
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.

None yet

2 participants