package com.thoughtworks.xstream.core.util;

public final class FastStack {

    private Object[] stack;
    private int pointer;

    public FastStack(int initialCapacity) {
        stack = new Object[initialCapacity];
    }

    public Object push(Object value) {
        if (pointer + 1 >= stack.length) {
            resizeStack(stack.length * 2);
        }
        stack[pointer++] = value;
        return value;
    }

    public void popSilently() {
        pointer--;
    }

    public Object pop() {
        return stack[--pointer];
    }

    public Object peek() {
        return pointer == 0 ? null : stack[pointer - 1];
    }

    public int size() {
        return pointer;
    }

    public boolean hasStuff() {
        return pointer > 0;
    }

    public Object get(int i) {
        return stack[i];
    }

    private void resizeStack(int newCapacity) {
        Object[] newStack = new Object[newCapacity];
        System.arraycopy(stack, 0, newStack, 0, Math.min(stack.length, newCapacity));
        stack = newStack;
    }

    public String toString() {
        StringBuffer result = new StringBuffer("[");
        for (int i = 0; i < pointer; i++) {
            if (i > 0) {
                result.append(", ");
            }
            result.append(stack[i]);
        }
        result.append(']');
        return result.toString();
    }
}

@@ -462,17 +462,31 @@
     
-pointer--
+stack%5B--pointer%5D = null
 ;%0A  
@@ -528,31 +528,102 @@
     
-return stack%5B--pointer%5D
+final Object result = stack%5B--pointer%5D; %0A        stack%5Bpointer%5D = null; %0A        return result
 ;%0A  

Drop unnecessary reference to popped elements to allow finalization through GC (XSTR-264).