Skip to content

Commit 82d2c13

Browse files
mistercrunchsloria
andauthored
fix: improve perf of _maybe_filter_foreign_keys method (#667)
* fix: improve perf of _maybe_filter_foreign_keys method * fix mypy/ruff errors * Update changelog --------- Co-authored-by: Steven Loria <[email protected]>
1 parent f2ae9f4 commit 82d2c13

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

CHANGELOG.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
Changelog
22
---------
33

4+
(unreleased)
5+
++++++++++++
6+
7+
Bug fixes:
8+
9+
* Fix memory usage regression in 1.4.1 (:issue:`665`).
10+
Thanks :user:`mistercrunch` for reporting and sending a PR.
11+
412
1.4.1 (2025-02-10)
513
++++++++++++++++++
614

src/marshmallow_sqlalchemy/schema.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -178,22 +178,16 @@ def _maybe_filter_foreign_keys(
178178
and not issubclass(base, SQLAlchemyAutoSchema)
179179
]
180180

181-
def is_declared_field(field: str) -> bool:
182-
return any(
183-
field
184-
in [
185-
name
186-
for name, _ in _get_fields(
187-
getattr(base, "_declared_fields", base.__dict__)
188-
)
189-
]
190-
for base in non_auto_schema_bases
191-
)
181+
# Pre-compute declared fields only once
182+
declared_fields: set[Any] = set()
183+
for base in non_auto_schema_bases:
184+
base_fields = getattr(base, "_declared_fields", base.__dict__)
185+
declared_fields.update(name for name, _ in _get_fields(base_fields))
192186

193187
return [
194188
(name, field)
195189
for name, field in fields
196-
if name not in foreign_keys or is_declared_field(name)
190+
if name not in foreign_keys or name in declared_fields
197191
]
198192
return fields
199193

0 commit comments

Comments
 (0)