diff options
| author | Federico Caselli <cfederico87@gmail.com> | 2023-04-19 18:39:18 -0400 |
|---|---|---|
| committer | mike bayer <mike_mp@zzzcomputing.com> | 2023-04-26 19:48:00 +0000 |
| commit | 105f18be353965b064750726597b63334fc0716b (patch) | |
| tree | 29ee8a339ea86dbb37fc07b2e654d11c5922a419 /lib/sqlalchemy/engine/result.py | |
| parent | ff198e35f0e04b8d38df25df234e72259069b4d1 (diff) | |
| download | sqlalchemy-105f18be353965b064750726597b63334fc0716b.tar.gz | |
Performance improvement in Row
Various performance improvements to Row instanciation
- avoid passing processors if they are all None
- improve processor logic in cython
- improve tuplegetter using slices when contiguous indexes are used
Some timing follow.
In particular [base_]row_new_proc that tests using processors has
a 25% improvement compared to before in cython.
Looking at the [b]row_new_proc_none that test a list of processors
all None, this has 50% improvement in cython when passing the none list,
but in this patch it would usually be disabled by passing None, so the
performance gain is actually 90%, since it would run the case
[base_]row_new.
Tuplegetter is a bit faster in the single item get and when getting
sequential indexes (like indexes 1,2,3,4) at the cost of a bit
longer creation time in python, cython is mostly the same.
Current times
| python | cython | cy / py |
base_row_new | 0.639817400 | 0.118265500 | 0.184842582 |
row_new | 0.680355100 | 0.129714600 | 0.190657202 |
base_row_new_proc | 3.076538900 | 1.488428600 | 0.483799701 |
row_new_proc | 3.119700100 | 1.532197500 | 0.491136151 |
brow_new_proc_none | 1.917702300 | 0.475511500 | 0.247958977 |
row_new_proc_none | 1.956253300 | 0.497803100 | 0.254467609 |
tuplegetter_one | 0.152512600 | 0.148523900 | 0.973846751 |
tuplegetter_many | 0.184394100 | 0.184511500 | 1.000636680 |
tuplegetter_seq | 0.154832800 | 0.156270100 | 1.009282917 |
tuplegetter_new_one | 0.523730000 | 0.343402200 | 0.655685563 |
tuplegetter_new_many| 0.738924400 | 0.420961400 | 0.569694816 |
tuplegetter_new_seq | 1.062036900 | 0.495462000 | 0.466520514 |
Parent commit times
| python | cython | cy / py |
base_row_new | 0.643890800 | 0.113548300 | 0.176347138 |
row_new | 0.674885900 | 0.124391800 | 0.184315304 |
base_row_new_proc | 3.072020400 | 2.017367000 | 0.656690626 |
row_new_proc | 3.109943400 | 2.048359400 | 0.658648450 |
brow_new_proc_none | 1.967133700 | 1.006326000 | 0.511569702 |
row_new_proc_none | 1.960814900 | 1.025217800 | 0.522852922 |
tuplegetter_one | 0.197359900 | 0.205999000 | 1.043773330 |
tuplegetter_many | 0.196575900 | 0.194888500 | 0.991416038 |
tuplegetter_seq | 0.192723900 | 0.205635000 | 1.066992729 |
tuplegetter_new_one | 0.534644500 | 0.414311700 | 0.774929322 |
tuplegetter_new_many| 0.479376500 | 0.417448100 | 0.870814694 |
tuplegetter_new_seq | 0.481580200 | 0.412697900 | 0.856966088 |
Change-Id: I2ca1f49dca2beff625c283f1363c29c8ccc0c3f7
Diffstat (limited to 'lib/sqlalchemy/engine/result.py')
| -rw-r--r-- | lib/sqlalchemy/engine/result.py | 29 |
1 files changed, 14 insertions, 15 deletions
diff --git a/lib/sqlalchemy/engine/result.py b/lib/sqlalchemy/engine/result.py index cc6d26c88..cf34c195a 100644 --- a/lib/sqlalchemy/engine/result.py +++ b/lib/sqlalchemy/engine/result.py @@ -41,6 +41,7 @@ from ..sql.base import _generative from ..sql.base import HasMemoized from ..sql.base import InPlaceGenerative from ..util import HasMemoized_ro_memoized_attribute +from ..util import NONE_SET from ..util._has_cy import HAS_CYEXTENSION from ..util.typing import Literal from ..util.typing import Self @@ -84,7 +85,7 @@ across all the result types _InterimSupportsScalarsRowType = Union[Row, Any] _ProcessorsType = Sequence[Optional["_ResultProcessorType[Any]"]] -_TupleGetterType = Callable[[Sequence[Any]], Tuple[Any, ...]] +_TupleGetterType = Callable[[Sequence[Any]], Sequence[Any]] _UniqueFilterType = Callable[[Any], Any] _UniqueFilterStateType = Tuple[Set[Any], Optional[_UniqueFilterType]] @@ -205,6 +206,13 @@ class ResultMetaData: else: self._key_fallback(key, None) + @property + def _effective_processors(self) -> Optional[_ProcessorsType]: + if not self._processors or NONE_SET.issuperset(self._processors): + return None + else: + return self._processors + class RMKeyView(typing.KeysView[Any]): __slots__ = ("_parent", "_keys") @@ -390,7 +398,7 @@ def result_tuple( ) -> Callable[[Iterable[Any]], Row[Any]]: parent = SimpleResultMetaData(fields, extra) return functools.partial( - Row, parent, parent._processors, parent._key_to_index + Row, parent, parent._effective_processors, parent._key_to_index ) @@ -454,7 +462,7 @@ class ResultInternal(InPlaceGenerative, Generic[_R]): def process_row( # type: ignore metadata: ResultMetaData, - processors: _ProcessorsType, + processors: Optional[_ProcessorsType], key_to_index: Mapping[_KeyType, int], scalar_obj: Any, ) -> Row[Any]: @@ -468,7 +476,7 @@ class ResultInternal(InPlaceGenerative, Generic[_R]): metadata = self._metadata key_to_index = metadata._key_to_index - processors = metadata._processors + processors = metadata._effective_processors tf = metadata._tuplefilter if tf and not real_result._source_supports_scalars: @@ -489,21 +497,12 @@ class ResultInternal(InPlaceGenerative, Generic[_R]): process_row, metadata, processors, key_to_index ) - fns: Tuple[Any, ...] = () - if real_result._row_logging_fn: - fns = (real_result._row_logging_fn,) - else: - fns = () - - if fns: + _log_row = real_result._row_logging_fn _make_row = make_row def make_row(row: _InterimRowType[Row[Any]]) -> _R: - interim_row = _make_row(row) - for fn in fns: - interim_row = fn(interim_row) - return interim_row # type: ignore + return _log_row(_make_row(row)) # type: ignore return make_row |
