-
-
Notifications
You must be signed in to change notification settings - Fork 96
optimizations to improve performance, add optional ormar-utils packag… #1571
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
collerek
wants to merge
9
commits into
master
Choose a base branch
from
check-optimisations-and-rs
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a4d97bb
optimizations to improve performance, add optional ormar-utils packag…
collerek 23e4cdf
update lock
collerek ae1db7c
update coverage and lock
collerek 1191a8b
Merge branch 'master' into check-optimisations-and-rs
collerek ccc0e85
bump poetry in workflows
collerek 15dfeeb
Merge remote-tracking branch 'origin/check-optimisations-and-rs' into…
collerek b263315
bump lock
collerek bddb3f0
remove marks on fixtures
collerek 17d9773
remove marks in benchmarks too
collerek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,11 @@ | |
|
|
||
| import ormar | ||
| from ormar.queryset.utils import translate_list_to_dict | ||
| from ormar.utils.rust_utils import HAS_RUST, ormar_rust_utils | ||
|
|
||
| if HAS_RUST: # pragma: no cover | ||
| _rs_group_by_pk = ormar_rust_utils.group_by_pk | ||
| _rs_plan_merge = ormar_rust_utils.plan_merge_items_lists | ||
|
|
||
| if TYPE_CHECKING: # pragma no cover | ||
| from ormar import Model | ||
|
|
@@ -56,14 +61,21 @@ def merge_instances_list(cls, result_rows: list["Model"]) -> list["Model"]: | |
| :rtype: list["Model"] | ||
| """ | ||
| merged_rows: list["Model"] = [] | ||
| grouped_instances: dict = {} | ||
|
|
||
| for model in result_rows: | ||
| grouped_instances.setdefault(model.pk, []).append(model) | ||
|
|
||
| for group in grouped_instances.values(): | ||
| model = cls._recursive_add(group)[0] | ||
| merged_rows.append(model) | ||
| if HAS_RUST and result_rows: # pragma: no cover | ||
| pks = [model.pk for model in result_rows] | ||
| index_groups = _rs_group_by_pk(pks) | ||
| for group_indices in index_groups: | ||
| group = [result_rows[i] for i in group_indices] | ||
| model = cls._recursive_add(group)[0] | ||
| merged_rows.append(model) | ||
| else: # pragma: no cover | ||
| grouped_instances: dict = {} | ||
| for model in result_rows: | ||
| grouped_instances.setdefault(model.pk, []).append(model) | ||
| for group in grouped_instances.values(): | ||
| model = cls._recursive_add(group)[0] | ||
| merged_rows.append(model) | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| return merged_rows | ||
|
|
||
|
|
@@ -149,18 +161,48 @@ def _merge_items_lists( | |
| :return: merged list of models | ||
| :rtype: list[Model] | ||
| """ | ||
| value_to_set = [x for x in other_value] | ||
| for cur_field in current_field: | ||
| if cur_field in other_value: | ||
| old_value = next((x for x in other_value if x == cur_field), None) | ||
| new_val = cls.merge_two_instances( | ||
| cur_field, | ||
| cast("Model", old_value), | ||
| relation_map=cur_field._skip_ellipsis( # type: ignore | ||
| relation_map, field_name, default_return=dict() | ||
| ), | ||
| ) | ||
| value_to_set = [x for x in value_to_set if x != cur_field] + [new_val] | ||
| else: | ||
| value_to_set.append(cur_field) | ||
| return value_to_set | ||
| if HAS_RUST: # pragma: no cover | ||
| current_pks = [getattr(m, "pk", None) for m in current_field] | ||
| other_pks = [getattr(m, "pk", None) for m in other_value] | ||
| plan = _rs_plan_merge(current_pks, other_pks) | ||
| value_to_set = list(other_value) | ||
| for cur_idx, other_idx in plan: | ||
| cur_item = current_field[cur_idx] | ||
| if other_idx is not None: | ||
| old_value = other_value[other_idx] | ||
| new_val = cls.merge_two_instances( | ||
| cur_item, | ||
| cast("Model", old_value), | ||
| relation_map=cur_item._skip_ellipsis( # type: ignore | ||
| relation_map, field_name, default_return=dict() | ||
| ), | ||
| ) | ||
| value_to_set = [ | ||
| x for x in value_to_set if getattr(x, "pk", None) != cur_item.pk | ||
| ] + [new_val] | ||
| else: | ||
| value_to_set.append(cur_item) | ||
| return value_to_set | ||
| else: # pragma: no cover | ||
| other_by_pk: dict = {} | ||
| for idx, item in enumerate(other_value): | ||
| other_by_pk.setdefault(item.pk, idx) | ||
|
|
||
| value_to_set = list(other_value) | ||
| for cur_field in current_field: | ||
| other_idx = other_by_pk.get(cur_field.pk) | ||
| if other_idx is not None: | ||
| old_value = other_value[other_idx] | ||
| new_val = cls.merge_two_instances( | ||
| cur_field, | ||
| cast("Model", old_value), | ||
| relation_map=cur_field._skip_ellipsis( # type: ignore | ||
| relation_map, field_name, default_return=dict() | ||
| ), | ||
| ) | ||
| value_to_set = [x for x in value_to_set if x.pk != cur_field.pk] + [ | ||
| new_val | ||
| ] | ||
| else: | ||
| value_to_set.append(cur_field) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| return value_to_set | ||
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function with high complexity (count = 14): group_related_list [qlty:function-complexity]