Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance optimization in AbstractBeanFactoryBasedTargetSource.hashCode() #30576

Closed
jengebr opened this issue Jun 1, 2023 · 0 comments
Closed
Assignees
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

Comments

@jengebr
Copy link

jengebr commented Jun 1, 2023

Affects: 5.2.20

Our production application shows 2.8% of spent cpu coming from the following stack trace:

java.lang.Object.hashCode
org.springframework.util.ObjectUtils.nullSafeHashCode
org.springframework.aop.target.AbstractBeanFactoryBasedTargetSource.hashCode
org.springframework.aop.framework.CglibAopProxy$HashCodeInterceptor.intercept
<proprietary>$SpringCGLIB-Proxy.hashCode
<proprietary>.hashCode

Reviewing the code for AbstractbeanFactoryBasedTargetSource.hashCode(), we see:

@Override
public int hashCode() {
	int hashCode = getClass().hashCode();
	hashCode = 13 * hashCode + ObjectUtils.nullSafeHashCode(this.beanFactory);
	hashCode = 13 * hashCode + ObjectUtils.nullSafeHashCode(this.targetBeanName);
	return hashCode;
}

this.targetBeanName is a String, and therefore never routes toObject.hashCode(). That leaves this.beanFactory as the only possible code path, and indeed there is no hashCode() defined on the interface or any bundled implementation.

Modifying our custom bean factory (extends DefaultListableBeanFactoryWithPublicMutex) with the following methods solves the problem:


    /**
     * This should be a singleton, hashcode doesn't matter unless somebody else is
     * silly enough to use it. See AbstractBeanFactoryBasedTargetSource.hashCode
     * (which uses it).
     * 
     * This implementation is effectively free, and certainly much faster than
     * Object.hashCode().
     */
    @Override
    public int hashCode() {
        return 0;
    }

    /**
     * This should be a singleton, use identity equals.
     */
    @Override
    public boolean equals(Object o) {
        return (this == o);
    }

I have two suggestions for how to solve this within the framework:

  1. Modify 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.
  2. Accelerate the 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 the BeanFactory instance, and the memory penalty of an int is small compared to the overall usage of most BeanFactory implementations.
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jun 1, 2023
@jhoeller jhoeller self-assigned this Jun 1, 2023
@jhoeller jhoeller added in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Jun 1, 2023
@jhoeller jhoeller added this to the 6.0.10 milestone Jun 1, 2023
@jhoeller jhoeller changed the title Performance optimization in common path Performance optimization in AbstractBeanFactoryBasedTargetSource.hashCode() Jun 1, 2023
@jhoeller jhoeller added the for: backport-to-5.3.x Marks an issue as a candidate for backport to 5.3.x label Jun 2, 2023
@github-actions github-actions bot added status: backported An issue that has been backported to maintenance branches and removed for: backport-to-5.3.x Marks an issue as a candidate for backport to 5.3.x labels Jun 2, 2023
jhoeller added a commit that referenced this issue Jun 2, 2023
Includes hashCode optimization in AbstractBeanFactoryBasedTargetSource.
Includes ThreadLocal naming fix in ThreadLocalTargetSource.

Closes gh-30576
Closes gh-30581

(cherry picked from commit c685525)
mdeinum pushed a commit to mdeinum/spring-framework that referenced this issue Jun 29, 2023
Includes hashCode optimization in AbstractBeanFactoryBasedTargetSource.
Includes ThreadLocal naming fix in ThreadLocalTargetSource.

Closes spring-projectsgh-30576
Closes spring-projectsgh-30581
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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
Projects
None yet
Development

No branches or pull requests

3 participants