Skip to content

Commit

Permalink
More efficient utils.chunk (#1613)
Browse files Browse the repository at this point in the history
* optimize bulk operations

- remove converting objects iterable to list in bulk_create
- change chunk method to return iterables lazy

* add changelog

* revert BulkCreateQuery

* add name to contributors
  • Loading branch information
Abdeldjalil-H committed May 14, 2024
1 parent b71eea3 commit 96b999e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ Fixed
- Fix `optional` parameter in `pydantic_model_creator` does not work for pydantic v2. (#1551)
- Fix `get_annotations` now evaluates annotations in the default scope instead of the app namespace. (#1552)

Changed
^^^^^^^
- Change `utils.chunk` from function to return iterables lazily.

0.20.1
------
Added
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Contributors
* Yuval Ben-Arie ``@yuvalbenarie``
* Stephan Klein ``@privatwolke``
* ``@WizzyGeek``
* Abdeldjalil Hezouat ``@Abdeldjalil-H``

Special Thanks
==============
Expand Down
18 changes: 14 additions & 4 deletions tortoise/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
from typing import TYPE_CHECKING, Any, Iterable, Optional
import sys
from typing import TYPE_CHECKING, Any, Iterable, Optional, Tuple

from tortoise.log import logger

if sys.version_info >= (3, 12):
from itertools import batched
else:
from itertools import islice

def batched(iterable: Iterable[Any], n: int) -> Iterable[Tuple[Any]]:
it = iter(iterable)
while batch := tuple(islice(it, n)):
yield batch


if TYPE_CHECKING: # pragma: nocoverage
from tortoise.backends.base.client import BaseDBAsyncClient

Expand Down Expand Up @@ -39,6 +51,4 @@ def chunk(instances: Iterable[Any], batch_size: Optional[int] = None) -> Iterabl
if not batch_size:
yield instances
else:
instances = list(instances)
for i in range(0, len(instances), batch_size):
yield instances[i : i + batch_size] # noqa:E203
yield from batched(instances, batch_size)

0 comments on commit 96b999e

Please sign in to comment.