This is a really small programming āstyleā best practice that came up at work last week.
TLDR; donāt pre-concatenate strings in Java log calls!
Iām working in Java now after a long stint in Scala-land. I used Java in university and at my first few internships but havenāt worked in it since then. So, there are a lot of coding conventions and best practices that Iām having to re-learnā¦
One of them is about logs!
Fun fact, my very first internship project at Google was about calculating the memory usage of log strings in Gmail. Log strings can be expensive. Letās look at some code.
// Version 1 - boo
log.info("Data processed for user " + user.getId());
// Version 2 - yay
log.info("Data processed for user {}", user.getId());
Though they look very similar, Version 1 is more expensive than the Version 2.
Letās say that you have your logging level configured so that only ERROR level logs are actually logged. However, the concatenation in version 1 will happen before the invocation of log.info
. That means that youāre doing extra work on every request to concatenate those stringsā¦and then throw them away! Yuck! If you are processing lots of traffic, all of those extra (useless) string allocations can add up and really impact your newgen memory churn. And worst of all, those strings are unique per user (or message ID, or whatever other variable youāre logging).
In version 2, the userās ID is passed as an argument to the logger. The logger will only build the string if it actually needs it. Yay! Much better.
It feels like a small style nit but these kinds of allocations can have a real impact. Also, logs are a common place to find this kind of wasted work, but the same principle applies generally. āDonāt pre-build unique strings unless you have toā is a good rule of thumb.
Thanks to Sean Wu for discussing this with me in Recurse Center chat!
Related links:
-
DBA Presents: Use the Built in Formatting to Construct This Argument
-
StackOverflow: Built-in string formatting vs string concatenation as logging parameter