[ 
https://issues.apache.org/jira/browse/FLINK-37735?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17947578#comment-17947578
 ] 

Zhanghao Chen commented on FLINK-37735:
---------------------------------------

Side note: we also tested with {{{}LambdaMetaFactory{}}}, which is much faster 
according to 
[https://www.optaplanner.org/blog/2018/01/09/JavaReflectionButMuchFaster.html] 
. Indeed it is on mircobenchmarks, but on production env, the difference is 
almost negligible even on our most high-volumn (15Mils record/s) and Pojo-seder 
heavy jobs. On the other hand, {{LambdaMetaFactory}} is quite verbose and more 
difficult to understand for non-JVM experts, so we think it better to use 
{{constructor.newInstance}} here and wait for higher versions of JDK to improve 
its performance transparently later rather trying to optimize it with 
{{LambdaMetaFactory}} on our own.

> Replace class.newInstance with constructor.newInstance
> ------------------------------------------------------
>
>                 Key: FLINK-37735
>                 URL: https://issues.apache.org/jira/browse/FLINK-37735
>             Project: Flink
>          Issue Type: Sub-task
>          Components: API / Type Serialization System
>            Reporter: Zhanghao Chen
>            Priority: Major
>         Attachments: image-2025-04-27-13-05-28-554.png, 
> image-2025-04-27-13-14-18-941.png
>
>
> *Problem*
> Flink's PojoSerializer and TupleSerializer uses {{class.newInstance}} to 
> instiantiate a new instance. The {{class.newInstance}} call will get caller 
> class and do access check on each invocation, which consumes much CPU, as 
> depicted in the flame graph below. It is also marked @Deprecated since JDK9, 
> and is subject to removal in later JDK versions.
> !image-2025-04-27-13-05-28-554.png|width=730,height=137!
> *Proposal*
> The suggested alternative is to use {{{}constructor.newInstance{}}}, which 
> allows bypassing the access check completely after setting accessible to 
> true. It also throws more specific exception on invocation error.
> The performance gets boosted by ~5% on the Flink POJO benchmark (tested on my 
> laptop with JDK17.0.14):
> ​ !image-2025-04-27-13-14-18-941.png!



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to