From: John Carter Date: 2001-08-24T08:17:40+09:00 Subject: [ruby-talk:20220] Sigh! I should have seen it coming...A ramble on coroutines, shifts and deques I was playing with doing a very simple recursive descent parser.... A := (token | '[' A ']' ) *; The lexical scanner part was easy and using yield it was trivial to feed a stream of tokens to the parser. Aargg! I couldn't twist the yield semantics into have a recursive consumer... So I tried multithreading... (So I was playing OK!?) So now I had a reasonably simple producer thread that communicated with a consumer thread via a pipe. Hey that was suprisingly painless... The pipe I initially implemented as an Array that the producer "push"ed things onto and the consumer "shift"ed things off within the sanctity of a mutex.synchronize {block}. Not good enough I said, so I implemented a ring buffer Deque on top of an Array. Pretty easy, the Array semantics actually make it pretty easy. Now I wouldn't have the burden of the "shift" on the consumer. Sigh! I expected the Array implementation to be faster for small arrays and the Deque implementation to be faster for large number of elements. What I forgot to add in was that "shift" are probably implemented as very fast memcpy's and my Deque code was implemented in an interpreted language. ie. The cross over point for where the Array implementation is as fast as the Deque implementation is at about 8000 elements. Sigh! I should have seen that coming and not have bothered... Now I must just benchmark my threaded implementation vs that brain twisting callcc implementation of coroutine's.... Hmm.. I probably should package my Threaded coroutine's with the callcc version as they both do the same thing but with different strengths and weaknesses. John Carter Phone : (64)(3) 358 6639 Tait Electronics Fax : (64)(3) 359 4632 PO Box 1645 Christchurch Email : john.carter@tait.co.nz New Zealand I'm becoming less and less convinced of humans as rational beings. I suspect we are merely meme collectors, and the reason meme is only kept on to help count our change.