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

In, AutowireUtils.sortConstructors(), Using valueOf is faster than using constructor [SPR-12385] #16993

Closed
spring-projects-issues opened this issue Oct 28, 2014 · 3 comments
Assignees
Labels
in: core type: enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

@spring-projects-issues spring-projects-issues commented Oct 28, 2014

Kang Baek-Kyu opened SPR-12385 and commented

Using valueOf is approximately 3.5 times faster than using constructor

Because Using new Integer(int) is guaranteed to always result in a new object whereas Integer.valueOf(int) allows caching of values to be done by the compiler, class library, or JVM.

So changed source like this

return (new Integer(c1pl)).compareTo(c2pl) * -1;
==>return (Integer.valueOf(c1pl)).compareTo(c2pl) * -1;


Affects: 4.1.1

Reference URL: #682

Referenced from: commits 4945cf1

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Oct 28, 2014

Juergen Hoeller commented

Good catch! However, there's an even more efficient way of expressing this:

return (c1pl < c2pl ? 1 : (c1pl > c2pl ? -1 : 0));

analogous to the Integer.compare(int, int) method in Java 7. I've applied this change to both constructor and factory method sorting for 4.1.2 now.

Juergen

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Oct 28, 2014

Juergen Hoeller commented

I've also applied analogous changes to ReflectiveConstructorResolver and ReflectiveMethodResolver in SpEL.

Juergen

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Oct 28, 2014

Kang Baek-Kyu commented

Aha. Hou did not know that there is a new way.
Thanks, Juergen.

@spring-projects-issues spring-projects-issues added type: enhancement in: core labels Jan 11, 2019
@spring-projects-issues spring-projects-issues added this to the 4.1.2 milestone Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core type: enhancement
Projects
None yet
Development

No branches or pull requests

2 participants