Performance in scala call-by-value journal libraries vs call-by-name

I have been looking at various scala logs lately, and the vast majority of them implement their logging functions as

def debug(s: => String) 

So, if you turned off debug logging, it will not execute the statement. However, I just came across logula , which specifically points to one of the advantages

Unlike many scala logs, Logula does not use a pass-by-name (e.g. f: => A) for its logging statements, which means two things:

  • The scala compiler does not need to create one-time closures for each registration statement. This should reduce the amount of garbage collection pressure.

Which actually has a common meaning for me. So my question is: are there any real performance indicators / data comparing the two approaches? Ideally, something from a live project compared to far-fetched benchmarks?

+8
performance scala logging
source share
2 answers

It only depends on the use cases faster. If you register static strings, it’s faster to just pass this constant and ignore it. Otherwise, if you create strings, you must create at least one object. The function objects are tiny and cheap β€” you'd better create one of them, except for the line, if you ignore it.

Personally, I believe that such an understanding of the principles of first-level compromises is even more valuable than a specific study of a particular application, which may have used one or the other, because it allows you to understand why you chose one or the other (and you still always want to compare your attachment).

(Note: how expensive the creation of the object depends on how much the garbage collector affects, as a rule, short objects can be created and deleted at a speed of the order of 10 8 per second, which should not cause concern, except for hard internal loops. in strict inner loops, I think something is wrong. Instead, you should write unit tests for this.)

+8
source share

I will go to a limb and say that philosophical discussions about compromises are more useful when there is some interesting compromise that can be said, and not here.

 class A { var debugging = true @inline final def debug(msg: => String) = if (debugging) println(msg) def f = { debug("I'm debugging!") ; 5 } } % scalac292 -optimise a.scala % ls -l *.class -rw-r--r-- 1 paulp staff 1503 Jul 31 22:40 A.class % 

Count closure objects.

+3
source share

All Articles