fix(sessions): extract session_search_entry to fix ELC OOM in session_search
The while loop in session_search had too many let bindings in scope; the ELC compiler's exponential rebinding accumulation caused OOM and truncation of dist/sessions.c since June 30. Moving the per-node logic into session_search_entry gives the compiler a clean scope boundary per call, restoring O(N) compile behaviour.
This commit is contained in:
+27
-16
@@ -373,6 +373,32 @@ fn session_update_patch(session_id: String, body: String) -> String {
|
||||
+ ",\"updated_at\":" + int_to_str(ts) + "}"
|
||||
}
|
||||
|
||||
// session_search_entry — extract one search-result entry from a raw node JSON.
|
||||
// Returns a JSON object string or "" if the node is not a valid session:meta node.
|
||||
//
|
||||
// Extracted from session_search's while loop body to reduce the loop's lexical
|
||||
// complexity. The ELC compiler runs out of memory processing while loops with
|
||||
// many `let` bindings — extracting the body into a separate function gives the
|
||||
// compiler a clean scope boundary at each call. Each function compiles in O(N)
|
||||
// rather than the exponential growth caused by rebinding accumulation inside loops.
|
||||
// (2026-07-01 self-review: root cause of sessions.c OOM/truncation since June 30)
|
||||
fn session_search_entry(node: String) -> String {
|
||||
let label: String = json_get(node, "label")
|
||||
if !str_eq(label, "session:meta") { return "" }
|
||||
let content: String = json_get(node, "content")
|
||||
let sess_id: String = json_get(content, "id")
|
||||
if str_eq(sess_id, "") { return "" }
|
||||
let title: String = json_get(content, "title")
|
||||
let created_raw: String = json_get(content, "created_at")
|
||||
let updated_raw: String = json_get(content, "updated_at")
|
||||
let eff_created: String = if str_eq(created_raw, "") { "0" } else { created_raw }
|
||||
let eff_updated: String = if str_eq(updated_raw, "") { eff_created } else { updated_raw }
|
||||
let e_id: String = "{\"id\":\"" + json_safe(sess_id) + "\""
|
||||
let e_title: String = ",\"title\":\"" + json_safe(title) + "\""
|
||||
let e_ts: String = ",\"created_at\":" + eff_created + ",\"updated_at\":" + eff_updated + "}"
|
||||
return e_id + e_title + e_ts
|
||||
}
|
||||
|
||||
// session_search — search session:meta nodes whose content matches query.
|
||||
fn session_search(query: String) -> String {
|
||||
if str_eq(query, "") { return "[]" }
|
||||
@@ -383,22 +409,7 @@ fn session_search(query: String) -> String {
|
||||
let out: String = ""
|
||||
let i: Int = 0
|
||||
while i < total {
|
||||
let node: String = json_array_get(results, i)
|
||||
let label: String = json_get(node, "label")
|
||||
let content: String = json_get(node, "content")
|
||||
let is_session: Bool = str_eq(label, "session:meta")
|
||||
let sess_id: String = json_get(content, "id")
|
||||
let title: String = json_get(content, "title")
|
||||
let created_raw: String = json_get(content, "created_at")
|
||||
let updated_raw: String = json_get(content, "updated_at")
|
||||
let eff_created: String = if str_eq(created_raw, "") { "0" } else { created_raw }
|
||||
let eff_updated: String = if str_eq(updated_raw, "") { eff_created } else { updated_raw }
|
||||
let entry: String = if is_session && !str_eq(sess_id, "") {
|
||||
"{\"id\":\"" + json_safe(sess_id) + "\""
|
||||
+ ",\"title\":\"" + json_safe(title) + "\""
|
||||
+ ",\"created_at\":" + eff_created
|
||||
+ ",\"updated_at\":" + eff_updated + "}"
|
||||
} else { "" }
|
||||
let entry: String = session_search_entry(json_array_get(results, i))
|
||||
let out = if !str_eq(entry, "") {
|
||||
if str_eq(out, "") { entry } else { out + "," + entry }
|
||||
} else { out }
|
||||
|
||||
Reference in New Issue
Block a user