Nanomath very slow #43

Closed
opened 2025-12-15 16:29:38 +00:00 by glen · 3 comments
Owner

The recent benchmarking PR shows that nanomath performance is far worse than both pocomath and mathjs 15. After a bit of poking around, the leading suspect is ArrayKeyedMap, which is used in nanomath centrally in the TypeDispatcher, but not in pocomath or mathjs. Let's see what happens if we switch to some other indexing scheme.

The recent benchmarking PR shows that nanomath performance is far worse than both pocomath and mathjs 15. After a bit of poking around, the leading suspect is ArrayKeyedMap, which is used in nanomath centrally in the TypeDispatcher, but not in pocomath or mathjs. Let's see what happens if we switch to some other indexing scheme.
Author
Owner

OK, well, getting rid of the ArrayKeyedMap did not help. It seems to be the typing a value is now very slow; benchmarking a bunch of typings in nanomath is 25.6 milliseconds vs. in pocomath it is 0.01 milliseconds. Perhaps the issue is the fact that Types are proxied objects for the sake of the function-call syntax?

OK, well, getting rid of the ArrayKeyedMap did not help. It seems to be the typing a value is now very slow; benchmarking a bunch of typings in nanomath is 25.6 milliseconds vs. in pocomath it is 0.01 milliseconds. Perhaps the issue is the fact that Types are proxied objects for the sake of the function-call syntax?
Author
Owner

Hmm, removing the proxy and the derivation from built-in Function improved things, but only by a factor of two: the typing benchmark still takes 12 milliseconds. Trying to figure out what else it could be.

Hmm, removing the proxy and the derivation from built-in Function improved things, but only by a factor of two: the typing benchmark still takes 12 milliseconds. Trying to figure out what else it could be.
Author
Owner

Just whittled away on typing until it got within a factor of two of pocomath, then switched to cbrt and polynomialRoot, where there were two problems:

  • Some advance resolutions in the factories were in fact not resolving anything (i.e., returning the function that resolves at call time), so I made that a warning and I fixed all instances of it.
  • The type converter factories were not pre-resolving the functions they used, so they were running resolution on every conversion, which is dreadfully slow.

With these things fixed up, brought nanomath to somewhere between one-third the speed and just slightly slower than pocomath, depending on the particular benchmark and the relative weight in it of resolution (which nanomath is still slow at) and computation (where nanomath is essentially just as fast).

Just whittled away on typing until it got within a factor of two of pocomath, then switched to cbrt and polynomialRoot, where there were two problems: * Some advance resolutions in the factories were in fact not resolving anything (i.e., returning the function that resolves at call time), so I made that a warning and I fixed all instances of it. * The type converter factories were not pre-resolving the functions they used, so they were running resolution on every conversion, which is dreadfully slow. With these things fixed up, brought nanomath to somewhere between one-third the speed and just slightly slower than pocomath, depending on the particular benchmark and the relative weight in it of resolution (which nanomath is still slow at) and computation (where nanomath is essentially just as fast).
glen closed this issue 2025-12-16 07:05:54 +00:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
StudioInfinity/nanomath#43
No description provided.