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

Avoid calling indexOf when checking array element types #18619

Merged
3 commits merged into from
Sep 22, 2017
Merged

Avoid calling indexOf when checking array element types #18619

3 commits merged into from
Sep 22, 2017

Conversation

ghost
Copy link

@ghost ghost commented Sep 20, 2017

Shlemiel gets a job as a type checker, checking the contextual types of array elements. On the first day he takes his array contextual type out to the AST and finishes 300 elements of the array. "That's pretty good!" says his boss, "you're a fast worker!" and pays him a kopeck.

The next day Shlemiel only checks 150 elements. "Well, that's not nearly as good as yesterday, but you're still a fast worker. 150 elements is respectable," and pays him a kopeck.

The next day Shlemiel checks 30 elements of the array. "Only 30!" shouts his boss. "That's unacceptable! On the first day you did ten times that much work! What's going on?"

"I can't help it," says Shlemiel. "Every day I get farther and farther away from the beginning of the array!"


indexOf was causing us to take ever-increasing time when for every array element, we would start counting again from the beginning of the array to find our position. (This only happens if the array has a contextual type.) This is an instance of a general problem where we climb ancestors to recompute information instead of passing it down directly.

I ran the performance tests and saw no negative effect on checker time, probably because our tests don't use lots of long arrays.

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Sep 21, 2017

Love the reference!

Do we believe this fixes #17051?

case SyntaxKind.ArrayLiteralExpression: {
const arrayLiteral = <ArrayLiteralExpression>parent;
const type = getApparentTypeOfContextualType(arrayLiteral);
return getContextualTypeForElementExpression(type, indexOf(arrayLiteral.elements, node));
Copy link
Member

@DanielRosenwasser DanielRosenwasser Sep 21, 2017

Choose a reason for hiding this comment

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

If you want to ensure you're always searching pretty efficiently, you can do a binary search using the pos of the node.

Copy link
Member

@DanielRosenwasser DanielRosenwasser Sep 21, 2017

Choose a reason for hiding this comment

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

Sent #18635 as a PR to this branch.

@ghost ghost merged commit 7e002ae into master Sep 22, 2017
@ghost ghost deleted the indexof branch September 22, 2017 15:50
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
This pull request was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants