cognitect / transit-java

transit-format implementation for Java
Apache License 2.0
62 stars 21 forks source link

Locking contention on com.cognitect.transit.impl.Cache #38

Open wilkerlucio opened 5 months ago

wilkerlucio commented 5 months ago

Hello,

During a performance tuning task, we notice a high locking time on com.cognitect.transit.impl.Cache during service startup.

cleanshot_2024-06-26_at_09 56 52_2x

I tracked it down to this code snippet: https://github.com/cognitect/transit-java/blob/8c29b1c0984a8d0bbe5bc1a5375216a4ff0a37ac/src/main/java/com/cognitect/transit/impl/WriterFactory.java#L27-L33

Without changing the library itself, I wrapped our write handlers here with a WriteHandlerMap class to avoid that block, getting into this code path instead: https://github.com/cognitect/transit-java/blob/8c29b1c0984a8d0bbe5bc1a5375216a4ff0a37ac/src/main/java/com/cognitect/transit/impl/WriterFactory.java#L23-L24

Doing so significantly improved our locking times; in comparable scenarios, it dropped from 53 minutes of locking time on com.cognitect.transit.impl.Cache to 1 minute and 30 seconds.

I think if we can improve this locking block, maybe replacing it with a ConcurrentHashMap or something else that avoids global locking, we may be able to improve transit performance in high concurrency scenarios.