Optimize QueryBuilder.__copy__ some more #205
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Original: Tests run in 87ms
Optimization 1: Tests run in 73ms (~19% speedup)
Use
__new__
instead of__init__
because we are going to clobber its stateUse
copy()
(shallow copy) which is supported on both sets and lists and faster than a comprehensionOptimization 2: Tests run in 67ms (30% speedup) INVALIDUnroll the loop
Invalid due to subclasses do define extra lists. And the unrolled variant doesn't copy everything it needs.
Optimization 2b: Tests run in 67ms (~30% speedup)
Same as Opt2, but subclasses that define lists (
MySQLQueryBuilder
&PostgreQueryBuilder
only)For Opt 1:
When running the benchmark suite for Tortoise ORM, two of the benchmarks get a 6% and 9% performance increase respectively.
For Opt2b:
When running the benchmark suite for Tortoise ORM, two of the benchmarks get a 12% and 15% performance increase respectively.
Due to the perf increase for Opt2a being noticeably better than Opt1, I opted to go with that
I had to use
copy.copy()
instead of<list/set>.copy()
for Py2.7 compatibility reasons.