This repository has no description
0

Configure Feed

Select the types of activity you want to include in your feed.

perf: batch resolveVertexMeta queries, drop contrail init from getIslands

resolveVertexMeta was doing 14 sequential D1 queries (cards+citations,
chunked at 50). Now builds all statements upfront and runs them in a
single db.batch() call. Also removed ensureContrailReady from getIslands
entirely — it only needs D1.

940ms → 320ms cold, 200ms warm.

👾 Generated with [Letta Code](https://letta.com)

Co-Authored-By: Letta Code <noreply@letta.com>

author
nandi
co-author
Letta Code
date (May 14, 2026, 3:59 AM UTC) commit 4366ef7e parent 76253998
+67 -66
+67 -66
src/worker.ts
··· 150 150 const atUris = uris.filter(u => u.startsWith("at://")); 151 151 const CHUNK = 50; 152 152 153 - // ── Resolve HTTP URIs (existing logic) ── 153 + // Build all queries upfront, then batch-execute in one D1 round trip 154 + const stmts: D1PreparedStatement[] = []; 155 + 156 + // HTTP URI queries — cards 157 + const httpChunks: string[][] = []; 158 + for (let i = 0; i < httpUris.length; i += CHUNK) { 159 + const chunk = httpUris.slice(i, i + CHUNK); 160 + httpChunks.push(chunk); 161 + const placeholders = chunk.map(() => "?").join(","); 162 + stmts.push( 163 + db.prepare( 164 + `SELECT record FROM records_card WHERE json_extract(record, '$.content.url') IN (${placeholders})` 165 + ).bind(...chunk) 166 + ); 167 + } 154 168 155 - // Batch card lookup 169 + // HTTP URI queries — citations 156 170 for (let i = 0; i < httpUris.length; i += CHUNK) { 157 171 const chunk = httpUris.slice(i, i + CHUNK); 158 172 const placeholders = chunk.map(() => "?").join(","); 159 - const rows = await db 160 - .prepare( 161 - `SELECT record FROM records_card 162 - WHERE json_extract(record, '$.content.url') IN (${placeholders})`, 163 - ) 164 - .bind(...chunk) 165 - .all<{ record: string }>(); 173 + stmts.push( 174 + db.prepare( 175 + `SELECT record FROM records_citation WHERE json_extract(record, '$.url') IN (${placeholders})` 176 + ).bind(...chunk) 177 + ); 178 + } 166 179 167 - for (const row of rows.results || []) { 180 + // AT URI queries — cards 181 + const atChunks: string[][] = []; 182 + for (let i = 0; i < atUris.length; i += CHUNK) { 183 + const chunk = atUris.slice(i, i + CHUNK); 184 + atChunks.push(chunk); 185 + const placeholders = chunk.map(() => "?").join(","); 186 + stmts.push( 187 + db.prepare(`SELECT uri, record FROM records_card WHERE uri IN (${placeholders})`).bind(...chunk) 188 + ); 189 + } 190 + 191 + // AT URI queries — collections 192 + for (let i = 0; i < atUris.length; i += CHUNK) { 193 + const chunk = atUris.slice(i, i + CHUNK); 194 + const placeholders = chunk.map(() => "?").join(","); 195 + stmts.push( 196 + db.prepare(`SELECT uri, record FROM records_collection WHERE uri IN (${placeholders})`).bind(...chunk) 197 + ); 198 + } 199 + 200 + // Execute all queries in one batch 201 + const results = stmts.length > 0 ? await db.batch(stmts) : []; 202 + let resultIdx = 0; 203 + 204 + // Process card results (HTTP) 205 + for (const chunk of httpChunks) { 206 + const rows = (results[resultIdx++] as { results: { record: string }[] } | null)?.results || []; 207 + for (const row of rows) { 168 208 try { 169 209 const card = JSON.parse(row.record); 170 210 const url = card?.content?.url; 171 211 if (!url || !meta.has(url)) continue; 172 212 const m = card.content.metadata; 173 213 if (m?.title || m?.description) { 174 - meta.set(url, { 175 - title: m.title || url, 176 - description: m.description || "", 177 - type: card.type === "URL" ? "url" : "note", 178 - }); 214 + meta.set(url, { title: m.title || url, description: m.description || "", type: card.type === "URL" ? "url" : "note" }); 179 215 } 180 216 } catch {} 181 217 } 182 218 } 183 219 184 - // Batch citation lookup (overrides card data if found) 185 - for (let i = 0; i < httpUris.length; i += CHUNK) { 186 - const chunk = httpUris.slice(i, i + CHUNK); 187 - const placeholders = chunk.map(() => "?").join(","); 188 - const rows = await db 189 - .prepare( 190 - `SELECT record FROM records_citation 191 - WHERE json_extract(record, '$.url') IN (${placeholders})`, 192 - ) 193 - .bind(...chunk) 194 - .all<{ record: string }>(); 195 - 196 - for (const row of rows.results || []) { 220 + // Process citation results (HTTP) — overrides card data 221 + for (let i = 0; i < httpChunks.length; i++) { 222 + const rows = (results[resultIdx++] as { results: { record: string }[] } | null)?.results || []; 223 + for (const row of rows) { 197 224 try { 198 225 const citation = JSON.parse(row.record); 199 226 const url = citation.url; 200 227 if (!url || !meta.has(url)) continue; 201 228 if (citation.title) { 202 229 const existing = meta.get(url)!; 203 - meta.set(url, { 204 - title: citation.title, 205 - description: citation.takeaway || existing.description, 206 - type: "citation", 207 - }); 230 + meta.set(url, { title: citation.title, description: citation.takeaway || existing.description, type: "citation" }); 208 231 } 209 232 } catch {} 210 233 } 211 234 } 212 235 213 - // ── Resolve AT URIs ── 214 - 215 - // Batch card lookup by AT URI 216 - for (let i = 0; i < atUris.length; i += CHUNK) { 217 - const chunk = atUris.slice(i, i + CHUNK); 218 - const placeholders = chunk.map(() => "?").join(","); 219 - const rows = await db 220 - .prepare(`SELECT uri, record FROM records_card WHERE uri IN (${placeholders})`) 221 - .bind(...chunk) 222 - .all<{ uri: string; record: string }>(); 223 - 224 - for (const row of rows.results || []) { 236 + // Process card results (AT) 237 + for (const chunk of atChunks) { 238 + const rows = (results[resultIdx++] as { results: { uri: string; record: string }[] } | null)?.results || []; 239 + for (const row of rows) { 225 240 try { 226 241 const card = JSON.parse(row.record); 227 242 const m = card?.content?.metadata; 228 243 const url = card?.content?.url; 229 244 if (meta.has(row.uri)) { 230 - meta.set(row.uri, { 231 - title: m?.title || url || row.uri, 232 - description: m?.description || "", 233 - type: "card", 234 - }); 245 + meta.set(row.uri, { title: m?.title || url || row.uri, description: m?.description || "", type: "card" }); 235 246 } 236 247 } catch {} 237 248 } 238 249 } 239 250 240 - // Batch collection lookup by AT URI 241 - for (let i = 0; i < atUris.length; i += CHUNK) { 242 - const chunk = atUris.slice(i, i + CHUNK); 243 - const placeholders = chunk.map(() => "?").join(","); 244 - const rows = await db 245 - .prepare(`SELECT uri, record FROM records_collection WHERE uri IN (${placeholders})`) 246 - .bind(...chunk) 247 - .all<{ uri: string; record: string }>(); 248 - 249 - for (const row of rows.results || []) { 251 + // Process collection results (AT) 252 + for (let i = 0; i < atChunks.length; i++) { 253 + const rows = (results[resultIdx++] as { results: { uri: string; record: string }[] } | null)?.results || []; 254 + for (const row of rows) { 250 255 try { 251 256 const coll = JSON.parse(row.record); 252 257 if (meta.has(row.uri)) { 253 - meta.set(row.uri, { 254 - title: coll.name || row.uri, 255 - description: coll.description || "", 256 - type: "collection", 257 - }); 258 + meta.set(row.uri, { title: coll.name || row.uri, description: coll.description || "", type: "collection" }); 258 259 } 259 260 } catch {} 260 261 } ··· 411 412 app.get("/xrpc/org.latha.strata.getIslands", async (c) => { 412 413 const limit = Math.min(parseInt(c.req.query("limit") || "50"), 100); 413 414 const summary = c.req.query("summary") === "true"; 414 - if (!summary) await ensureContrailReady(db); 415 + // No ensureContrailReady — just D1 queries 415 416 416 417 const rows = await db 417 418 .prepare("SELECT uri, record FROM records_strata ORDER BY time_us DESC LIMIT ?")