Storing Sollang streams in variables and streaming mouse events
Public postsEnglish
Keep Sollang’s Stream<T> as a lazy execution plan without allocation, store it in variables, pass it across library boundaries, and connect Windows and Linux mouse events through a bounded EventStream<T>.
Sollang’s stream was designed from the beginning to be “code where values flow from left to right”.
However, as I continued to look at the examples, one issue emerged.
1..100
-> map { ... }
-> filter { ... }
-> each { ... }
This version is beautiful and fast. However, it only consumes each right at the end, and cannot store the stream itself in a variable or be returned by a library function.
The starting point for this project was simple.
Store the stream in a variable, and then use that variable for each.
Now, in Sollang, the following format actually works.
import std.sequence
makeValues values: Range -> Stream<Int> {
values -> defer
}
main {
1..3 -> makeValues => values
values -> each value {
"$value" -> println
}
}
Stream is not a container
Stream<T> was not created as an array-like container. It is still a delayed execution plan.
The local pipeline is completely fused into the original loop. There is no intermediate array, no iterator object, and no callback objects are created at each step.
Only when crossing the boundaries of functions or libraries, does the producer ABI composed of the next three values be used.
- producer context
next(context, output)drop(context)
That is, even when being libraryized, the entire data is not copied into memory. It is consumed one value at a time.
And the stream is an affine value. If a stream that has been consumed as terminal each is used again, a compilation error will occur. For performance reasons, its lifetime is not hidden; it is revealed according to the ownership rules.
EventStream<T> follows the same philosophy
Events such as mouse movements or clicks are slightly more complex than regular streams. The producer is external, consumers may be slow, and when closing, you need to clean up the background workers and terminal status.
So, I added the following rule to EventStream<T>.
- A fixed-capacity ring buffer that does not expand after creation
DropNewest,DropOldest,CoalesceMotionoverflow policy- Cancellation request and unblocking of blocked I/O
- worker join
- Restore console and terminal mode
In Windows, it reads the actual MOUSE_EVENT_RECORD from ReadConsoleInputW. On Linux, it handles the SGR 1003/1006 mouse protocol.
Since mouse movements occur very frequently, an unlimited queue was not chosen. In particular, CoalesceMotion allows processing to focus on the latest position instead of accumulating all old movement coordinates.
Why didn't the browser support it directly?
The browser Pointer Event is passed to the host event loop via a callback.
However, in the current synchronous pull stream, if events are awaited on the same main thread, the event callback cannot be executed. Instead of just pretending to support polling or leaving unresolved imports, we now output a clear capability diagnostic.
It was determined that EventStream for browsers should be connected via the later host-driven callback lowering mechanism.
Even println2, which was quietly ignored, has been modified too
When executing the following code in Playground, the previously incorrect function calls are ignored.
"값이 왼쪽에서 오른쪽으로 흐릅니다." -> println2
Now, the next error appears normally.
알 수 없는 함수 호출 'println2'
Reference compiler, self-host compiler, browser Stage2 and the actual public playground UI are all configured with the same behavior.
Verification result
- Full Windows Regression 785/785
- Full Linux regression 785/785
- Windows/Linux Stage2 verification passed
- Windows/Linux Stage3 fixed point alignment
- 5 browser Stream programs and error diagnosis passed
- Retirement Sollang 0.2.260725 Installation and Execution Verification
- Confirming public playground TLS and actual UI execution
Stage2 The overall analysis has been reduced to around 60 seconds based on the original 101.8 seconds. Even though the feature surface has expanded, it is now faster than before.
“Not using any memory at all” is not accurate. Local fusion has no intermediate allocation, while at the library boundary, EventStream uses a fixed-size ring buffer defined by the user.
The important point is that memory usage does not secretly increase according to the size of input data.
With this implementation, the Sollang stream has gone beyond simple example syntax, becoming a language feature that can actually pass through variables, functions, and library boundaries.
Comments: 0
Sign in to leave a comment.
No comments yet. Write the first one.