r/java • • Jun 20 '26

Populating a POJO with Reflection vs. with the ClassFile API - small benchmark

I wanted to learn about the ClassFile API, and as a small pet project, I created a benchmark to compare how faster a generated class is than a reflection-based approach when we want to populate a POJO from a Map.

TL;DR: it turned out, the generated bytecode was 5x faster than using reflection.

Details here, enjoy: https://github.com/erosb/learn-classfile-api

And please prove me wrong if you find any mistakes in the benchmark :)

62 Upvotes

28 comments sorted by

View all comments

Show parent comment

1

u/[deleted] Jun 22 '26

[removed] — view removed comment

2

u/uniVocity Jun 22 '26

Simple wrappers help a bit here - eg a custom ‘PropertyAccessor’ which will end up looking like
an AtomicReference but using opaque read/write.

I managed to use that successfully and the JVM seemed to be able to inline everything.

1

u/benevanstech Jun 26 '26

The static restriction will go away pretty soon.