Replies: 7 comments 19 replies
-
Symbols, definitions and rulesIn WL, each symbol has a definition, that includes different kind of rules (ownvalues, downvalues, upvalues, formats) and values (attributes, options, etc) linked to a "fully qualified name" (FQN) in the form In the current implementation, the definition of a symbol is stored in a
All these dictionaries have the name of a symbol as the key. A definition is then accessed by calling By now, the best I can imagine to improve this was implemented in definition3 (PR #59). In this branch, the call to |
Beta Was this translation helpful? Give feedback.
-
Following the same line, #64 tries to accelerate the access to the attributes of a symbol, again avoiding calling |
Beta Was this translation helpful? Give feedback.
-
Good observations and a good start. Let's start to drill down from a high-level observation into some concrete actions or next steps that will help us address this. Let me start with:
Actually, the first question I have is: is this the simplest example that shows a general problem worth solving? Would we see dramatic discrepancies still if we removed the outer The first step in fixing this is to isolate and pinpoint problems. The simpler and narrower the problem, the better - if Let us assume that we can get dramatic improvements just in the simpler Now let me break down one aspect of this particular expression. In Python or a programming language like that you would write:
And one thing that you notice is that
In Python the improvement might not be so great. But in Mathics, since things like evaluating In compiler optimization technology this kind of improving code transformation is called "constant propagation" and "moving invariant code out of loops". (here the invariant code is an expression) And in sequences (e.g. One would hope that a symbolic manipulation system like SymPy already understands that this is is a problem and looks for invariant code and constant propagation. So a question I have for someone is: Does Sympy understand this issue and handle it in some way? If so, we should try to hook into that. If not, we would suggest that Sympy look into it and possibly write our own routines for constant propagation and moving invariant expressions out of loops. Especially when functions are "compiled". |
Beta Was this translation helpful? Give feedback.
-
If this is simple, then would you please write the corresponding thing in sympy, and add to the table what this speed is? This would narrow the slowness between that may exist between Mathics implementation, and Sympy Python and Mathematica. And/or do likewise for other examples. I thought I would try this, but when I come down to it, this is too complicated and/or we haven't narrowed things enough. Yes, writing examples delineating precise boundaries is a bit effort, possibly hard, and maybe requires a bit more skill. However not doing so means flailing around. I see that there was work in mathics-benchmark to try this, but those examples don't look like they have any kind of sympy interaction at all. Possibly they only show how bad the Mathics Python implementation is for Rule replacement and pattern matching. And the problem here, is that in say compilation it doesn't matter how bad that is if it is done only one time and subsequently we have a representation that doesn't need to go through those steps anymore. (There would be other representations and other steps that would happen instead. |
Beta Was this translation helpful? Give feedback.
-
One little thing that has been nagging me has been the choice of the name If I understand correctly, the class If that's the case then |
Beta Was this translation helpful? Give feedback.
-
@rocky @mmatera I think we need to move to a better importing system. Now it is done through a file search and checking every var from the module to see if that is a builtin. My idea is to use a decorator to do that (otherwise we would need to have a file with hundreds of imports), something like the following: def contribute(cls):
cls.contribute()
@contribute
class N(Builtin):
... |
Beta Was this translation helpful? Give feedback.
-
The attribute stuff is easy to make backward compatible : just requieres an extra 'if ' in the contribute method. |
Beta Was this translation helpful? Give feedback.
-
During the last month, I was doing some exploration about how to improve one of the biggest issues in the project: the slowness of the evaluation process, compared against WMA and other implementations of WL.
Let's consider a "simple" example:
Do[ll=Join@@Table[{a,b,c},{i,n}];Timing[Plus@@ll][[1]]//Print[n,"->",#1]&,{n,{1,10,100,1000,10000}}]
This is, check how much it takes to evaluate the sum
a+b+c+a+b+c+....
with 3n terms.
In WMA, we get (in my Intel i7, 16GB laptop)
in a way that a simple addition takes ~ 300 times more time in Mathics than in WMA.
Or, in a more sophisticated example
'Table[Sin[i+Sqrt[2.]],{i,100.}]//Timing//#1[[1]]&'
WMA: 0.00007
mathics: 0.085582
with a difference of ~1200 times or, for a matrix:
Table[Sin[i-j+Sqrt[2.]],{i,100.},{j,100.}]//Timing//#1[[1]]&
WMA: 0.001593
mathics: 10.1881
the factor is around 6800 times.
From these "simple" examples, it follows that the difference between both implementations is not linear, and hence, we can not just blame to the slowness of Python against C, but to something hidden in the evaluation algorithm.
We can expect that using tools like Cython, or JIT could reduce an overall factor (maybe one order of magnitud) in the evaluation time, but not to correct the (polynomial?) scaling. Then, to solve the slowness of Mathics, we need to review more carefully different aspects of the algorithm:
and probably many other important details, ranging from memory management to specific algorithms for certain frequently used symbols (like arithmetic, logic, etc) and the interface to libraries like sympy, mpmath, or numpy.
Ideally, we would like to pick one microscopic aspect of this process at a time, and try to improve it. However, many of these parts are actually quite entangled, and hence, modify one aspect forces to implement a lot of other changes in order to make it works (just at the level of passing the CI tests).
Here I would like to share the ideas I had, how and where were implemented, and what I have learned doing it.
Beta Was this translation helpful? Give feedback.
All reactions