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

add field predicate types to selectionTest in vega-selections #3675

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

jonathanzong
Copy link
Member

@jonathanzong jonathanzong commented Mar 3, 2023

This change introduces the ability for vlSelectionTest to check for more types of field predicates.

This additive change will have no immediate effect, since Vega-Lite doesn't currently produce selection tuples that use these predicate types. However, future proposed changes for Animated Vega-Lite will require us to support these field predicates in addition to the existing point and interval predicates.

cc @arvind

@jonathanzong
Copy link
Member Author

i believe this supersedes #1839 which is also very stale anyway

@@ -41,6 +46,16 @@ function testPoint(datum, entry) {
if (!inrange(dval, values[i], false, false)) return false;
} else if (f.type === TYPE_RANGE_LE) {
if (!inrange(dval, values[i], false, true)) return false;
} else if (f.type === TYPE_PRED_LT) {
Copy link
Member

Choose a reason for hiding this comment

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

Should this become a switch now?

Copy link
Member Author

Choose a reason for hiding this comment

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

i personally don't find switch to be clearer in this instance because all of the returns are conditional, so we'd have to add break statements to every one and remember to keep adding them if we change it in the future. IMO, not shorter, possibly less maintainable, not a significant change in readability. however, happy to make the change if you feel otherwise

Copy link
Member

Choose a reason for hiding this comment

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

Makes sense. I don't have a strong opinion. You could pull the logic here out into a function and then use a return in each branch.

Copy link
Member Author

Choose a reason for hiding this comment

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

the if statements only return false because it's an early exit from the loop (none of the comparisons can be false for the test to return true), so we can't use an unconditional return inside the switch without changing the loop too

presumably it is written this way for efficiency(tm) over readability? but it did take me trying to rewrite it to realize this so maybe it should just be made more readable?

what do you think of e.g.

fields.every(f => {
  switch (f.type) {
    case TYPE_ENUM:
      return ...;
    case ...
  }
})

Copy link
Member

@arvind arvind Mar 8, 2023

Choose a reason for hiding this comment

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

Your intuition is correct @jonathanzong! Because this code path is typically called against every visualized data point on every interaction (eg potentially every mouse drag), we need it to be really performant. When this was first written, most browsers were still faster at evaluating an old school for loop. It was also one of the reasons we minimized functions calls (because they can—or at least could—incur performance overhead).

So, while I’m sympathetic to readability concerns, if we really wanted to make a change here (including extracting this to another function call), perhaps we could run a micro benchmark to validate whether it would cause a performance hit?

(My guess is browsers have advanced significantly since these code paths were first written, so I wouldn’t be surprised if some of these reasons no longer hold. On the other hand, back in the day, I do remember being surprised by what syntaxes did and didn’t incur performance penalties!)

@jonathanzong
Copy link
Member Author

bump @arvind

Copy link
Member

@domoritz domoritz left a comment

Choose a reason for hiding this comment

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

Looks good to me but I will defer to @arvind for the final review.

@domoritz
Copy link
Member

@arvind can you wrap up the review and merge if it looks good?

@kanitw kanitw requested a review from a team as a code owner May 20, 2024 23:33
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

3 participants