Skip to content

⚡ Bolt: Optimize single-model database fetches - #930

Draft
ToolchainLab wants to merge 2 commits into
mainfrom
bolt-optimize-db-fetches-9060013900320677031
Draft

⚡ Bolt: Optimize single-model database fetches#930
ToolchainLab wants to merge 2 commits into
mainfrom
bolt-optimize-db-fetches-9060013900320677031

Conversation

@ToolchainLab

Copy link
Copy Markdown
Collaborator

💡 What:

  • Refactored db.execute(select(...)).scalars().first() to use db.scalar(select(...)) across src/h4ckath0n/auth/service.py, src/h4ckath0n/auth/passkeys/service.py, and src/h4ckath0n/cli/_common.py.
  • Refactored select(func.count()) in _is_bootstrap_admin to use select(User.id).limit(1).

🎯 Why:

  • Calling db.execute(...).scalars().first() forces SQLAlchemy to allocate and parse intermediate ExecutionResult and ScalarResult objects, which is unnecessary overhead when only fetching a single item (or checking existence). Using db.scalar optimizes this.
  • Using func.count() to check if a table is empty is an anti-pattern that can cause full table scans on large tables. Using limit(1) is a massive performance win for existence checks.

📊 Impact:

  • Reduces memory allocation and parsing overhead on highly frequent operations (like authenticate_user and register_user).
  • Prevents O(N) database operations in bootstrap checks, making it O(1).

🔬 Measurement:

  • uv run pytest confirms that all auth and CLI flows behave identically.

PR created automatically by Jules for task 9060013900320677031 started by @ToolchainLab

- Changed db.execute(select(...)).scalars().first() to db.scalar(select(...))
  across auth and CLI modules to avoid intermediate ExecutionResult allocation.
- Changed _is_bootstrap_admin func.count() check to a select(User.id).limit(1)
  existence check to avoid full table scans on large user tables.

Co-authored-by: ToolchainLab <263750431+ToolchainLab@users.noreply.github.com>
@google-labs-jules

Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

- Changed db.execute(select(...)).scalars().first() to db.scalar(select(...))
  across auth and CLI modules to avoid intermediate ExecutionResult allocation.
- Changed _is_bootstrap_admin func.count() check to a select(User.id).limit(1)
  existence check to avoid full table scans on large user tables.
- Fixed SIM102, E501 and unused import errors detected by Ruff.

Co-authored-by: ToolchainLab <263750431+ToolchainLab@users.noreply.github.com>
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.

1 participant