I implemented this iter.Pull strategy for keeping parser state, instead of
using a background goroutine (as I did for years) in my pure Go lisp, 
zygomys, 
and the resulting code is much easier to read and follow, and about 12% 
faster on 
Linux (although 0.3% slower on Darwin).

Details and specific code here:

https://github.com/glycerine/zygomys?tab=readme-ov-file#v9-release-notes-using-iterator-instead-of-a-goroutine-for-parser-state

v8.1.0 has the old goroutine version, v9.0.7 has the new iterator pull 
(implicit coroutine, so
no linkname hacking required) version, if you want to compare code 
side-by-side. 

See the zygo/parser.go file specifically to compare the read-ability.
It might look a little strange, because you store the iterator, the 
yield(), next(), and stop() func
as members in the Parser state, but it works, and works well. 

I'm much happier re-using my library for things like JSON or config parsing 
without having to worry about
the extra background goroutine in my stack dumps, which was always a bit 
surprising.

- Jason


On Monday, November 18, 2024 at 10:22:10 PM UTC roger peppe wrote:

> On Mon, 18 Nov 2024 at 18:02, Romain Doumenc <rom...@trout.software> wrote:
>
>> Hi Roger,
>>
>> Glad you found this interesting, and thank you for reading !
>>
>> One can obviously always use methods on a structure instead of
>> coroutines (in the example I provided, the state could be stored in
>> the tokenizer), and the question is when storing the state on the Go
>> stack directly makes code more readable
>
>
> That's not quite what I meant. I was suggesting exactly storing the state
> on the Go stack, just as you're doing with your coroutines, but instead
> of using the internal Go API, ISTM that it should be possible to use
> iter.Pull <https://pkg.go.dev/iter#Pull>, which uses coroutines under the 
> hood, instead.
> Here is a sketch of the idea: https://go.dev/play/p/pFHvaSgOn9c
>  
>   cheers,
>     rog.
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion visit 
https://groups.google.com/d/msgid/golang-nuts/e79975fe-6839-4d4d-8e52-f26748c1bdbcn%40googlegroups.com.

Reply via email to