Compare commits

...
1 Commits
Author SHA1 Message Date
Akizon77 61bd0c8bdd perf(metric): reduce SQLite memory usage and make mmap opt-in
Lower default cache size from 16MB to 4MB and journal size limit from 16MB to 4MB. Change MMapSizeBytes handling so 0 explicitly disables mmap instead of being overwritten with a 32MB default; negative values are normalized to 0. Update tests to match the new default journal size limit.
2026-08-05 21:13:19 +08:00
3 changed files with 12 additions and 9 deletions

No files matched your search

+3 -3
View File
@@ -158,11 +158,11 @@ func DefaultConfig(driver Driver, dsn string) Config {
SQLite: SQLiteOptions{
PerformanceProfile: SQLiteProfileBalanced,
BusyTimeout: 5 * time.Second,
CacheSizeKB: 16 * 1024,
CacheSizeKB: 4 * 1024,
TempStoreMemory: false,
MMapSizeBytes: 32 * 1024 * 1024,
MMapSizeBytes: 0,
WALAutoCheckpoint: 4000,
JournalSizeLimitBytes: 16 * 1024 * 1024,
JournalSizeLimitBytes: 4 * 1024 * 1024,
},
RollupPolicy: RollupPolicy{
RawRetention: 10 * time.Minute,
+7 -4
View File
@@ -247,16 +247,19 @@ func prepareSQLiteConfig(cfg Config) (Config, error) {
cfg.SQLite.BusyTimeout = 5 * time.Second
}
if cfg.SQLite.CacheSizeKB == 0 {
cfg.SQLite.CacheSizeKB = 16 * 1024
cfg.SQLite.CacheSizeKB = 4 * 1024
}
if cfg.SQLite.MMapSizeBytes == 0 {
cfg.SQLite.MMapSizeBytes = 32 * 1024 * 1024
// MMapSizeBytes == 0 表示显式禁用 mmap,而不是"未设置":DefaultConfig 始终
// 填充 SQLiteOptions,因此这里的 0 是"跳过 mmap"的明确选择,不能被覆盖为
// 32MB。负数视为非法输入,归一为 0(禁用)。
if cfg.SQLite.MMapSizeBytes < 0 {
cfg.SQLite.MMapSizeBytes = 0
}
if cfg.SQLite.WALAutoCheckpoint == 0 {
cfg.SQLite.WALAutoCheckpoint = 4000
}
if cfg.SQLite.JournalSizeLimitBytes == 0 {
cfg.SQLite.JournalSizeLimitBytes = 16 * 1024 * 1024
cfg.SQLite.JournalSizeLimitBytes = 4 * 1024 * 1024
}
if cfg.DB == nil {
+2 -2
View File
@@ -213,8 +213,8 @@ func TestSQLiteInDirCreatesDirectoryAndAppliesPragmas(t *testing.T) {
if err := store.db.QueryRowContext(ctx, "PRAGMA journal_size_limit").Scan(&journalSizeLimit); err != nil {
t.Fatalf("query journal size limit: %v", err)
}
if journalSizeLimit != 16*1024*1024 {
t.Fatalf("journal size limit = %d, want %d", journalSizeLimit, 16*1024*1024)
if journalSizeLimit != 4*1024*1024 {
t.Fatalf("journal size limit = %d, want %d", journalSizeLimit, 4*1024*1024)
}
}