• Feeds

  • Lua coroutine 不一样的多线程编程思路

    上周末开始看《Lua程序设计》第二版,目前体会到其中比较有趣的有两点,一是强大的table数据结构,另外就是coroutine。也许Lua中的coroutine是一种很好的设计模式,但我初步的体会还是没想到其他语言和场合能非常适合用到coroutine的场景。

    一、简介

    协同程序与线程差不多,也就是一条执行序列,拥有自己独立的栈,局部变量和指令指针,同时又与其它协同程序共享全局变量和其它大部分东西。线程与协同程序的主要区别在于,一个具有多线程的程序可以同时运行几个线程,而协同程序却需要彼此协作地运行。就是说,一个具有多个协同程序的程序在任何时刻只能运行一个协同程序,并且正在运行的协同程序只会在其显示地挂起时,它的执行才会暂停。

    如:

    co = coroutine.create(function ()
    for i=1,10 do
    print("co", i)
    coroutine.yield()
    end
    end)

    从主线程调用
    coroutine.resume(co)
    会依次打印1到10

    二、原理探析

    • coroutine创建的所谓的“线程”都不是真正的操作系统的线程,实际上是通过保存stack状态来模拟的。
    • 由于是假的线程,所以切换线程的开销极小,同时创建线程也是轻量级的,new_thread只是在内存新建了一个stack用于存放新coroutine的变量,也称作lua_State

    LUA_API lua_State *lua_newthread (lua_State *L)

    • 调用yield()当前线程交出控制权,同时还可以通过stack返回参数。调用resume的线程(可理解为主线程)获得返回的参数。
    • Lua yield()和Java中的Thread.yield()有点相似,但是区别更大。Java中的yield调用后只是将当前CPU切换到另外一个线程,CPU可能随时会继续回到线程执行。
    • 我更倾向于把Lua中的yield()和resume()和Java中的wait()和notify()来对比。它们表现的行为基本一致。
    • 关于stack实现也可参看Yufeng(Erlang高手)的分析文章 lua coroutine是如何实现的?

    三、Why coroutine?

    上面对coroutine有个基本的了解,因此大家都会象我一样去想,为什么要用coroutine?先研究下优点

    • 每个coroutine有自己私有的stack及局部变量。
    • 同一时间只有一个coroutine在执行,无需对全局变量加锁。
    • 顺序可控,完全由程序控制执行的顺序。而通常的多线程一旦启动,它的运行时序是没法预测的,因此通常会给测试所有的情况带来困难。所以能用coroutine解决的场合应当优先使用coroutine。

    再看缺点,研究coroutine缺点之前,我寻找了一下Lua中为什么实现coroutine的一些说明。在巴西人写的paper Coroutines in Lua(pdf)中解释了几个原因:

    • Lua是ANSI C实现的,ANSI C并不包含thread的实现,因此如果要在Lua增加thread的支持就要使用操作系统本地的实现,这样会造成通用的问题。同时也会使Lua变得臃肿。因此Lua选择了在ANSI C上实现的coroutine。
    • Lua主要设计目的之一是给C调用,如果Lua内部又有多线程实现的话会造成C调用状态的混乱,而只提供coroutine层面的挂起则可以保持状态的一致性。

    以上这些理由都是基于Lua特殊的原因而使用的,并不是很通用的原因。我们也了解到,coroutine实际上是一种古老的设计模式,它在60年代就已经定型,但是现代语言很少有重视这个特性,目前可以举例的有Windows的fibers, Python的generators

    四、Lua coroutine和Erlang

    上面优点有1条没展开,就是每个coroutine有自己私有的stack及内存变量空间。因此可以认为coroutine和Erlang中的process是非常相似的。但是coroutine只能同时只有一个在执行,如果能让他多个同时跑,我觉得就和Erlang非常相似了。

    《Lua程序设计》第二版30.2介绍的一种实现方法,让多个c threads启动,然后每个c thread启动一个coroutine(类似Erlang process),然后通过stack传递变量值(类似Erlang process message),这样就可以实现一个类似Erlang的process模型了。由于coroutine实际上可以用任何语言实现,那其他语言应该也可实现同样这种设计方法。

    五、Lua其他

    Lua目前主要用在游戏编程领域,通常的观点Lua是“胶水语言”。用来把各个模块化的功能粘合起来。就我目前阅读的一些代码来看,C和Lua通常是混合在一起的,并没有明确的边界。对于我一个外行的眼光看来我分不清哪些是在做C的事情,哪些是在调用Lua。特别是这个“胶水”如果放得太多,系统中各个模块的独立性将会受到影响。比如云风的这篇Lua 不是 C++也提到,“这属于过厚的粘合层,是绝对需要抛弃的”。

    另外Code@Pig一篇[网游设计] 一点感想也提到要简化调用,我总结它的观点主要两点:

    1. 不要存在冗余的关系,给一个部分负责管理就好。(由Lua/python来管理)
    2. 粘合层(Lua/python接口)不要过胖,我们可以通过引入一个“间接层”来把粘合层做“薄”

    虽然Lua的高效和精简的设计让人赞誉有加,但是它的性能排名并不高,和Python大致在同一个级别。另外“胶水语言”的定位也妨碍了它在更多领域的发展。

    Thrift and Protocol Buffers performance in Java Round 2

    In my last test Thrift and Protocol Buffers performance in Java, Some comments told me that there are some tuning parameter for Protocol Buffer which can improve performance magically. The parameter was not turn on by default. I added
    option optimize_for = SPEED
    to the proto file, and re-generated the Java class, and the result:

    Thrift Loop    : 10,000,000
    Get object     : 14,394msec
    Serdes thrift  : 37,671msec
    Objs per second: 265,456
    Total bytes    : 1,130,000,000
    
    ProtoBuf Loop  : 10,000,000
    Get object     : 8,170msec
    Serdes protobuf: 33,054msec
    Objs per second: 302,535
    Total bytes    : 829,997,866
    

    From the result, Protocol Buffers is 1.1 times faster than Thrift!

    And from the Google Protocol Buffers group, why the optimize for speed was not turn on by default.

    When using C++ or Java protocol buffers, for best performance you need to add a line to your .proto files:

    option optimize_for = SPEED;

    Otherwise, by default, the compiler optimizes for code size.  Optimizing for code size results in generated code that around a half to a third of the size, but runs an order of magnitude slower…

    Here is the original post

    Ideas for creating a friendfeed like feed aggregator system

    I was invited to join a meeting about implement a friendfeed like feed system. Here are some ideas about requirement and architecture, which I typed on my BlackBerry during the meeting.

    1. Like the friendfeed, The product can import external RSS, so we separate the system into two parts. The rss crawl system and the feed pubsub system. The pubsub system has no responsbility to grab the feed from source. The feed itself only save feed summary and url.
    2. We decided to use the INBOX approach, which will push the published feed to be saved in all subscriber’s data table. More information of how this work can refer to Scaling a Microblogging Service – Part I.
    3. User’s homepage is an aggregation result. We choose to return a limited recent real-time date, no infinity pagination. But the user’s own feed(user’s profile page) may have a bigger date range.
    4. The unsubscibe logic have two choice, delete or keep the history data from one’s inbox. We decide to keep them.
    5. If the feed source had been deleted, do we need to delete all references in all subscriber’s inbox?  If need delete, each feed push to the pubsub system need to have a unique resource id. Another problem is after the source updated whether to publish a new feed or update the current feed?
    6. How to manage the group(QUN in Chinese) feed, deliver to all member’s inbox? Or share a group inbox?
    7. How to impl the feed comment logic, publish the comment to feed system or design a standalone comment system. We prefer to use a standalone comment system which doesn’t publish the comment back to the feed system.
    8. Every feed has a media type, such as text, video, image so the subscribe API can only retrieve a limited media type (text for mobile device). And a feed may have tags.
    9. The read/unread count is easy to implement. But the load is heavy. (QQ / QQzone may has such logic.)
    10. Need open API for 3rd party client(like twitter client), and RSS feed, may have OAuth integration.
    11. The storage may like friendfeed’s mysql schema (see How FriendFeed uses MySQL to store schema-less data) or use Amazon simpledb.
    12. May add support for XMPP Publish-Subscribe, or PEP(Personal Eventing Protocol) for pushing realtime time to users in the future.