PR: SQLite Query Caching Optimizations#1241
Open
archpulse wants to merge 1 commit into
Open
Conversation
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.
PR: SQLite Query Caching Optimizations
Description
This pull request optimizes database query performance in CodeGraph by caching static prepared statements inside the
QueryBuilderclass.By caching static SQL statements inside
this.stmtsinstead of compiling them dynamically on every database method invocation, we reduce CPU consumption and query preparation overhead.Key Changes
src/db/queries.tsto add type definitions for new cached statements.QueryBuildermethods to prepare the statement on the first call and reuse it afterwards:isNameSegmentVocabEmptygetDistinctNodeNamesgetNamesForSegmentgetAllNodesgetDistinctFileLanguagesgetDependentFilePathsgetDependencyFilePathsgetCrossFileIncomingEdgesWithTargetgetLastIndexedAtgetNodeAndEdgeCountgetStats(and internal helpers)getMetadata,setMetadata,getAllMetadataPerformance Benchmarks
Timing comparison before and after the optimization for 5,000 iterations:
getDependentFilePaths: 290.55 ms ➔ 177.69 ms (~1.63x speedup)getStats: 60.43 ms ➔ 47.23 ms (~1.28x speedup)getMetadata: 46.15 ms ➔ 21.00 ms (~2.20x speedup)getNamesForSegment: 745.96 ms ➔ 696.91 ms (~1.07x speedup)Detailed Changes (Diff Summary)
1.
src/db/queries.tsPrepared Statements TypingsAdded the following properties to the
stmtsobject type signature insideQueryBuilder:2. Method Optimizations in
QueryBuilderUpdated methods to use lazy compilation and caching of their static SQL query prepared statements. Below is an example pattern implemented in
getMetadataandsetMetadata:All other listed query methods were modified to adhere to this pattern.
Verification
All 20 database correctness/performance tests pass successfully:
Test Environment
Note: Benchmarks were run on a battery-powered system under a non-performance power profile, so results represent a conservative measurement.