douping6871 2015-01-28 07:21
浏览 10
已采纳

单令牌提前查询的性能损失是多少?

When comparing Go and Scala end of statement detection I found out that the rules for Scala are richer, namely:

A line ending is treated as a semicolon unless one of the following conditions is true:

  • The line in question ends in a word that would not be legal as the end of a statement, such as a period or an infix operator.
  • The next line begins with a word that cannot start a statement.
  • The line ends while inside parentheses (...) or brackets [...], because these cannot contain multiple statements anyway.

Quoted from Scala - The rules of semicolon inference.

Rule #1 is how the Go works as well. Rule #3 too. The only difference is rule #2 – it involves single lookahead, since there is one token involved ("word").

What kind of performance penalty is involved: 1% slower, 5%, 10%?

I would love to see a comment (not the question) why Go designers left out that rule – if not for performance, it makes language more reliable, for example in method chaining:

x = some_object.select(...)
               .sort(...)
               .reverse(...)
               .where(...)
               .single()

If I am not mistaken for Go it is an error (you can solve it in two possible ways – taking entire statement in braces or expression in parentheses, but it is manual tweaking), Scala will take it as it should.

  • 写回答

2条回答 默认 最新

  • doubu4406 2015-01-28 08:05
    关注

    The performance penalty is utterly negligible compared to everything else that the compiler has to do. The Scala-internals mailing list has the following exchange between Haoyi Li and Martin Odersky regarding a parboiled2 parser Haoyi wrote for Scala:

    Haoyi Li: In terms of perf[ormance], it can parse everything in scala/scala, lift, scalaz, scalajs, playframework and shapeless in 15 seconds.... Does anyone know how much of the time in the compiler and macros is spent parsing? My impression is that the vast vast vast majority of the time is spent in the typechecker.

    Odersky: Yes, parsing is pretty insignificant compared to the other tasks of a compiler ... That said, the [parser for the next-generation Scala compiler] (hand-written, 2100 lines including error reporting, accurate positions and tree construction) achieves several hundred thousand lines a second. So parboiled still has some way to go to beat that :-)

    When we're talking about hundreds of thousands of lines of code parsed per second including rule #2, one can infer that speed is not the issue. Go compilation tends to clock in at around 20k lines per second, so even if Go parsing took zero time, and the entire time for Scala parsing was taken up by the one-line lookahead, it would be less than 10% of a penalty to the build process.

    In reality it should be more like 0%. Lookahead is usually really cheap; you've already got a token stream, so you just look at the next one.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 有偿求码,CNN+LSTM实现单通道脑电信号EEG的睡眠分期评估
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路