Asynchronous parsing

Is it possible to break up the parser by having it do some parsing, then return a continuation to do more? Then you can interleave parsing work with a Cmd to run the continuation, which allows the UI to process queued Cmds?

I used this technique here:

https://package.elm-lang.org/packages/the-sett/ai-search/latest/Search#nextN

Note that SearchResult allows a continuation where there is still more searching to do:

type SearchResult state
    = Complete
    | Goal state (() -> SearchResult state)
    | Ongoing state (() -> SearchResult state)

I experimented with using nextN with batches ranging in size from 10 to 10,000. With small batches the UI remained responsive, but the search did not run very fast. With large batches I could get better performance out of the search, but the UI was very unresponsive. I concluded that if you really need a burst of 100% CPU work that will last longer that a few milliseconds - you are going to need to look into web workers.

As far as I know, you cannot just pass anything between the main UI thread and a webworker when both are built with Elm - you do it through ports and need to encode via JSON.

2 Likes