ssb: Reduce redundant work in ssb.following.
All checks were successful
Build Tilde Friends / Build-All (push) Successful in 33m15s

This commit is contained in:
Cory McWilliams 2025-05-11 22:20:16 -04:00
parent 35656a5c34
commit 1f2664e5a8

View File

@ -1577,17 +1577,9 @@ static following_t* _make_following_node(const char* id, following_t*** followin
return entry;
}
static void _populate_follows_and_blocks(tf_ssb_t* ssb, following_t* entry, following_t*** following, int* following_count, block_node_t* active_blocks, bool include_blocks)
static void _populate_follows_and_blocks(
sqlite3* db, sqlite3_stmt* statement, following_t* entry, following_t*** following, int* following_count, block_node_t* active_blocks, bool include_blocks)
{
sqlite3* db = tf_ssb_acquire_db_reader(ssb);
sqlite3_stmt* statement = NULL;
if (sqlite3_prepare(db,
"SELECT content ->> '$.contact' AS contact, content ->> '$.following', content ->> '$.blocking' "
"FROM messages "
"WHERE contact IS NOT NULL AND author = ? AND content ->> '$.type' = 'contact' "
"ORDER BY sequence",
-1, &statement, NULL) == SQLITE_OK)
{
if (sqlite3_bind_text(statement, 1, entry->id, -1, NULL) == SQLITE_OK)
{
while (sqlite3_step(statement) == SQLITE_ROW)
@ -1639,13 +1631,11 @@ static void _populate_follows_and_blocks(tf_ssb_t* ssb, following_t* entry, foll
}
}
}
sqlite3_finalize(statement);
}
tf_ssb_release_db_reader(ssb, db);
sqlite3_reset(statement);
}
static void _get_following(
tf_ssb_t* ssb, following_t* entry, following_t*** following, int* following_count, int depth, int max_depth, block_node_t* active_blocks, bool include_blocks)
static void _get_following(sqlite3* db, sqlite3_stmt* statement, following_t* entry, following_t*** following, int* following_count, int depth, int max_depth,
block_node_t* active_blocks, bool include_blocks)
{
int old_depth = entry->depth;
entry->depth = tf_min(depth, entry->depth);
@ -1656,28 +1646,47 @@ static void _get_following(
if (!entry->populated)
{
entry->populated = true;
_populate_follows_and_blocks(ssb, entry, following, following_count, active_blocks, include_blocks);
_populate_follows_and_blocks(db, statement, entry, following, following_count, active_blocks, include_blocks);
}
block_node_t blocks = { .entry = entry, .parent = active_blocks };
for (int i = 0; i < entry->following_count; i++)
{
_get_following(ssb, entry->following[i], following, following_count, depth + 1, max_depth, &blocks, include_blocks);
_get_following(db, statement, entry->following[i], following, following_count, depth + 1, max_depth, &blocks, include_blocks);
}
}
}
}
static sqlite3_stmt* _make_following_statement(sqlite3* db)
{
sqlite3_stmt* statement = NULL;
if (sqlite3_prepare(db,
"SELECT content ->> '$.contact' AS contact, content ->> '$.following', content ->> '$.blocking' "
"FROM messages "
"WHERE contact IS NOT NULL AND author = ? AND content ->> '$.type' = 'contact' "
"ORDER BY sequence",
-1, &statement, NULL) != SQLITE_OK)
{
tf_printf("prepare failed: %s", sqlite3_errmsg(db));
}
return statement;
}
tf_ssb_following_t* tf_ssb_db_following_deep(tf_ssb_t* ssb, const char** ids, int count, int depth, bool include_blocks)
{
following_t** following = NULL;
int following_count = 0;
sqlite3* db = tf_ssb_acquire_db_reader(ssb);
sqlite3_stmt* statement = _make_following_statement(db);
for (int i = 0; i < count; i++)
{
following_t* entry = _make_following_node(ids[i], &following, &following_count, NULL, include_blocks);
_get_following(ssb, entry, &following, &following_count, 0, depth, NULL, include_blocks);
_get_following(db, statement, entry, &following, &following_count, 0, depth, NULL, include_blocks);
entry->ref_count++;
}
sqlite3_finalize(statement);
tf_ssb_release_db_reader(ssb, db);
int actual_following_count = 0;
for (int i = 0; i < following_count; i++)
@ -1721,12 +1730,17 @@ const char** tf_ssb_db_following_deep_ids(tf_ssb_t* ssb, const char** ids, int c
{
following_t** following = NULL;
int following_count = 0;
sqlite3* db = tf_ssb_acquire_db_reader(ssb);
sqlite3_stmt* statement = _make_following_statement(db);
for (int i = 0; i < count; i++)
{
following_t* entry = _make_following_node(ids[i], &following, &following_count, NULL, false);
_get_following(ssb, entry, &following, &following_count, 0, depth, NULL, false);
_get_following(db, statement, entry, &following, &following_count, 0, depth, NULL, false);
entry->ref_count++;
}
sqlite3_finalize(statement);
tf_ssb_release_db_reader(ssb, db);
int actual_following_count = 0;
for (int i = 0; i < following_count; i++)