Speed up detect_fts
by 61%
#2491
Open
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_fts
indatasette/utils/__init__.py
⏱️ Runtime :
5.56 milliseconds
→3.45 milliseconds
(best of56
runs)📝 Explanation and details
Here's an optimized version of your code. The improvements are.
detect_fts_sql
directly intodetect_fts
to avoid the function call overhead..fetchall()
and then checkinglen(rows)
pattern with.fetchone()
for early exit.Summary of changes:
detect_fts
and use parameterized queries (?
) for safety and speed.fetchone()
instead offetchall()
andlen(rows) == 0
, to reduce memory usage and speed up short-circuiting.limit 1
to 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_fts
test_utils.py::test_detect_fts_different_table_names