Open
Conversation
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.**
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_ftsindatasette/utils/__init__.py⏱️ Runtime :
5.56 milliseconds→3.45 milliseconds(best of56runs)📝 Explanation and details
Here's an optimized version of your code. The improvements are.
detect_fts_sqldirectly intodetect_ftsto avoid the function call overhead..fetchall()and then checkinglen(rows)pattern with.fetchone()for early exit.Summary of changes:
detect_ftsand use parameterized queries (?) for safety and speed.fetchone()instead offetchall()andlen(rows) == 0, to reduce memory usage and speed up short-circuiting.limit 1to the query to speed up the lookup when there are many matches (should be rare, but helps).||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:
⚙️ Existing Unit Tests and Runtime
test_utils.py::test_detect_ftstest_utils.py::test_detect_fts_different_table_names