Skip to content

Fix: Model.__init__ does a per-field import inside its hot per-instance loop - #123

Open
pullapprove5[bot] wants to merge 1 commit into
masterfrom
pullapprove/pa-21-22b59d
Open

Fix: Model.__init__ does a per-field import inside its hot per-instance loop#123
pullapprove5[bot] wants to merge 1 commit into
masterfrom
pullapprove/pa-21-22b59d

Conversation

@pullapprove5

@pullapprove5 pullapprove5 Bot commented Sep 6, 2026

Copy link
Copy Markdown

Reproduced and fixed the finding. plain-postgres/plain/postgres/base.py's Model.init had from plain.postgres.fields.related import RelatedField inside the for field in meta.fields: loop (line 122-123) — executed once per field per instance construction. Crucially, RelatedField is already imported at module level in the same file (line 30) and used by five other call sites in base.py (is_cached checks at lines 321/516, related-field filters at 884/1041/1195) — so the inline import was a pure redundant re-import with zero functional purpose (no circular-import need, nothing lazy/deferred about it). I benchmarked real Model construction (ContactSubmission with 4 fields, and Note with a ForeignKeyField/RelatedField) in the example app before and after removing the inline import, and per-instance time dropped by roughly 15-20% consistently across repeated runs, confirming the overhead was real and eliminated by hoisting nothing — the loop-local import statement was simply removed since the name was already bound at module scope. Fixed by deleting the two redundant lines from the loop body. Ran ./scripts/fix plain-postgres (clean) and the full ./scripts/test plain-postgres suite (795 passed, 1 skipped, unrelated) plus the example app test (1 passed) on the patched tree — no regressions. Note: this sandbox had no uv, Postgres, or psycopg-binary preinstalled; I installed uv via pip, installed PostgreSQL via apt (sudo available), and added psycopg-binary to make the example app runnable for benchmarking/testing — these are sandbox bootstrap steps only, not part of the committed fix.


Opened by a PullApprove implementation run (implement-finding v4) for:

  • PA-21 — Model.init does a per-field import inside its hot per-instance loop

Merging this is what closes them as fixed.

…ce loop

Reproduced and fixed the finding. plain-postgres/plain/postgres/base.py's Model.__init__ had `from plain.postgres.fields.related import RelatedField` inside the `for field in meta.fields:` loop (line 122-123) — executed once per field per instance construction. Crucially, `RelatedField` is already imported at module level in the same file (line 30) and used by five other call sites in base.py (is_cached checks at lines 321/516, related-field filters at 884/1041/1195) — so the inline import was a pure redundant re-import with zero functional purpose (no circular-import need, nothing lazy/deferred about it). I benchmarked real Model construction (ContactSubmission with 4 fields, and Note with a ForeignKeyField/RelatedField) in the example app before and after removing the inline import, and per-instance time dropped by roughly 15-20% consistently across repeated runs, confirming the overhead was real and eliminated by hoisting nothing — the loop-local import statement was simply removed since the name was already bound at module scope. Fixed by deleting the two redundant lines from the loop body. Ran `./scripts/fix plain-postgres` (clean) and the full `./scripts/test plain-postgres` suite (795 passed, 1 skipped, unrelated) plus the example app test (1 passed) on the patched tree — no regressions. Note: this sandbox had no `uv`, Postgres, or psycopg-binary preinstalled; I installed `uv` via pip, installed PostgreSQL via apt (sudo available), and added `psycopg-binary` to make the example app runnable for benchmarking/testing — these are sandbox bootstrap steps only, not part of the committed fix.
@pullapprove5

pullapprove5 Bot commented Sep 9, 2026

Copy link
Copy Markdown
Author
PENDING: 1 review scope pending
Scope Progress Pending
code 0/1 davegaeddert

View in PullApprove

Next steps:

  • pullapprove5[bot]: Waiting for reviews

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants