qos-ch / logback

The reliable, generic, fast and flexible logging framework for Java.
http://logback.qos.ch
Other
2.97k stars 1.28k forks source link

LogbackMDCAdapter Performance #812

Open mosesn opened 3 months ago

mosesn commented 3 months ago

Problem

We've been using LogbackMDCAdapter, but recently found that it's copying around a lot of hashmaps. We're currently trying out LogbackMDCAdapterSimple, and it seems better and we found that it does even more copying.

Question

Why does Logback provide both LogbackMDCAdapterSimple and LogbackMDCAdapter? Would you consider just consolidating on LogbackMDCAdapterSimple?

Proposed Improvement

Since LogbackMDCAdapterSimple uses a thread local, it doesn't seem like it actually needs to use copy-on-write semantics to keep the map safe. What about using a simple ThreadLocal<Map<String, String>> where the map is mutable and we update it directly?

Result

We require fewer intermediate data structures as we assemble MDC.

ceki commented 3 months ago

@mosesn The MDC map is copied into LoggingEvent instances which may be handed to different threads, e.g. AsyncAppender's dispatcher thread.