tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Konstantin Kolinko <knst.koli...@gmail.com>
Subject Re: svn commit: r1389076 - in /tomcat/trunk: java/org/apache/tomcat/util/collections/ test/org/apache/tomcat/util/collections/
Date Sun, 23 Sep 2012 19:03:27 GMT
2012/9/23  <markt@apache.org>:
> Author: markt
> Date: Sun Sep 23 15:20:38 2012
> New Revision: 1389076
>
> URL: http://svn.apache.org/viewvc?rev=1389076&view=rev
> Log:
> ConcurrentLinkedQueue is currently the biggest contributor to garbage in the load test
so this is intended as a replacement.
>
> Added:
>     tomcat/trunk/java/org/apache/tomcat/util/collections/ConcurrentStack.java   (with
props)
>     tomcat/trunk/test/org/apache/tomcat/util/collections/
>     tomcat/trunk/test/org/apache/tomcat/util/collections/TestConcurrentStack.java   (with
props)
>     tomcat/trunk/test/org/apache/tomcat/util/collections/TesterPerformanceConcurrentStack.java
  (with props)
>
> Added: tomcat/trunk/java/org/apache/tomcat/util/collections/ConcurrentStack.java
> URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/collections/ConcurrentStack.java?rev=1389076&view=auto

> +package org.apache.tomcat.util.collections;
> +
> +/**
> + * This is intended as a (mostly) GC-free alternative to
> + * {@link java.util.concurrent.LinkedBlockingDeque} where the requirement is to
> + * create a pool of re-usable objects with no requirement to shrink the pool.
> + * The aim is to provide the bare minimum of required functionality as quickly
> + * as possible with minimum garbage.
> + */
> +public class ConcurrentStack<T> {
> +
> +    private int size = 128;
> +    /*
> +     * Points to the next available object in the stack
> +     */
> +    private int index = -1;
> +
> +    private Object[] stack = new Object[size];
> +
> +    public synchronized void push(T obj) {
> +        index++;
> +        if (index == size) {
> +            expand();
> +        }
> +        stack[index] = obj;
> +    }
> +
> +    @SuppressWarnings("unchecked")
> +    public synchronized T pop() {
> +        if (index == -1) {
> +            return null;
> +        }
> +        return (T) stack[index--];
> +    }
> +
> +    private void expand() {
> +        int newSize = size * 2;
> +        Object[] newStack = new Object[newSize];
> +        System.arraycopy(stack, 0, newStack, 0, size);
> +        // This is the only point where garbage is created by throwing away the
> +        // old array. Note it is only the array, not the contents, that becomes
> +        // garbage.
> +        stack = newStack;
> +        size = newSize;
> +    }
> +}
>


Mismatching name. It is rather a "SynchronizedStack".

Wouldn't its synchronized methods be a bottleneck? (That is what
ConcurrentLinkedQueue is designed to avoid).


The pop() method should assign null value to the slot that it freed.

(It is not important if the object is going to be re-added to the
cache.  It would be noticeable if the object would be discarded, e.g.
a CharBuffer that grew too big so that we do not want to reuse it. It
would not be GC'd before the slot value is overwritten).

Best regards,
Konstantin Kolinko

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


Mime
View raw message