mkarneim / pojobuilder

A Java Code Generator for Pojo Builders
Other
334 stars 44 forks source link

Improve performance by removing 'self' field #163

Open drekbour opened 5 years ago

drekbour commented 5 years ago

I tested this using the JMH test cases return this; instead of the default return self; and saw very notable increase in performance (and decrease in heap allocation). I think the JVM doesn't know self will point to itself so reserves enough space for a second full builder object.

1 Use of self should be constrained to withGenerationGap. 2 Use of self vs casting should be compared (not done here)

PojobuilderPerformance.constructManually                                                 thrpt            243.662           ops/s
PojobuilderPerformance.constructViaBuilder_private                                       thrpt            145.929           ops/s
PojobuilderPerformance.constructViaBuilder_shared                                        thrpt            131.137           ops/s
PojobuilderPerformance.constructViaBuilder_threadlocal                                   thrpt            153.278           ops/s
PojobuilderPerformance.constructViaThisBuilder_private                                   thrpt            226.686           ops/s
PojobuilderPerformance.constructViaThisBuilder_shared                                    thrpt            141.562           ops/s
drekbour commented 5 years ago

Results of a Generation Gap builder using return (BookBuilder2)this; instead of return self are similarly enhanced:

PojobuilderPerformance.constructViaBuilder_private                                       thrpt           143.334           ops/s
PojobuilderPerformance.constructViaBuilder_private:cycles:u                              thrpt       7328416.600            #/op
PojobuilderPerformance.constructViaBuilder_private:instructions:u                        thrpt       9539486.380            #/op
PojobuilderPerformance.constructViaBuilder_private:·gc.alloc.rate.norm                   thrpt       8005231.096            B/op
PojobuilderPerformance.constructViaThisBuilder_private                                   thrpt           244.758           ops/s
PojobuilderPerformance.constructViaThisBuilder_private:cycles:u                          thrpt       4605288.336            #/op
PojobuilderPerformance.constructViaThisBuilder_private:instructions:u                    thrpt       6696290.695            #/op
PojobuilderPerformance.constructViaThisBuilder_private:·gc.alloc.rate.norm               thrpt       3200551.912            B/op

In fact it reaches the same speed as manual construction which I cannot really explain but sure is interesting!