Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When we create a lot of Strings, CoderResult$Cache.get() becomes the bottleneck. #2

Closed
DanielYWoo opened this issue Sep 18, 2017 · 1 comment

Comments

@DanielYWoo
Copy link

I got a lot of this kind of thread dump during a load test:

"catalina-exec-458" #26857 daemon prio=5 os_prio=0 tid=0x00007f56a01ab800 nid=0x2517 waiting for monitor entry [0x00007f53d38f7000]
   java.lang.Thread.State: BLOCKED (on object monitor)
        at java.nio.charset.CoderResult$Cache.get(CoderResult.java:201)
        - waiting to lock <0x00000005c0e89b58> (a java.nio.charset.CoderResult$1)
        at java.nio.charset.CoderResult$Cache.access$200(CoderResult.java:194)
        at java.nio.charset.CoderResult.malformedForLength(CoderResult.java:236)
        at sun.nio.cs.UTF_8$Decoder.malformedN(UTF_8.java:147)
        at sun.nio.cs.UTF_8$Decoder.decode(UTF_8.java:473)
        at java.lang.StringCoding$StringDecoder.decode(StringCoding.java:153)
        at java.lang.StringCoding.decode(StringCoding.java:193)
        at java.lang.StringCoding.decode(StringCoding.java:254)
        at java.lang.String.<init>(String.java:546)
        at java.lang.String.<init>(String.java:566)

The cache has a synchronized method get(int len). Can we optimize this? Replace this with a lock-free cache?


private static abstract class Cache {

        private Map<Integer,WeakReference<CoderResult>> cache = null;

        protected abstract CoderResult create(int len);

        private synchronized CoderResult get(int len) {
            if (len <= 0)
                throw new IllegalArgumentException("Non-positive length");
            Integer k = new Integer(len);
            WeakReference<CoderResult> w;
            CoderResult e = null;
            if (cache == null) {
                cache = new HashMap<Integer,WeakReference<CoderResult>>();
            } else if ((w = cache.get(k)) != null) {
                e = w.get();
            }
            if (e == null) {
                e = create(len);
                cache.put(k, new WeakReference<CoderResult>(e));
            }
            return e;
        }

    }

@gfredericks
Copy link
Contributor

@DanielYWoo I apologize for the confusion, but this repository is only a mirror of the openjdk code, and has no connection to the openjdk maintainers. I don't know how to contact them any better than you do.

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

No branches or pull requests

2 participants