【问题标题】:Akka with Frege running slower than Scala counterpartAkka 与 Frege 的运行速度比 Scala 慢
【发布时间】:2013-06-23 01:40:48
【问题描述】:

作为练习,我将这些 ScalaJava 的 Akka 示例移植到 Frege。虽然它运行良好,但运行速度比 Scala(540ms) 慢(11​​ 秒)。

module mmhelloworld.akkatutorialfregecore.Pi where
import mmhelloworld.akkatutorialfregecore.Akka

data PiMessage = Calculate | 
                Work {start :: Int, nrOfElements :: Int} |
                Result {value :: Double} | 
                PiApproximation {pi :: Double, duration :: Duration}

data Worker = private Worker where
    calculatePiFor :: Int -> Int -> Double
    calculatePiFor !start !nrOfElements = loop start nrOfElements 0.0 f where
        loop !curr !n !acc f = if n == 0 then acc
                               else loop (curr + 1) (n - 1) (f acc curr) f
        f !acc !i = acc + (4.0 * fromInt (1 - (i `mod` 2) * 2) / fromInt (2 * i + 1))

    onReceive :: Mutable s UntypedActor -> PiMessage -> ST s ()
    onReceive actor Work{start=start, nrOfElements=nrOfElements} = do
        sender <- actor.sender
        self <- actor.getSelf
        sender.tellSender (Result $ calculatePiFor start nrOfElements) self 

data Master = private Master {
    nrOfWorkers :: Int,
    nrOfMessages :: Int,
    nrOfElements :: Int,
    listener :: MutableIO ActorRef,
    pi :: Double,
    nrOfResults :: Int,
    workerRouter :: MutableIO ActorRef,
    start :: Long } where

    initMaster :: Int -> Int -> Int -> MutableIO ActorRef -> MutableIO UntypedActor -> IO Master
    initMaster nrOfWorkers nrOfMessages nrOfElements listener actor = do
        props <- Props.forUntypedActor Worker.onReceive
        router <- RoundRobinRouter.new nrOfWorkers
        context <- actor.getContext
        workerRouter <- props.withRouter router >>= (\p -> context.actorOf p "workerRouter")
        now <- currentTimeMillis ()
        return $ Master nrOfWorkers nrOfMessages nrOfElements listener 0.0 0 workerRouter now

    onReceive :: MutableIO UntypedActor -> Master -> PiMessage -> IO Master
    onReceive actor master Calculate = do
        self <- actor.getSelf
        let tellWorker start = master.workerRouter.tellSender (work start) self
            work start = Work (start * master.nrOfElements) master.nrOfElements
        forM_ [0 .. master.nrOfMessages - 1] tellWorker
        return master
    onReceive actor master (Result newPi) = do
        let (!newNrOfResults, !pi) = (master.nrOfResults + 1, master.pi + newPi)
        when (newNrOfResults == master.nrOfMessages) $ do
            self <- actor.getSelf
            now <- currentTimeMillis ()
            duration <- Duration.create (now - master.start) TimeUnit.milliseconds
            master.listener.tellSender (PiApproximation pi duration) self
            actor.getContext >>= (\context -> context.stop self)
        return master.{pi=pi, nrOfResults=newNrOfResults}

data Listener = private Listener where
    onReceive :: MutableIO UntypedActor -> PiMessage -> IO ()
    onReceive actor (PiApproximation pi duration) = do
        println $ "Pi approximation: " ++ show pi
        println $ "Calculation time: " ++ duration.toString
        actor.getContext >>= ActorContext.system >>= ActorSystem.shutdown

calculate nrOfWorkers nrOfElements nrOfMessages = do
    system <- ActorSystem.create "PiSystem"
    listener <- Props.forUntypedActor Listener.onReceive >>= flip system.actorOf "listener"
    let constructor = Master.initMaster nrOfWorkers nrOfMessages nrOfElements listener
        newMaster = StatefulUntypedActor.new constructor Master.onReceive
    factory <- UntypedActorFactory.new newMaster
    masterActor <- Props.fromUntypedFactory factory >>= flip system.actorOf "master"
    masterActor.tell Calculate
    getLine >> return () --Not to exit until done

main _ = calculate 4 10000 10000

是我对 Akka 做错了什么,还是与 Frege 的懒惰有关?例如,当我最初在 Worker.calculatePiFor 中使用 fold(strict fold) 代替 loop 时,需要 27 秒。

依赖关系:

  1. 弗雷格的 Akka 原生定义:Akka.fr
  2. 用于扩展 Akka 类的 Java 助手,因为我们无法在 弗雷格:Actors.java

【问题讨论】:

    标签: scala akka frege


    【解决方案1】:

    我对 Actor 并不十分熟悉,但假设最紧密的循环确实是 loop,您可以避免将函数 f 作为参数传递。

    一方面,传递函数的应用程序不能利用实际传递函数的严格性。相反,代码生成必须保守地假设传递的函数懒惰地接受它的参数并返回一个懒惰的结果。

    其次,在我们的例子中,你在这里只使用了一次f,所以可以内联它。 (这就是您链接的文章中的scala代码中的完成方式。)

    在以下模仿您的示例代码中查看为尾递归生成的代码:

    test b c = loop 100 0 f 
       where 
          loop 0 !acc f = acc
          loop n !acc f = loop (n-1) (acc + f (acc-1) (acc+1)) f   -- tail recursion
          f x y = 2*x + 7*y
    

    我们到达那里:

    // arg2$f is the accumulator
    arg$2 = arg$2f + (int)frege.runtime.Delayed.<java.lang.Integer>forced(
          f_3237.apply(PreludeBase.INum_Int._minusƒ.apply(arg$2f, 1)).apply(
                PreludeBase.INum_Int._plusƒ.apply(arg$2f, 1)
              ).result()
        );    
    

    您在这里看到f 被延迟调用,这导致所有参数表达式也被延迟计算。请注意这需要的方法调用次数! 在您的情况下,代码仍应类似于:

    (double)Delayed.<Double>forced(f.apply(acc).apply(curr).result())
    

    这意味着,使用装箱值 acc 和 curr 构建两个闭包,然后计算结果,即使用未装箱的参数调用函数 f,结果再次装箱,只是为了再次装箱(强制)用于下一个循环。

    现在比较以下,我们只是不传递f,而是直接调用它:

    test b c = loop 100 0 
        where 
          loop 0 !acc = acc
          loop n !acc = loop (n-1) (acc + f (acc-1) (acc+1)) 
          f x y = 2*x + 7*y
    

    我们得到:

    arg$2 = arg$2f + f(arg$2f - 1, arg$2f + 1);
    

    好多了! 最后,在上述情况下,我们可以完全不调用函数:

          loop n !acc = loop (n-1) (acc + f) where
            f = 2*x + 7*y
            x = acc-1
            y = acc+1
    

    这得到:

    final int y_3236 = arg$2f + 1;
    final int x_3235 = arg$2f - 1;
    ...
    arg$2 = arg$2f + ((2 * x_3235) + (7 * y_3236));
    

    请尝试一下,让我们知道会发生什么。性能的主要提升应该来自不通过f,而内联可能会在 JIT 中完成。

    fold 的额外费用可能是因为您还必须在应用之前创建一些列表。

    【讨论】:

    • 太棒了!现在下降到1.3s。我查看了生成的 Java 源代码。它现在退化为while 循环。
    猜你喜欢
    • 2012-12-25
    • 2019-04-23
    • 1970-01-01
    • 2011-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多