Significant performance improvements to AddResult / GetParameter
Saving and loading data from the SQL database rely on _adapt_xxx and _convert_xxx methods. However, currently they are using many numpy functions (np.isnan, np.isinf, np.save, np.load) that are slow when dealing with arrays containing single values. This PR makes the following changes that significantly improve data loading and saving performance:
- For
float: Replacenumpyfunctions bymathfunctions, usemath.isinfiniteinstead of combiningisnanandisinf; - For
complex: Do not save complex single values asnumpyarrays of one value. It saves both time and space if we avoid reading and writing the relatively largenumpyheader. This changes the representation of the data in the database, but it's fully backwards compatible (and tested); - For
array: Use directly functions fromnp.lib.format, it skips some work ofnp.saveandnp.load;
b0b6528 rewrites adapters and converters avoiding numpy functions and significantly improves performance (around x3 when loading regular numeric data and up to x70 for complex data).
20c58d7 is a minor improvement (~10% when loading complex or numeric data), it avoids unpacking sqlite3.Row to list when it's unnecessary (those functions can reorder the columns however, it's most of the time unnecessary because the SQL queries are properly sorted).
3d7b0fd follows the previous one: as the SQL queries are properly sorted, the named tuple feature of sqlite3.Row (row[column_name]) becomes superfluous.
459b342 is up-to-date benchmarks. (Note that the benchmarks are not run in CI.)
Commit b0b6528 is the most important one, the two other modifications give pretty minor improvements compared to the number of changes to the codebase. If you feel that it's not worth changing so much for such a small gain, they can be left out.
(we worked on these preformance improvements together with @mgunyho)
closes #1238 (?)
Codecov Report
Merging #4446 (3362c91) into master (34f942b) will decrease coverage by
0.02%. The diff coverage is93.93%.
:exclamation: Current head 3362c91 differs from pull request most recent head b476b79. Consider uploading reports for the commit b476b79 to get more accurate results
@@ Coverage Diff @@
## master #4446 +/- ##
==========================================
- Coverage 68.23% 68.21% -0.03%
==========================================
Files 339 339
Lines 31801 31778 -23
==========================================
- Hits 21700 21676 -24
- Misses 10101 10102 +1
Thanks @fblanchetNaN and @mgunyho I will need a bit of time to look at this so please bear with me
@fblanchetNaN and @mgunyho thanks for the contribution. @astafan8 And I finally had a chance to look at it today. We agreed that this looks good and we would like to merge most of it. We are not completely confident in the logic for scalar complex numbers so we would prefer if we can merge the other optimizations but leave that one for a subsequent pr.
Thanks for the response!
We agree that the complex scalar logic looks a bit convoluted, even though it is just following the numpy format standard. We can take it out from this PR and discuss it further separately, although for us this is actually the more significant change because it really affects the loading times in some of our experiments.
I can remove the changes related to complex stuff and force-push if that sounds good. I guess we anyway need to rebase to fix the conflicts.
The branch is now updated excluding the changes related to complex numbers
We just realized that some stuff from the complex adapter/converter was left over (like pointed out here, and also here). Is it ok to rebase and force-push? Or should I add new commits to preserve your review? Sorry about the mess.
Is it ok to rebase and force-push? Or should I add new commits to preserve your review? Sorry about the mess.
no worries :) both are fine, feel free to do what's most convenient for you, the comments will be preserved anyways.
The branch is now updated including the required changes, all checks have passed.
I have marked the conversations as resolved after replying/accepting them.
Build succeeded:
- builddocs (ubuntu-latest, 3.10)
- builddocs (ubuntu-latest, 3.9)
- builddocs (windows-latest, 3.7)
- pytestmin (ubuntu-latest, 3.7)
- pytestmypy (ubuntu-latest, 3.10.6)
- pytestmypy (ubuntu-latest, 3.7)
- pytestmypy (ubuntu-latest, 3.8)
- pytestmypy (ubuntu-latest, 3.9)
- pytestmypy (windows-latest, 3.10.6)
- pytestnonlocal (3.7)