-
Notifications
You must be signed in to change notification settings - Fork 38.3k
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
Optimize DefaultLifecycleProcessor::startBeans #25506
Conversation
phases.put(phase, group); | ||
} | ||
LifecycleGroup group = phases.computeIfAbsent(phase, | ||
p -> new LifecycleGroup(phase, this.timeoutPerShutdownPhase, lifecycleBeans, autoStartupOnly)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
documentation of computeIfAbsent
says:
return the current (existing or computed) value...
so you could do:
phases.computeIfAbsent(
phase,
p -> new LifecycleGroup(phase, timeoutPerShutdownPhase, lifecycleBeans, autoStartupOnly)
).add(beanName, bean);
But looking at the implementation, phases
is really just a sorted by Keys, which mean it's really supposed to be a TreeMap
? So that code could be written as :
private void startBeans(boolean autoStartupOnly) {
Map<String, Lifecycle> lifecycleBeans = getLifecycleBeans();
Map<Integer, LifecycleGroup> phases = new TreeMap<>();
lifecycleBeans.forEach((beanName, bean) -> {
if (!autoStartupOnly || (bean instanceof SmartLifecycle && ((SmartLifecycle) bean).isAutoStartup())) {
int phase = getPhase(bean);
phases.computeIfAbsent(
phase,
p -> new LifecycleGroup(phase, timeoutPerShutdownPhase, lifecycleBeans, autoStartupOnly)
).add(beanName, bean);
}
});
phases.forEach((key, value) -> value.start());
}
The use of forEach
is safe since it is documented as :
Unless otherwise specified by the implementing class, actions are performed in the order of entry set iteration
and TreeMap::entrySet
is documented as :
The set's iterator returns the entries in ascending key order...
What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think what you said makes sense, but I added a judgment to reduce the execution of the iterator automatic code.
Thanks !
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm sorry, but can you explain "but I added a judgment to reduce the execution of the iterator automatic code" - what this means? I don't get it. thank you
This has been merged into Thanks |
Use lambda for minor refactoring