You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
⚡️ Speed up method CodeFlashBenchmarkPlugin.write_benchmark_timings by 146% in PR #59 (codeflash-trace-decorator)
Based on the line profiling data provided, the major bottlenecks are.
1. Establishing a connection to the SQLite database.
2. Executing the SQL commands, particularly committing the transaction.
To speed up the code, consider the following optimizations.
1. Avoid repeatedly establishing a connection if not necessary.
2. Reduce the number of commits by grouping operations.
Here's the optimized code.
### Changes Made.
1. Introduced `close_connection()` to safely close the connection and commit any remaining data when the program exits. This ensures that the connection is not prematurely closed and all data is committed properly.
2. Introduced `_get_connection()` to lazily initialize the connection only if necessary, avoiding repeated opening and closing of the database connection.
3. Used the `atexit` module to ensure the database connection is properly closed when the program exits, which handles any uncommitted data.
4. Moved the commit operation to the `close_connection` function to avoid frequent commits within the `write_benchmark_timings` function.
### Expected Improvements.
- Reduced overhead from frequent opening/closing of the database connection.
- Reduced the costly commit operations to only when the program exits.
- Cleaner code structure by encapsulating connection management logic.
0 commit comments