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

Replace equality definition on ObserverExpression #1517

Merged
merged 3 commits into from
Sep 10, 2021

Conversation

mdickinson
Copy link
Member

@mdickinson mdickinson commented Sep 10, 2021

The current ObserverExpression implements equality via _as_graphs, I suspect purely as a convenience for testing purposes. This PR replaces that equality check with a simple structural equality. It also:

  • makes all expressions hashable, to make caching easier
  • adds convenience __repr__ implementations
  • renames the observer attribute to _observer, for consistency with the other expression classes, to emphasize that it's not used outside this module, and to discourage users from modifying the value
  • fixes one (recently-introduced) test

@mdickinson
Copy link
Member Author

Actually, I think we can do a little better on the __repr__. Let me remove it from this PR, and re-introduce in a separate one.

Copy link
Contributor

@corranwebster corranwebster 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.

Some small concerns about performance if the equality test becomes critical path since it is effectively recursive, but if it becomes a problem we can fix, and it's probably no worse than testing graph equality.

@mdickinson
Copy link
Member Author

mdickinson commented Sep 10, 2021

Since this is partly about performance, I should probably include some code timings. Here's a (successful) equality test on a reasonably complicated but still somewhat realistic expression:

On main:

(traits) mdickinson@mirzakhani traits % ipython
Python 3.9.7 (default, Sep  1 2021, 04:00:27) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.27.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: from traits.observation.api import *

In [2]: expr1 = parse("a,[b,c].items.updated,d:e,f")

In [3]: expr2 = parse("a,[b,c].items.updated,d:e,f")

In [4]: expr1 == expr2
Out[4]: True

In [5]: %timeit expr1 == expr2
100 µs ± 2.7 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

and on this branch, with the same exact definitions of expr1 and expr2:

In [7]: %timeit expr1 == expr2
8.54 µs ± 171 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

Failed equality tests should be much faster. Again, on main:

In [6]: expr1 = parse("a,[b,c].items.updated,d:e,f")

In [7]: expr2 = parse("[a,b,c].items.updated,d:e,f")

In [8]: expr1 == expr2
Out[8]: False

In [9]: %timeit expr1 == expr2
62.7 µs ± 1.29 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

and the same test on this branch:

(traits) mdickinson@mirzakhani traits % ipython
Python 3.9.7 (default, Sep  1 2021, 04:00:27) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.27.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: from traits.observation.api import parse

In [2]: expr1 = parse("a,[b,c].items.updated,d:e,f")

In [3]: expr2 = parse("[a,b,c].items.updated,d:e,f")

In [4]: %timeit expr1 == expr2
652 ns ± 14 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

@mdickinson mdickinson merged commit e75d199 into main Sep 10, 2021
@mdickinson mdickinson deleted the cleanup/observer-expression-equality-and-hash branch September 10, 2021 10:18
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