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

Remove extra loop (?) #11889

Merged
merged 2 commits into from
Jan 5, 2018
Merged

Remove extra loop (?) #11889

merged 2 commits into from
Jan 5, 2018

Conversation

jquense
Copy link
Contributor

@jquense jquense commented Dec 19, 2017

Am I just off base here? It looks like the for loop's condition is the inverse of the while loop. Presumably parents will have no other elements in it with cached instances since that's the condition for building said array?

let inst = node[internalInstanceKey];
if (inst.tag === HostComponent || inst.tag === HostText) {
// In Fiber, this will always be the deepest root.
return inst;
}
for (; node && (inst = node[internalInstanceKey]); node = parents.pop()) {
closest = inst;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've tried to check back through the history to see what was going on here, and at least originally it used to try and precache nodes: 6d20556#diff-a2fd175216a5aa6425363e6773426f3c

But at some point that was removed but I can't find git blame is not helping :/

Then

Copy link
Contributor

Choose a reason for hiding this comment

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

When does line 50 trigger?

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry, I mean like what code path causes closest to be assigned to the instance? And what codepath even lets this function get past return inst on line 47?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think in practical usage it doesn't get past 47, but I guess theoretically it could, if the fiber was a different type?

My guess tho is that this was for both stack and fiber and never got removed. Though even if it could get past 47 I don't think the loop does anything other than run one iteration, and return inst

Copy link
Contributor

Choose a reason for hiding this comment

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

Cool. I'd love to drop this if the code path no longer fires after Stack's removal. I'm afraid to ping anyone to bug them on holiday, but I think it's worth investigating.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

no rush clearly. :)

Copy link
Contributor

Choose a reason for hiding this comment

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

It was hard to find due to a couple project reorganizations nuking the file history, but this was removed in #10798. I think the most likely explanation is that the whole loop could have been removed as well, but it wasn't clear that it was stack-only.

It's a little confusing, but at first glance it seems safe to remove 😄


return closest;
Copy link
Contributor

Choose a reason for hiding this comment

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

Does it make sense to return null for invalid fiber types? Maybe it should throw if it's not a HostComponent or HostText? I'm not sure if there are any situations where this should be called with any other type of node.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

that's a good question. I think your probably right, however i was trying to keep with the spirit (for lack of a better term) of the method by failing softly.

I do think tho that a situation where its not a component or text is kind of a different sort of error and maybe should fail hard. Are roots or portals cached on elements? Presumable you should hit a component before that tho

@gaearon gaearon merged commit 1c7c38c into master Jan 5, 2018
@gaearon
Copy link
Collaborator

gaearon commented Jan 5, 2018

This seems safe to me, thanks.

@gaearon gaearon deleted the remove-loop branch January 5, 2018 18:35
@gaearon
Copy link
Collaborator

gaearon commented Jan 5, 2018

(We should probably follow this up with an invariant)

yenshih pushed a commit to yenshih/react that referenced this pull request Jan 6, 2018
* Remove extra loop (?)

* prettier
ManasJayanth pushed a commit to ManasJayanth/react that referenced this pull request Jan 12, 2018
* Remove extra loop (?)

* prettier
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants