[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Algorithm help: passing strings between threads with minimal copying
- From: Tim Hill <drtimhill@...>
- Date: Sat, 5 Apr 2014 10:35:59 -0700
On Apr 5, 2014, at 10:25 AM, Rena <hyperhacker@gmail.com> wrote:
>
> Of course one thread can't touch another thread's Lua state, since a state can only be used by one thread at a time. So we need to store the received messages somewhere until the receiving thread asks for them. No problem, we have a struct for each thread already (holding its lua_State* and other nice things); we can copy the string into a list in that struct, using mutexes to ensure it's not being read and written at the same time.
>
> The issue with that design, though, is excess copying. If thread A sends a message to thread B, that means A has a copy of the string, B's incoming message queue will have another copy, and B's Lua state will make a third copy when lua_pushlstring() is used to receive the message. I'd quite like to eliminate that extra copy.
>
Are you really sure the copying is a significant enough problem to justify the complexity of your solutions? I’ve designed several marshaling systems around Lua, and the “extra hop” really wasn’t significant in terms of performance. Depending on your message volume, you may even find that a fixed pool of buffers is very efficient, as the CPU caches will quickly “warm” and the extra copy overhead will get even smaller.
—Tim