Why Are Annotations in Java so Slow?

Annotations in Java seem extremely slow, especially since they are essentially fixed values. I'm not even talking about looking up annotations, but just querying the values after you already have an annotation object. Calling value() takes comparatively forever. In a recent project, I created what I call "mirror" classes which are just exactly like the annotations but with fields that are simply copied from the annotation, and the speed boost was phenomenal. But that leads to the question, what is the annotation actually doing that takes so much time? A very simple setup shows that it takes 3-11x time to call value() on an annotation than a regular function. It's faster on Java 17 than Java 8, but I still don't know what is taking so much time. Anyone know? Or is there a way to speed it up? Example Code: @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface MyAnnotation { public int value() default 0; } public class MyAnnotationStruct { public int i = 5; public int value() { return i; } } @MyAnnotation(value = 5) public class Main { public static void main(String[] argv) { MyAnnotation ann = Main.class.getAnnotation(MyAnnotation.class); MyAnnotationStruct astr = new MyAnnotationStruct(); long sum = 0; for(long i = 0; i < 1000000000L; i++) { sum = sum + i + ann.value(); // Swap with astr.value() for comparison } System.out.println("result: " + sum); } } On Java 8, the struct version takes 1.0 seconds and the annotation version takes 7.2 seconds.

May 20, 2025 - 10:50
 0

Annotations in Java seem extremely slow, especially since they are essentially fixed values. I'm not even talking about looking up annotations, but just querying the values after you already have an annotation object. Calling value() takes comparatively forever.

In a recent project, I created what I call "mirror" classes which are just exactly like the annotations but with fields that are simply copied from the annotation, and the speed boost was phenomenal. But that leads to the question, what is the annotation actually doing that takes so much time?

A very simple setup shows that it takes 3-11x time to call value() on an annotation than a regular function. It's faster on Java 17 than Java 8, but I still don't know what is taking so much time. Anyone know? Or is there a way to speed it up?

Example Code:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface MyAnnotation {
        public int value() default 0;
}

public class MyAnnotationStruct {
        public int i = 5;
        public int value() {
                return i;
        }
}

@MyAnnotation(value = 5)
public class Main {
        public static void main(String[] argv) {
                MyAnnotation ann = Main.class.getAnnotation(MyAnnotation.class);
                MyAnnotationStruct astr = new MyAnnotationStruct();
                long sum = 0;
                for(long i = 0; i < 1000000000L; i++) {
                        sum = sum + i + ann.value(); // Swap with astr.value() for comparison

                }
                System.out.println("result: " + sum);
        }
}

On Java 8, the struct version takes 1.0 seconds and the annotation version takes 7.2 seconds.