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

faster access to Context and ContextPath #78

Merged
merged 2 commits into from
Nov 26, 2021
Merged

faster access to Context and ContextPath #78

merged 2 commits into from
Nov 26, 2021

Conversation

mmatera
Copy link
Contributor

@mmatera mmatera commented Nov 25, 2021

This PR is a small part of #64 that helps to access the current context and the context without the need to look at the corresponding definition. These variables are intensively used each time that the context of a symbol needs to be resolved.

@rocky
Copy link
Member

rocky commented Nov 25, 2021

If we could get a benchmark on this, I'd appreciate it.

On my list of things to do is benchmark all of the changes since 4.0.0 to get a sense of what improvements there have been.

@mmatera
Copy link
Contributor Author

mmatera commented Nov 25, 2021

If we could get a benchmark on this, I'd appreciate it.

On my list of things to do is benchmark all of the changes since 4.0.0 to get a sense of what improvements there have been.

Working on it...

@mmatera
Copy link
Contributor Author

mmatera commented Nov 25, 2021

Here are the benchmarks.

From running timeit in jupyter

expr master [us] faster_context [us] Improvement
a 12.7 9.11 28%
E 17.6 13.1 25%
F[] 42.6 38.6 9.4%
2*b 156 150 3.8%
Plus[a,b] 157 146 7%
a+1+a+1... 821 779 5%

Code:

from mathics.session import MathicsSession
from mathics.core.symbols import Symbol
from mathics.core.expression import Expression
from mathics.core.atoms import Integer, Real


session = MathicsSession()
evaluation = session.evaluation

print("evaluate an undefined symbol")
%timeit session.evaluate("a")

print("evaluate a symbol")
%timeit session.evaluate("E")

print("evaluate an expression")
%timeit session.evaluate("F[]")

print("evaluate a simple product")
%timeit session.evaluate("2*b")

print("evaluate a simple addition")
%timeit session.evaluate("Plus[a,b]")

print("evaluate a longer addition")
%timeit session.evaluate("a+1+a+1+a+1+a+1+a+1+a+1+a+1+a+1+a")

And these are some results from mathics-benchmarks:

imagen
imagen
imagen
imagen

@mmatera
Copy link
Contributor Author

mmatera commented Nov 25, 2021

Again, I found funny some of the results in mathics-benchmarks: Integer[1] seems to be 14% slower than in master, but I found it hard to imagine where this overhead is coming taking into account the implemented changes...

@rocky
Copy link
Member

rocky commented Nov 25, 2021

Again, I found funny some of the results in mathics-benchmarks: Integer[1] seems to be 14% slower than in master, but I found it hard to imagine where this overhead is coming taking into account the implemented changes...

Overall, excellent work! I will investigate the slowdowns to see if I can understand what is going on.

@rocky
Copy link
Member

rocky commented Nov 26, 2021

Again, I found funny some of the results in mathics-benchmarks: Integer[1] seems to be 14% slower than in master, but I found it hard to imagine where this overhead is coming taking into account the implemented changes...

I am not seeing this, but instead the "faster_context" branch is faster, but not by much.

Here is what I did. I git cloned the master and faster_context branches in separate virtual environments.

Then I went into ipython in each and rean the setup code listed above

from mathics.session import MathicsSession
from mathics.core.symbols import Symbol
...

Here is what I see. First master:

In [9]: %timeit session.evaluate("Integer[1]")
%timeit session.evaluate("Integer[1]"))
139 µs ± 330 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

In [10]: %timeit session.evaluate("Integer[1]")
%timeit session.evaluate("Integer[1]"))
140 µs ± 431 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

Next faster_context

In [15]: %timeit session.evaluate("Integer[1]")
%timeit session.evaluate("Integer[1]"))
134 µs ± 517 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

In [16]: %timeit session.evaluate("Integer[1]")
135 µs ± 734 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

@rocky
Copy link
Member

rocky commented Nov 26, 2021

I was able to see the speedups reported in lookup up single simples (in a loop of 1000 times, so this is kind of expected).

The greatest mathics-benchmark difference I am not seeing though. Something is weird with mathics benchmark I think.

@rocky
Copy link
Member

rocky commented Nov 26, 2021

In resolving the CHANGES conflicts, I made some of the simpler typographical changes. However deeper changes should be made in a separate PR.

@rocky
Copy link
Member

rocky commented Nov 26, 2021

LGTM

@rocky rocky merged commit c5354aa into master Nov 26, 2021
@TiagoCavalcante TiagoCavalcante deleted the faster_context branch November 26, 2021 18:57
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