Another idea worth pursuing might be to keep a set of symbols contained in the rhs of each assigned symbol together with the assignemnt itself. When assigning a new symbol, and before doing the substitution in all existing assignments, ginsh could check if it makes sense to do the substitution prior to actually calling .subs(). This won't cure the O(N) behavior but it will make the slope much more flat. This boils down to keeping track of unassigned (free) symbols in ginsh in addition to the assigned signals which we already track, and to also
Hi Vitaly, Walking this lane a little bit more (assuming the current behavior should be kept). On 3/31/26 10:51 PM, Richard B. Kreckel wrote: track in which assigned symbol every free symbol occurs in. An assigned symbol can always be freed by unassign('x'). And a free symbol can be assigned by e.g. y=42*x and at this moment we subs(y==42*x) in all assigned symbols where y occurs in and tell the free symbol x that it now occurs in all assigned symbols where previously y was in. Effectively, all free symbols in y (x in our example) become free symbols in the assigned symbols where we just substituted. That is a little bit tedious but straightforward and should fix your performance problems, I guess. A slightly more complete implementation would verify, after the substitution, whether the free symbols just introduced in the assigned symbol *really* occurs in the assigned symbols expression, using .has(), constructing a symbols_map, or similar. After all, the symbol might have cancelled in the substitution! Doing this would descend into the expression and be a rather expensive operation. Is that worth it? Not sure. It might be better to lazily omit this extra verification. What would be the cost? At most an possible extra substitution later because ginsh doesn't know that the symbol it is substituting hasn't cancelled from the assigned symbol. (The result expression would be the same.) Bye, -richard. PS: Just in case, I added a little test suite for ginsh. -- Richard B. Kreckel <https://in.terlu.de/~kreckel/>