Search code examples
rubyperformancesorbet

Will using Sorbet's ruby type checker have an impact on a ruby app's performance?


Maybe a newb's question but if you never ask youll never know

Will using Stripe's Sorbet (https://sorbet.org/) on a RoR app, can potentially improve the app's performance? (performance meaning response times, not robustness \ runtime error rate)

I did some reading on dynamically typed languages (particularly Javascript in this case) and found out that if we keep sending some function (foo for example) the same type of objects, the engine does some optimising work on that function, so that when it is invoked again with the same types, there interpreting work would be quicker.

I thought maybe ruby interpreter does a similar work which can potentially mean that type-checking may increase interpreting speed


Solution

  • I thought maybe ruby interpreter does a similar work which can potentially mean that type-checking may increase interpreting speed

    It doesn't yet, but one could potentially build this one day. Goal of Sorbet was to build a type system for people, compared to building a type system for computers(compiler). It can introduce some performance overhead, but as Stripe runs it in production, we keep it in check. Internally, we page us if overhead is >7% of cpu time.

    I did some reading on dynamically typed languages (particularly Javascript in this case) and found out that if we keep sending some function (foo for example) the same type of objects, the engine does some optimising work on that function, so that when it is invoked again with the same types, there interpreting work would be quicker.

    Yes, this can be done. What you're describing is a common optimization in Just-In-Time(JIT) compilers. The technique that you seem to refer to uses run time profiling and actually is a common alternative technique that allows to achieve this result in absence of type system. It's also worth noting that well-build JITs can do it more frequently than a type system, as type system encodes what could happen, while profiling & JITs can optimize for what actually happens in practice.

    That said, building a JIT is frequently much more work than building an online compiler, thus, depending on amount of investment one wants to put into speeding up Ruby, either using building a JIT or using types can prove better under different real-world constrains.

    I thought maybe ruby interpreter does a similar work which can potentially mean that type-checking may increase interpreting speed

    Summarizing the previous paragraph, Sorbet typesystem does not currently speedup Ruby, but it doesn't slow it down much either. Type systems could be indeed used to speed up languages, but they aren't your only tool, with profiling & JIT compilation being the major competitor.