what is the most performant way to do this: `log("...
# codereview
c
what is the most performant way to do this:
log("going to work in items ${items.map {it.id}}")
maybe a way to append directly to the string buffer instead of creating an array.
t
you have me curious, naively I would go with
joinToString
and
prefix
but it is just a hunch, never measured
c
so something like:
Copy code
items.joinToString(prefix = "going to work on items (", postfix = ")") { it.id.toString() }
I think i want something implemented like the latter but looking like the original example
t
yep, but again, I have no real clue if that is performant or not. seems better than the original and leave space for kotlin and java designer to improve performance in future release with new String and string building optimizations
it looks ugly, agreed
c
I think from the performance point of view its great because it uses only string builder
k
The other thing to note is, irrespective of how optimized your log message is, you should compute it only if you are logging at that level. If you're using slf4j, you can use https://github.com/MicroUtils/kotlin-logging which is a wrapper library that allows you to pass a lambda that is only evaluated if necessary:
Copy code
logger.debug { items.joinToString(prefix = "going to work on items (", postfix = ")") { it.id.toString() } }
If you're using a logging framework that already supports lambdas (such as log4j 2 or java.util.logging) you can use that directly.
c
yes, that was just an example. we do it exactly like you said in, and use microutils logging.