perf: O(1) dedup/LMC cache, g_slist_append → prepend+reverse

- Add archive_ids hash set and stanza_senders hash map to
  ff_contact_state_t, populated during index build/extend via
  lightweight _ff_cache_line_ids() (no full parse overhead).
- Replace O(n) file scans in _ff_has_archive_id() and
  _ff_find_original_sender() with O(1) hash table lookups.
- Replace g_slist_append with g_slist_prepend + g_slist_reverse
  in _ff_apply_lmc, _flatfile_get_previous_chat, and
  ff_verify_integrity to avoid O(n²) list building.
- Add db_sqlite_last_changes() declaration to database.h.
This commit is contained in:
2026-03-12 10:14:51 +03:00
parent 06463b75b4
commit 71f9ab5007
6 changed files with 146 additions and 86 deletions

View File

@@ -213,7 +213,6 @@ log_database_export_to_flatfile(const gchar* const contact_jid)
if (!g_flatfile_account_jid) {
// Flatfile backend not initialized — init it temporarily
// We need g_flatfile_account_jid for path construction
g_free(g_flatfile_account_jid);
g_flatfile_account_jid = g_strdup(myjid->barejid);
}
@@ -289,7 +288,9 @@ log_database_export_to_flatfile(const gchar* const contact_jid)
// Lock the temp file
int fd = fileno(fp);
flock(fd, LOCK_EX);
if (flock(fd, LOCK_EX) != 0) {
log_warning("export: flock(%s) failed: %s", tmp_path, strerror(errno));
}
fprintf(fp, "%s", FLATFILE_HEADER);
@@ -416,7 +417,6 @@ log_database_import_from_flatfile(const gchar* const contact_jid)
}
if (!g_flatfile_account_jid) {
g_free(g_flatfile_account_jid);
g_flatfile_account_jid = g_strdup(myjid->barejid);
}
@@ -522,6 +522,13 @@ log_database_import_from_flatfile(const gchar* const contact_jid)
sqlite_be->add_incoming(msg);
message_free(msg);
// Verify the row was actually inserted
if (db_sqlite_last_changes() < 1) {
log_error("import: insert failed for %s at %s", contact, ts);
import_ok = FALSE;
break;
}
contact_imported++;
if (contact_imported % 500 == 0) {