I see in JMH a popular problem with ConstantFold, but what if I have inverse problem. I need the static final field as parameter. For example, it's can be some constant variable for some algorithm. But in java-doc I see: {@link Param} fields should be non-final fields.. I made test for the static parameter (with @Param annotation) and for the static final, and I see, that access to static final is ~1.5 - 2 times more productive.
I found a quick solution in reflection:
private static final int SURROGATE = Runtime.getRuntime().availableProcessors(); //example
private static final Field SURROGATE_FIELD;
private static final String MODIFIERS_FIELD = "modifiers";
private static final ReflectionFactory reflection =
ReflectionFactory.getReflectionFactory();
private static final FieldAccessor SURROGATE_FIELD_ACCESSOR;
static {
try {
SURROGATE_FIELD = ConstantFinalFieldBench.class.getDeclaredField("SURROGATE");
SURROGATE_FIELD.setAccessible(true);
Field modifiersField =
Field.class.getDeclaredField(MODIFIERS_FIELD);
modifiersField.setAccessible(true);
int modifiers = modifiersField.getInt(SURROGATE_FIELD);
modifiers &= ~Modifier.FINAL;
modifiersField.setInt(SURROGATE_FIELD, modifiers);
SURROGATE_FIELD_ACCESSOR = reflection.newFieldAccessor(
SURROGATE_FIELD, false
);
} catch (Exception ex) {
throw new Error(ex);
}
}
@Param({"10"})
private static int paramConst;
@Setup
public void init() throws IllegalAccessException {
SURROGATE_FIELD_ACCESSOR.setInt(null, paramConst);
}
Access to the "SURROGATE" parameter has performance, like to the final field. But maybe I missed something, or do not know, maybe has another method to do it?! Or it will good point to make support it in future.