Skip to content

Conversation

misrasaurabh1
Copy link

@misrasaurabh1 misrasaurabh1 commented Jun 20, 2025

Saurabh's comments - This was auto generated by codeflash, but I reviewed and cleaned it up before opening here. Let me know what feedback you have for me. I have a lot of other optimizations as well for my fork, and i can open the best ones next.

📄 61% (0.61x) speedup for detect_fts in datasette/utils/__init__.py

⏱️ Runtime : 5.56 milliseconds 3.45 milliseconds (best of 56 runs)

📝 Explanation and details

Here's an optimized version of your code. The improvements are.

  • Combine detect_fts_sql directly into detect_fts to avoid the function call overhead.
  • Replace the .fetchall() and then checking len(rows) pattern with .fetchone() for early exit.
  • Use parameterized SQL to avoid manual escaping and improve security.
  • Optimize SQL for minimal pattern matching when possible.
  • Use string concatenation only if it is cheaper/more readable than format/replace in hot paths.

Summary of changes:

  • Inline the SQL into detect_fts and use parameterized queries (?) for safety and speed.
  • Use fetchone() instead of fetchall() and len(rows) == 0, to reduce memory usage and speed up short-circuiting.
  • Add limit 1 to the query to speed up the lookup when there are many matches (should be rare, but helps).
  • Use || string concatenation and query params to avoid manual quoting. This is safe and efficient with SQLite.

The function signature and return values are unchanged, and all comments are preserved unless the code has been modified.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 44 Passed
🌀 Generated Regression Tests 🔘 None Found
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage 100.0%
⚙️ Existing Unit Tests and Runtime
Test File::Test Function Original ⏱️ Optimized ⏱️ Speedup
test_utils.py::test_detect_fts 119μs 70.3μs ✅69.3%
test_utils.py::test_detect_fts_different_table_names 39.9μs 42.8μs ⚠️-6.80%

Codeflash

codeflash-ai bot and others added 3 commits June 20, 2025 01:03
Here's an optimized version of your code. The improvements are.

- Combine `detect_fts_sql` directly into `detect_fts` to avoid the function call overhead.
- Replace the `.fetchall()` and then checking `len(rows)` pattern with `.fetchone()` for early exit.
- Use parameterized SQL to avoid manual escaping and improve security.
- Optimize SQL for minimal pattern matching when possible.
- Use string concatenation only if it is cheaper/more readable than format/replace in hot paths.




**Summary of changes:**
- Inline the SQL into `detect_fts` and use parameterized queries (`?`) for safety and speed.
- Use `fetchone()` instead of `fetchall()` and `len(rows) == 0`, to reduce memory usage and speed up short-circuiting.
- Add `limit 1` to the query to speed up the lookup when there are many matches (should be rare, but helps).  
- Use `||` string concatenation and query params to avoid manual quoting. This is safe and efficient with SQLite.

**The function signature and return values are unchanged, and all comments are preserved unless the code has been modified.**
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