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

ResourceHandlerRegistry#getHandlerMapping should initialize handler once in outer loop #27153

Closed
lijinliangyihao opened this issue Jul 11, 2021 · 1 comment
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Milestone

Comments

@lijinliangyihao
Copy link

Affects: 5.3.8


the code fragment below in method getHandlerMapping, line 168

    Map<String, HttpRequestHandler> urlMap = new LinkedHashMap<>();
    for (ResourceHandlerRegistration registration : this.registrations) {
	for (String pathPattern : registration.getPathPatterns()) {

            // 1. we get the 'handler' here from the outer loop variable 'registration', 
            //    set it's properties all with bean fields
            ResourceHttpRequestHandler handler = registration.getRequestHandler();
            if (this.pathHelper != null) {
	      handler.setUrlPathHelper(this.pathHelper);
            }
            if (this.contentNegotiationManager != null) {
	      handler.setContentNegotiationManager(this.contentNegotiationManager);
            }
            handler.setServletContext(this.servletContext);
            handler.setApplicationContext(this.applicationContext);
            try {
	      handler.afterPropertiesSet();
            }
            catch (Throwable ex) {
	      throw new BeanInitializationException("Failed to init ResourceHttpRequestHandler", ex);
            }

            // 2.  and here, we put it in a map, with inner loop's element as the key
            urlMap.put(pathPattern, handler);
	}
    }

Can't we move the code fragment in the inner loop between comment 1 and 2 to the outer loop?

@lijinliangyihao lijinliangyihao changed the title ResourceHandlerRegistry#getHandlerMapping inefficient loop: most part of the inner loop should moved to the outer loop ResourceHandlerRegistry#getHandlerMapping inefficient loop: most part of the inner loop should be moved to the outer loop Jul 11, 2021
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jul 11, 2021
@rstoyanchev rstoyanchev self-assigned this Jul 13, 2021
@rstoyanchev rstoyanchev added in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Jul 13, 2021
@rstoyanchev rstoyanchev added this to the 5.3.9 milestone Jul 13, 2021
@rstoyanchev
Copy link
Contributor

Good point. It looks like the inner loop grew over time and it went unnoticed.

@rstoyanchev rstoyanchev changed the title ResourceHandlerRegistry#getHandlerMapping inefficient loop: most part of the inner loop should be moved to the outer loop ResourceHandlerRegistry#getHandlerMapping should initialize handler once in outer loop Jul 13, 2021
Zoran0104 pushed a commit to Zoran0104/spring-framework that referenced this issue Aug 20, 2021
lxbzmy pushed a commit to lxbzmy/spring-framework that referenced this issue Mar 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

3 participants