Skip to content

Reduce logging overhead - #439

Merged
codefromthecrypt merged 1 commit into
OpenFeign:masterfrom
schlosna:feature/logging-improvements
Aug 16, 2016
Merged

Reduce logging overhead#439
codefromthecrypt merged 1 commit into
OpenFeign:masterfrom
schlosna:feature/logging-improvements

Conversation

@schlosna

Copy link
Copy Markdown
Contributor
  • Avoid overhead when logging is disabled.
  • Reuse a single StringBuilder when constructing method tag.

@codefromthecrypt

codefromthecrypt commented Aug 15, 2016 via email

Copy link
Copy Markdown

@schlosna

Copy link
Copy Markdown
Contributor Author

I have a separate local branch I'll push and create a PR later tonight or tomorrow to hook up the benchmarks module and add a small JMH microbenchmark for the StringBuilder, but the main reason I added it was that I saw a bunch of string copies in some profiles since we almost always append an additional format string after generating the method tag format, and sometimes the new line placeholder. There is still another string copy for the String.format, though as already commented in the code we're not doing printf to slf4j message conversion (yet).

@codefromthecrypt

codefromthecrypt commented Aug 16, 2016

Copy link
Copy Markdown

Since I asked for the benchmark, I'll share in the pain :) Here's a succinct way to compare (I think). Feel free to use or toss.

@Benchmark
public String logBasic_concat() {
  return logBasic(concatLogger);
}

@Benchmark
public String logBasic_builder() {
  return logBasic(builderLogger);
}

static String logBasic(Logger logger) {
  return logger.toLog("Github#contributors", "---> %s %s HTTP/1.1", "GET", "https://api.github.com/repos/openfeign/feign");
}

interface Logger {
  String toLog(String configKey, String format, Object... args);
}

static final Logger concatLogger = (configKey, format, args) -> {
 String methodTag = new StringBuilder().append('[').append(configKey.substring(0, configKey.indexOf('('))).append("] ").toString();
 return String.format(methodTag + format, args);
}

static final Logger builderLogger = (configKey, format, args) -> {
 StringBuilder methodTag = new StringBuilder().append('[').append(configKey.substring(0, configKey.indexOf('('))).append("] ").toString();
 return String.format(methodTag.append(format).toString(), args);
}

* Avoid overhead when logging is disabled.
@schlosna
schlosna force-pushed the feature/logging-improvements branch from 4befdd0 to bcc1a47 Compare August 16, 2016 02:29
@schlosna

Copy link
Copy Markdown
Contributor Author

This PR is now just adding guard around logging.

Ran the JMH benchmark https://github.com/schlosna/feign/blob/feature/method-tag-benchmark/benchmark/src/main/java/feign/benchmark/MethodTagBenchmarks.java which shows the existing implementation as faster, so I've reverted that part of this change.

Benchmark                              Mode  Cnt       Score       Error  Units
MethodTagBenchmarks.logBasic_builder  thrpt   15  954826.745 ± 68500.736  ops/s
MethodTagBenchmarks.logBasic_concat   thrpt   15  979587.330 ± 48137.135  ops/s

@codefromthecrypt
codefromthecrypt merged commit 3c23f1d into OpenFeign:master Aug 16, 2016
@codefromthecrypt

Copy link
Copy Markdown

JVM's a mysterious thing, right? :) thanks for the help and the diligence!

@schlosna

Copy link
Copy Markdown
Contributor Author

Thanks for merging. The results were a bit surprising, but good.

There have been a few discussions over the years about moving more of these optimizations into both javac and the JVM itself, see https://bugs.openjdk.java.net/browse/JDK-4059189 and http://markmail.org/message/mrhy6ystcirswhqf , and Java 9's indy string concatenation will likely change the results as well.

@schlosna
schlosna deleted the feature/logging-improvements branch September 10, 2018 10:05
velo pushed a commit that referenced this pull request Oct 7, 2024
* Avoid overhead when logging is disabled.
velo pushed a commit that referenced this pull request Oct 8, 2024
* Avoid overhead when logging is disabled.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants