Although rarely used, Enum.values() is a function that returns an array holding all the possible values of the specified Enum. It generates a new array every time it’s called! Since there’s no way to force the immutability of the array, I guess that was their only way of doing this.
I just eliminated 200+MB/s of garbage by writing my own cached values() function which returns a precomputed static array of the values in the enum.
private static EnumName[] values = values();
public static EnumName[] valuesCached(){
return values;
}
EDIT: Modified the code example to look more sane with syntax highlighting.