Skip to content

Commit 978105b

Browse files
committed
fix: 3% better performance for collections
1 parent c8100a5 commit 978105b

2 files changed

Lines changed: 6 additions & 6 deletions

File tree

sqlalchemy_serializer/lib/schema.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ def is_included(self, key: str) -> bool:
131131
def fork(self, key: str) -> "Schema":
132132
return Schema(tree=self._tree[key])
133133

134+
def reset(self):
135+
"""Reset schema to initial empty state for reuse."""
136+
self._tree = Tree()
137+
134138

135139
def merge_trees(old: Tree, *trees):
136140
for tree in trees:

sqlalchemy_serializer/serializer.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -421,12 +421,8 @@ def serialize_collection(iterable: t.Iterable, *args, **kwargs) -> list:
421421
# Serialize each item using the shared Serializer
422422
result = []
423423
for item in iterable:
424-
# Create a fresh schema for this item
425-
serializer.schema = Schema()
426-
427-
# Update schema with provided only/rules first (simulating __call__ behavior)
428-
# This matches the behavior in Serializer.__call__ where schema is updated
429-
# before serialize is called
424+
# Reset and update schema for this item (reuse instead of creating new)
425+
serializer.schema.reset()
430426
serializer.schema.update(only=only, extend=rules)
431427

432428
# Serialize the item - serialize_model will update the schema again

0 commit comments

Comments
 (0)