Performance optimization in AbstractBeanFactoryBasedTargetSource.hashCode() #30576
Labels
in: core
Issues in core modules (aop, beans, core, context, expression)
status: backported
An issue that has been backported to maintenance branches
type: enhancement
A general enhancement
Milestone
Affects: 5.2.20
Our production application shows 2.8% of spent cpu coming from the following stack trace:
Reviewing the code for
AbstractbeanFactoryBasedTargetSource.hashCode()
, we see:this.targetBeanName
is aString
, and therefore never routes toObject.hashCode()
. That leavesthis.beanFactory
as the only possible code path, and indeed there is nohashCode()
defined on the interface or any bundled implementation.Modifying our custom bean factory (extends
DefaultListableBeanFactoryWithPublicMutex
) with the following methods solves the problem:I have two suggestions for how to solve this within the framework:
AbstractBeanFactoryBasedTargetSource.hashCode()
to not consider the bean factory. This is okay because 1) bean factory cardinality is extremely low and most beans have the same factory, and 2) the identity check in equals is nearly-free and, frankly, faster than calculating the hashcode.hashCode()
method by implementing the method on the most common superclasses. One suggestion is to calculate a random number on object instantiation and keep that around as the hashcode. This serves the purpose of almost-uniquely identifying theBeanFactory
instance, and the memory penalty of anint
is small compared to the overall usage of mostBeanFactory
implementations.The text was updated successfully, but these errors were encountered: