2 Commits

Author SHA1 Message Date
1f2664e5a8 ssb: Reduce redundant work in ssb.following.
All checks were successful
Build Tilde Friends / Build-All (push) Successful in 33m15s
2025-05-11 22:20:16 -04:00
35656a5c34 prettier 2025-05-11 21:54:53 -04:00
4 changed files with 80 additions and 61 deletions

View File

@@ -167,16 +167,23 @@ class TfElement extends LitElement {
if (ids.indexOf(id) == -1) { if (ids.indexOf(id) == -1) {
delete cache.about[id]; delete cache.about[id];
} else { } else {
users[id] = Object.assign( users[id] = Object.assign(users[id] || {}, cache.about[id]);
users[id] || {},
cache.about[id]
);
} }
} }
let ids_out_of_date = ids.filter(x => cache.about[x]?.seq === undefined || (users[x]?.seq && users[x]?.seq > cache.about[x].seq)); let ids_out_of_date = ids.filter(
(x) =>
cache.about[x]?.seq === undefined ||
(users[x]?.seq && users[x]?.seq > cache.about[x].seq)
);
console.log('loading about for', ids.length, 'accounts', ids_out_of_date.length, 'out of date'); console.log(
'loading about for',
ids.length,
'accounts',
ids_out_of_date.length,
'out of date'
);
if (ids_out_of_date.length) { if (ids_out_of_date.length) {
try { try {
let rows = await tfrpc.rpc.query( let rows = await tfrpc.rpc.query(
@@ -219,12 +226,12 @@ class TfElement extends LitElement {
if (!cache.about[id]?.seq) { if (!cache.about[id]?.seq) {
cache.about[id] = {seq: users[id]?.seq ?? 0}; cache.about[id] = {seq: users[id]?.seq ?? 0};
} }
}; }
let new_cache = JSON.stringify(cache); let new_cache = JSON.stringify(cache);
if (new_cache != original_cache) { if (new_cache != original_cache) {
let start_time = new Date(); let start_time = new Date();
tfrpc.rpc.databaseSet('about', new_cache).then(function() { tfrpc.rpc.databaseSet('about', new_cache).then(function () {
console.log('saving about took', (new Date() - start_time) / 1000); console.log('saving about took', (new Date() - start_time) / 1000);
}); });
} }

View File

@@ -242,12 +242,11 @@ class TfProfileElement extends LitElement {
</div> </div>
</div>` </div>`
: null; : null;
let image =profile.image; let image = profile.image;
if (typeof image == 'string' && !image.startsWith('&')) { if (typeof image == 'string' && !image.startsWith('&')) {
try { try {
image = JSON.parse(image)?.link; image = JSON.parse(image)?.link;
} catch { } catch {}
}
} }
image = this.editing?.image ?? image; image = this.editing?.image ?? image;
let description = this.editing?.description ?? profile.description; let description = this.editing?.description ?? profile.description;

View File

@@ -41,8 +41,7 @@ class TfUserElement extends LitElement {
if (typeof image_link == 'string' && !image_link.startsWith('&')) { if (typeof image_link == 'string' && !image_link.startsWith('&')) {
try { try {
image_link = JSON.parse(image_link)?.link; image_link = JSON.parse(image_link)?.link;
} catch { } catch {}
}
} }
if (image_link !== undefined) { if (image_link !== undefined) {
image = html`<img image = html`<img

View File

@@ -1577,75 +1577,65 @@ static following_t* _make_following_node(const char* id, following_t*** followin
return entry; 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); if (sqlite3_bind_text(statement, 1, entry->id, -1, NULL) == SQLITE_OK)
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)
{ {
while (sqlite3_step(statement) == SQLITE_ROW) const char* contact = (const char*)sqlite3_column_text(statement, 0);
if (sqlite3_column_type(statement, 1) != SQLITE_NULL)
{ {
const char* contact = (const char*)sqlite3_column_text(statement, 0); bool is_following = sqlite3_column_int(statement, 1) != 0;
if (sqlite3_column_type(statement, 1) != SQLITE_NULL) following_t* next = _make_following_node(contact, following, following_count, active_blocks, include_blocks);
if (next)
{ {
bool is_following = sqlite3_column_int(statement, 1) != 0; if (is_following)
following_t* next = _make_following_node(contact, following, following_count, active_blocks, include_blocks);
if (next)
{ {
if (is_following) if (_add_following_entry(&entry->following, &entry->following_count, next))
{ {
if (_add_following_entry(&entry->following, &entry->following_count, next)) next->ref_count++;
{
next->ref_count++;
}
} }
else }
else
{
if (_remove_following_entry(&entry->following, &entry->following_count, next))
{ {
if (_remove_following_entry(&entry->following, &entry->following_count, next)) next->ref_count--;
{
next->ref_count--;
}
} }
} }
} }
if (sqlite3_column_type(statement, 2) != SQLITE_NULL) }
if (sqlite3_column_type(statement, 2) != SQLITE_NULL)
{
bool is_blocking = sqlite3_column_int(statement, 2) != 0;
following_t* next = _make_following_node(contact, following, following_count, active_blocks, include_blocks);
if (next)
{ {
bool is_blocking = sqlite3_column_int(statement, 2) != 0; if (is_blocking)
following_t* next = _make_following_node(contact, following, following_count, active_blocks, include_blocks);
if (next)
{ {
if (is_blocking) if (_add_following_entry(&entry->blocking, &entry->blocking_count, next))
{ {
if (_add_following_entry(&entry->blocking, &entry->blocking_count, next)) next->block_ref_count++;
{
next->block_ref_count++;
}
} }
else }
else
{
if (_remove_following_entry(&entry->blocking, &entry->blocking_count, next))
{ {
if (_remove_following_entry(&entry->blocking, &entry->blocking_count, next)) next->block_ref_count--;
{
next->block_ref_count--;
}
} }
} }
} }
} }
} }
sqlite3_finalize(statement);
} }
tf_ssb_release_db_reader(ssb, db); sqlite3_reset(statement);
} }
static void _get_following( static void _get_following(sqlite3* db, sqlite3_stmt* statement, following_t* entry, following_t*** following, int* following_count, int depth, int max_depth,
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) block_node_t* active_blocks, bool include_blocks)
{ {
int old_depth = entry->depth; int old_depth = entry->depth;
entry->depth = tf_min(depth, entry->depth); entry->depth = tf_min(depth, entry->depth);
@@ -1656,28 +1646,47 @@ static void _get_following(
if (!entry->populated) if (!entry->populated)
{ {
entry->populated = true; 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 }; block_node_t blocks = { .entry = entry, .parent = active_blocks };
for (int i = 0; i < entry->following_count; i++) 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) 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; following_t** following = NULL;
int following_count = 0; 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++) for (int i = 0; i < count; i++)
{ {
following_t* entry = _make_following_node(ids[i], &following, &following_count, NULL, include_blocks); 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++; entry->ref_count++;
} }
sqlite3_finalize(statement);
tf_ssb_release_db_reader(ssb, db);
int actual_following_count = 0; int actual_following_count = 0;
for (int i = 0; i < following_count; i++) 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; following_t** following = NULL;
int following_count = 0; 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++) for (int i = 0; i < count; i++)
{ {
following_t* entry = _make_following_node(ids[i], &following, &following_count, NULL, false); 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++; entry->ref_count++;
} }
sqlite3_finalize(statement);
tf_ssb_release_db_reader(ssb, db);
int actual_following_count = 0; int actual_following_count = 0;
for (int i = 0; i < following_count; i++) for (int i = 0; i < following_count; i++)