Skip to content

Commit c26b364

Browse files
committed
Thin views: a view.update while only the live row changed writes just that row into the last full reply (thin-splice.bend; the render memo in thin-actor.bend: same read model, info, request, anchor, view, renderer and thread, at most 1 s old, same chunk marker, else a full render); test/thin_splice_test.bend: spliced == full render, web and mobile
1 parent 2d62f0a commit c26b364

5 files changed

Lines changed: 362 additions & 25 deletions

File tree

‎src/server/server.bend‎

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10998,22 +10998,46 @@ def Hub.thin.fetch(memory: Bool, +cfg: Cfg, +thread: String, +anchor: Nat) -> IO
1099810998
case True{}: IO.pure(TSTO.Page, TSTO.Page{Cfg.thin(cfg), THIN.Index.page(Cfg.thin(cfg), thread, anchor, 32n)})
1099910999
case False{}: TSTO.Ts.page(Cfg.thin.store(cfg), thread, anchor, 32n, Cfg.thin(cfg))
1100011000

11001-
def Hub.thin.render.page(+pg: TSTO.Page, h: Hub, +client: U32, +o: J.Json, +c: Client, +now: Nat, +context: ThinContext, +p: J.Json) -> IO(Hub):
11001+
def Hub.thin.render.page(+pg: TSTO.Page, h: Hub, +client: U32, +o: J.Json, +c: Client, +now: Nat, +context: ThinContext, +p: J.Json, +key: String, +ms: Nat) -> IO(Hub):
1100211002
match h:
1100311003
case Hub{+st, f, ls, cl, as, inbox, +cfg, note}:
1100411004
+web = String.eq(Client.thin.mode(c), "view-web")
11005+
+stream = TLIVE.Stream.get(Cfg.thin.streams(cfg), TV.Target.thread(st, J.Json.get_obj(p, "target")))
1100511006
+actor = TA.Actor.mail_cache(TA.Actor.refresh(ThinContext.actors(context), web, st, Hub.info(cfg),
1100611007
J.Json.obj([J.KV{"t", J.Str{"live"}}, J.KV{"items", J.Json.arr(Nil{})}]), now, p, ThinContext.view(context)), ThinContext.actors(context), Cfg.thin.mail(cfg))
11007-
Hub.thin.rendered(TA.Actor.render(actor, web, st, TSTO.Page.index(pg), p, Cfg.thin.epoch(cfg), now, TSTO.Page.ids(pg), TLIVE.Stream.get(Cfg.thin.streams(cfg), TV.Target.thread(st, J.Json.get_obj(p, "target")))),
11008+
Hub.thin.rendered(TA.Rendered.remember(TA.Actor.render(actor, web, st, TSTO.Page.index(pg), p, Cfg.thin.epoch(cfg), now, TSTO.Page.ids(pg), stream), key, ms, stream),
1100811009
Hub{st, f, ls, cl, as, inbox, cfg, note}, client, o, c, now, context)
1100911010

11010-
def Hub.thin.render(h: Hub, +client: U32, +o: J.Json, +c: Client, +now: Nat, +context: ThinContext, +anchor: Nat) -> IO(Hub):
11011+
def Hub.thin.render.full(h: Hub, +client: U32, +o: J.Json, +c: Client, +now: Nat, +context: ThinContext, +anchor: Nat, +p: J.Json, +key: String, +ms: Nat) -> IO(Hub):
1101111012
match h:
1101211013
case Hub{+st, f, ls, cl, as, inbox, +cfg, note}:
11013-
+p = Hub.thin.input(String.eq(J.Json.get_str(o, "m", ""), "view.action"), J.Json.get_obj(o, "p"), ThinContext.cid(context))
1101411014
do IO<Hub>:
1101511015
+pg : TSTO.Page <- Hub.thin.fetch(String.is_empty(Cfg.thin.store(cfg)), cfg, TV.Target.thread(st, J.Json.get_obj(p, "target")), anchor)
11016-
Hub.thin.render.page(pg, Hub{st, f, ls, cl, as, inbox, cfg, note}, client, o, c, now, context, p)
11016+
Hub.thin.render.page(pg, Hub{st, f, ls, cl, as, inbox, cfg, note}, client, o, c, now, context, p, key, ms)
11017+
11018+
# a view.update whose render memo still holds (thin-actor.bend's
11019+
# Memo.reply: only the live row can have changed) answers with it spliced
11020+
# (thin-splice.bend); anything else renders in full and is remembered
11021+
def Hub.thin.render.memo(+m: Maybe<&2, J.Json>, h: Hub, +client: U32, +o: J.Json, +c: Client, +now: Nat, +context: ThinContext, +anchor: Nat, +p: J.Json, +key: String, +ms: Nat) -> IO(Hub):
11022+
match m:
11023+
case Some{+res}:
11024+
Hub.thin.respond(h, client, o, res, c, context, now)
11025+
case None{}:
11026+
Hub.thin.render.full(h, client, o, c, now, context, anchor, p, key, ms)
11027+
11028+
def Hub.thin.render.at(h: Hub, +client: U32, +o: J.Json, +c: Client, +now: Nat, +context: ThinContext, +anchor: Nat, +ms: Nat) -> IO(Hub):
11029+
match h:
11030+
case Hub{+st, f, ls, cl, as, inbox, +cfg, note}:
11031+
+p = Hub.thin.input(String.eq(J.Json.get_str(o, "m", ""), "view.action"), J.Json.get_obj(o, "p"), ThinContext.cid(context))
11032+
+web = String.eq(Client.thin.mode(c), "view-web")
11033+
+thread = TV.Target.thread(st, J.Json.get_obj(p, "target"))
11034+
+key = TA.Memo.key(M.State.seq(st), J.Json.show(Hub.info(cfg)), p, anchor, ThinContext.view(context), web, thread)
11035+
Hub.thin.render.memo(TA.Memo.reply(TA.Actor.memos(ThinContext.actors(context)), String.eq(J.Json.get_str(o, "m", ""), "view.update"), key, ms, web,
11036+
TLIVE.Stream.get(Cfg.thin.streams(cfg), thread), thread, Cfg.thin.epoch(cfg), J.Json.get_obj(p, "target"), TA.Actor.loc(ThinContext.actors(context))),
11037+
Hub{st, f, ls, cl, as, inbox, cfg, note}, client, o, c, now, context, anchor, p, key, ms)
11038+
11039+
def Hub.thin.render(h: Hub, +client: U32, +o: J.Json, +c: Client, +now: Nat, +context: ThinContext, +anchor: Nat) -> IO(Hub):
11040+
IO.bind(Nat, Hub, IO.now(), +ms => Hub.thin.render.at(h, client, o, c, now, context, anchor, ms))
1101711041

1101811042
# a search page: over the whole index, or (with a store) over the
1101911043
# candidates it fetched (TSTO.Ts.search); the same rows either way

‎src/server/thin-actor.bend‎

Lines changed: 76 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,27 @@ import ../core/thin-live.bend as TL
1111
import ../web/app.bend as A
1212
import ../web/mail-actions.bend as MA
1313
import ../mobile/hubs.bend as H
14+
import ./thin-splice.bend as S
15+
import ../core/i18n.bend as I
1416

1517
# Private, connection-owned UI state. It never enters a client snapshot or
1618
# persisted event. Keep it behind a List in the connection context so the
1719
# native ABI does not flatten a second full Ui into every Client argument.
20+
# memo: the last full render's reply and what it was made of (at most
21+
# one; thin-splice.bend writes only the live row into it until anything
22+
# else changes or a second passes: Memo.splice)
23+
type Memo is Data:
24+
Memo{key: String, clock: Nat, partial: Bool, result: J.Json}
25+
1826
type Actor is Data:
19-
Actor{ui: U.Ui, view: String, target: J.Json, pending: Map<&2, String>, reads: List<&2, J.Json>, dirty: Bool}
27+
Actor{ui: U.Ui, view: String, target: J.Json, pending: Map<&2, String>, reads: List<&2, J.Json>, dirty: Bool, memo: List<&2, Memo>}
2028

2129
type Rendered is Data:
2230
Rendered{actor: Actor, result: J.Json}
2331

2432
def Actor.ui( +a: Actor) -> U.Ui:
2533
match a:
26-
case Actor{u, _, _, _, _, _}: u
34+
case Actor{u, _, _, _, _, _, _}: u
2735

2836
def Actor.scope(+target: J.Json) -> String:
2937
J.Json.show(V.Target.json(J.Json.get_str(target, "kind", ""), J.Json.get_str(target, "id", "")))
@@ -114,28 +122,28 @@ def Actor.merge( +fresh: U.Ui, +old: U.Ui, +same: Bool, +thread: String, +presen
114122
M.Time.max(rid, rid2), want, error, note, Actor.prefs(Map.to_list(&2, String, nf2), nf), outbox, stores}
115123

116124
def Actor.make(+u: U.Ui, +view: String, +target: J.Json) -> Actor:
117-
Actor{u, view, target, Map.new(&2, String), Nil{}, False{}}
125+
Actor{u, view, target, Map.new(&2, String), Nil{}, False{}, Nil{}}
118126

119127
def Actor.refresh( +xs: List<&2, Actor>, +web: Bool, +st: M.State, +info: J.Json, +live: J.Json, +now: Nat, +p: J.Json, +view: String) -> Actor:
120128
match xs:
121129
case Nil{}: Actor.make(V.Session.current(web, st, info, live, now, Actor.safe_input(st, p)), view, J.Json.get_obj(p, "target"))
122-
case Actor{old, oldView, +target, pending, reads, dirty} <> _:
130+
case Actor{old, oldView, +target, pending, reads, dirty, _} <> _:
123131
+input = Actor.safe_input(st, p)
124132
+thread = V.Target.thread(st, J.Json.get_obj(p, "target"))
125133
+fresh = V.Session.current(web, st, info, live, now, input)
126134
+same = Bool.and(String.eq(oldView, view), W.Target.same(target, J.Json.get_obj(p, "target")))
127135
Actor{V.Session.renderer(web, U.Ui.info_merge(Actor.merge(fresh, old, same, thread, Maybe.is_some(&2, J.Json, J.Json.get(J.Json.get_obj(J.Json.get_obj(input, "session"), "drafts"), thread))), info), p),
128-
view, J.Json.get_obj(p, "target"), Bool.pick(Map<&2, String>, same, pending, Map.new(&2, String)), Bool.pick(List<&2, J.Json>, same, reads, Nil{}), dirty}
136+
view, J.Json.get_obj(p, "target"), Bool.pick(Map<&2, String>, same, pending, Map.new(&2, String)), Bool.pick(List<&2, J.Json>, same, reads, Nil{}), dirty, Nil{}}
129137

130138
# The hub's canonical projection is shared; each actor keeps its own pages,
131139
# drafts, request slots and exact scope. Compare only the selected body's
132140
# newest identity across authoritative states, never its text or full log.
133141
def Actor.mail_cache(+a: Actor, previous: List<&2, Actor>, cache: TM.Cache) -> Actor:
134142
match a previous:
135-
case Actor{u, view, target, pending, reads, dirty} Nil{}:
136-
Actor{TM.Cache.install(cache, u), view, target, pending, reads, dirty}
137-
case Actor{u, view, target, pending, reads, dirty} old <> _:
138-
Actor{TM.Cache.refresh(cache, u, Actor.ui(old)), view, target, pending, reads, dirty}
143+
case Actor{u, view, target, pending, reads, dirty, memo} Nil{}:
144+
Actor{TM.Cache.install(cache, u), view, target, pending, reads, dirty, memo}
145+
case Actor{u, view, target, pending, reads, dirty, memo} old <> _:
146+
Actor{TM.Cache.refresh(cache, u, Actor.ui(old)), view, target, pending, reads, dirty, memo}
139147

140148
def Actor.rpc(+command: J.Json) -> J.Json:
141149
Maybe.default(&2, J.Json, J.Json.parse(J.Json.get_str(command, "text", "")), J.Null{})
@@ -166,16 +174,16 @@ def Actor.reads( +cs: List<&2, J.Json>, +acc: List<&2, J.Json>) -> List<&2, J.Js
166174

167175
def Actor.accept( +a: Actor, +r: V.ActResult, +queue: Bool) -> Actor:
168176
match a r:
169-
case Actor{_, view, +target, pending, reads, dirty} V.ActResult{u, +cs}:
177+
case Actor{_, view, +target, pending, reads, dirty, memo} V.ActResult{u, +cs}:
170178
Actor{u, view, target, Actor.note(cs, pending, Actor.scope(target)),
171-
List.append(&2, J.Json, reads, Bool.pick(List<&2, J.Json>, queue, Actor.reads(cs, Nil{}), Nil{})), dirty}
179+
List.append(&2, J.Json, reads, Bool.pick(List<&2, J.Json>, queue, Actor.reads(cs, Nil{}), Nil{})), dirty, memo}
172180

173181
def Actor.mail_ready( +a: Actor) -> Actor:
174182
Actor.accept(a, V.ActResult.web(MA.MA.after(U.Mailv.sync(U.Act{Actor.ui(a), Nil{}}))), True{})
175183

176184
def Actor.origin( +a: Actor, +origin: String) -> Actor:
177185
match a:
178-
case Actor{u, view, target, pending, reads, dirty}: Actor{U.Ui.info_set(u, "origin", origin), view, target, pending, reads, dirty}
186+
case Actor{u, view, target, pending, reads, dirty, memo}: Actor{U.Ui.info_set(u, "origin", origin), view, target, pending, reads, dirty, memo}
179187

180188
def Actor.mail_context_if( +shown: Bool, +u: U.Ui) -> J.Json:
181189
match shown:
@@ -202,7 +210,7 @@ def Actor.receive( +web: Bool, +u: U.Ui, +key: String, +now: Nat, +o: J.Json) ->
202210

203211
def Actor.mark_dirty( +a: Actor) -> Actor:
204212
match a:
205-
case Actor{u, view, target, pending, reads, _}: Actor{u, view, target, pending, reads, True{}}
213+
case Actor{u, view, target, pending, reads, _, _}: Actor{u, view, target, pending, reads, True{}, Nil{}}
206214

207215
def Actor.now( +u: U.Ui) -> Nat:
208216
match u:
@@ -215,26 +223,26 @@ def Actor.reply_if( +owned: Bool, +a: Actor, +web: Bool, +key: String, +o: J.Jso
215223

216224
def Actor.reply(+a: Actor, +web: Bool, +key: String, +o: J.Json) -> Actor:
217225
match a:
218-
case Actor{u, view, +target, +pending, reads, dirty}:
226+
case Actor{u, view, +target, +pending, reads, dirty, memo}:
219227
+id = J.Json.as_raw(J.Json.get(o, "id"), "")
220228
Actor.reply_if(Bool.and(String.eq(J.Json.get_str(o, "t", ""), "reply"), String.eq(Actor.pending_scope(pending, id), Actor.scope(target))),
221-
Actor{u, view, target, Map.del(&2, String, pending, id), reads, dirty}, web, key, o)
229+
Actor{u, view, target, Map.del(&2, String, pending, id), reads, dirty, memo}, web, key, o)
222230

223231
def Actor.clean( +a: Actor) -> Actor:
224232
match a:
225-
case Actor{u, view, target, pending, _, _}: Actor{u, view, target, pending, Nil{}, False{}}
233+
case Actor{u, view, target, pending, _, _, memo}: Actor{u, view, target, pending, Nil{}, False{}, memo}
226234

227235
def Actor.queued( +a: Actor) -> List<&2, J.Json>:
228236
match a:
229-
case Actor{_, _, _, _, reads, _}: reads
237+
case Actor{_, _, _, _, reads, _, _}: reads
230238

231239
def Actor.dirty( +a: Actor) -> Bool:
232240
match a:
233-
case Actor{_, _, _, _, _, dirty}: dirty
241+
case Actor{_, _, _, _, _, dirty, _}: dirty
234242

235243
def Actor.capacity( +a: Actor) -> Bool:
236244
match a:
237-
case Actor{_, _, _, pending, reads, _}: Bool.and(Nat.is_le(List.length(&2, String, Map.keys(&2, String, pending)), 128n), Nat.is_le(List.length(&2, J.Json, reads), 16n))
245+
case Actor{_, _, _, pending, reads, _, _}: Bool.and(Nat.is_le(List.length(&2, String, Map.keys(&2, String, pending)), 128n), Nat.is_le(List.length(&2, J.Json, reads), 16n))
238246

239247
# A queued read is useful only to the exact surviving connection incarnation
240248
# and view that produced it. Equal RPC ids on another socket grant nothing.
@@ -244,3 +252,52 @@ def Job.valid(+actualClient: U32, +jobClient: U32, +actualGen: U32, +jobGen: U32
244252

245253
def Control.fresh(+id: Nat, +last: Nat) -> Bool:
246254
Bool.and(Nat.is_gt(id, Nat.mul(65536n, 2147483648n)), Bool.and(Nat.is_le(id, P.Session.nat.max()), Nat.is_gt(id, last)))
255+
256+
# The render memo (thin-splice.bend)
257+
# ----------------------------------
258+
259+
def Actor.memos(xs: List<&2, Actor>) -> List<&2, Memo>:
260+
match xs:
261+
case Actor{_, _, _, _, _, _, memo} <> _: memo
262+
case Nil{}: Nil{}
263+
264+
def Actor.loc(xs: List<&2, Actor>) -> I.Loc:
265+
match xs:
266+
case Actor{u, _, _, _, _, _, _} <> _: U.Lang.loc(u)
267+
case Nil{}: I.En{}
268+
269+
def Actor.remember(+a: Actor, +m: Memo) -> Actor:
270+
match a:
271+
case Actor{u, view, target, pending, reads, dirty, _}: Actor{u, view, target, pending, reads, dirty, [m]}
272+
273+
# what a render is made of but its live row: the read model's sequence, the
274+
# hub's info, the request but its revisions, the page anchor, the view, the
275+
# renderer and the thread (a reply arriving clears the memo: Actor.reply_if)
276+
def Memo.request(+p: J.Json) -> J.Json:
277+
match p:
278+
case J.Obj{+fs}: J.Obj{V.Obj.drop(V.Obj.drop(fs, "requestRevision"), "viewRevision")}
279+
case _: p
280+
281+
def Memo.key(+seq: Nat, +info: String, +p: J.Json, +anchor: Nat, +view: String, +web: Bool, +thread: String) -> String:
282+
J.Json.show(J.Json.obj([J.KV{"seq", J.Json.nat(seq)}, J.KV{"info", J.Str{info}}, J.KV{"p", Memo.request(p)}, J.KV{"anchor", J.Json.nat(anchor)},
283+
J.KV{"view", J.Str{view}}, J.KV{"web", J.Flag{web}}, J.KV{"thread", J.Str{thread}}]))
284+
285+
def Memo.reply.k(+ok: Bool, +res: J.Json, +web: Bool, +stream: TL.Stream, +thread: String, +epoch: String, +target: J.Json, +lc: I.Loc) -> Maybe<&2, J.Json>:
286+
match ok:
287+
case True{}: S.Splice.reply(web, res, stream, thread, epoch, target, lc)
288+
case False{}: None{}
289+
290+
# the memo's reply with only the live row and window new, when nothing else
291+
# can have changed: the same key, at most a second old (clock text), a live
292+
# row now with the same chunk marker as then; None: render in full
293+
def Memo.reply(ms: List<&2, Memo>, +update: Bool, +key: String, +clock: Nat, +web: Bool, +stream: TL.Stream, +thread: String, +epoch: String, +target: J.Json, +lc: I.Loc) -> Maybe<&2, J.Json>:
294+
match ms:
295+
case Nil{}: None{}
296+
case Memo{+k, +at, +partial, +res} <> _:
297+
Memo.reply.k(Bool.and(update, Bool.and(String.eq(k, key), Bool.and(Nat.is_ge(clock, at), Bool.and(Nat.is_lt(Nat.sub(clock, at), 1000n),
298+
Bool.and(Nat.is_gt(TL.Stream.size(stream), 0n), Bool.or(Bool.and(partial, S.Splice.partial(stream)), Bool.and(Bool.not(partial), Bool.not(S.Splice.partial(stream))))))))), res, web, stream, thread, epoch, target, lc)
299+
300+
# a full render remembered for the next ones (clock: ms)
301+
def Rendered.remember(r: Rendered, +key: String, +clock: Nat, +stream: TL.Stream) -> Rendered:
302+
match r:
303+
case Rendered{+a, +res}: Rendered{Actor.remember(a, Memo{key, clock, S.Splice.partial(stream), res}), res}

0 commit comments

Comments
 (0)