Skip to content

Avoid re-encoding old state#36

Open
martosaur wants to merge 2 commits intolaunchscout:mainfrom
martosaur:am-cache-encoded-state
Open

Avoid re-encoding old state#36
martosaur wants to merge 2 commits intolaunchscout:mainfrom
martosaur:am-cache-encoded-state

Conversation

@martosaur
Copy link
Copy Markdown
Contributor

@martosaur martosaur commented Mar 13, 2026

Currently, LiveState encodes both old and new state before diffing. This becomes a bottleneck if the state is updated frequently, e.g. if we're streaming a lot of small updates (LLM response deltas 😄 )

One optimization that can be made is to store encoded old state in the socket assigns so we wouldn't need to re-encode it again. That was initially my plan, but I quickly ran into a wall. MessageBuilder is a slightly awkward abstraction. Looking at the docs, it's main purpose is to make diffing algorithm swappable, but it does more than that:

  • it handles encoding, which is technically swappable, but it's unclear if that's intentional. This protocol is core to the library.
  • it defines message shape, which isn't swappable as JS library code relies on it.

So I went ahead and merged both of these things into the channel itself and made JSONDiff.diff/2 function replaceable with diff_function channel option. I also made channel not increment version and send empty patch if an updated resulted in an empty diff. There was also a bug in push_state_change which used overridable backend state keys for message that was supposed to be sent to the frontend.

Obviously, since message_builder option is deleted, this is a backwards incompatible change. So it's completely at your mercy if you want to go with major version.

[the code in this PR was slightly LLM assisted]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant