Skip to content

perf: refactor sqlite database connection lifecycle - #532

Open
winshaurya wants to merge 1 commit into
amplitude:mainfrom
winshaurya:perf/sqlite-connection-refactor
Open

perf: refactor sqlite database connection lifecycle#532
winshaurya wants to merge 1 commit into
amplitude:mainfrom
winshaurya:perf/sqlite-connection-refactor

Conversation

@winshaurya

Copy link
Copy Markdown

Summary

Description

This PR addresses severe disk I/O thrashing and potential file descriptor leaks in AMPDatabaseHelper.m.

@sojingle

Previously, sqlite3_open_v2 and sqlite3_close were being called on every single event insertion/query executed within inDatabase:. In high-volume event tracking scenarios, this constant opening and closing of the SQLite database file causes noticeable CPU/disk overhead and battery drain. Additionally, sqlite3_close() was failing silently when unfinalized statements were present.

Changes Made:

  • Refactored AMPDatabaseHelper to maintain a single, persistent sqlite3 *_database handle scoped to the serial queue lifecycle.
  • Database is now lazily opened once when first queried.
  • Correctly implements sqlite3_close(_database) in dealloc and deleteDB.

This change yields a significant performance optimization for high-throughput tracking.

Checklist

  • Does your PR title have the correct title format?
  • Does your PR have a breaking change?:

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