Skip to content

CharacterEncodingFilter should avoid overhead of setCharacterEncoding(String) #37753

Done

Description

@shakuzen

When the CharacterEncodingFilter is configured, such as in a typical Spring Boot WebMVC application running on embedded Tomcat, on every request, CharacterEncodingFilter calls request.setCharacterEncoding(String). With Boot's defaults the request encoding is forced, so this happens even when the request already declares a charset. On Tomcat 11 that call resolves the name again each time: it lowercases it (allocating a new string), then looks it up in a ConcurrentHashMap. Servlet 6.1 added setCharacterEncoding(Charset), which Tomcat 11 implements without any lookup.

CharacterEncodingFilter.doFilterInternal
  RequestFacade.setCharacterEncoding(String)
    Request.setCharacterEncoding(String)
      CharsetHolder.getInstance(String)
        B2CConverter.getCharset(String)
          String.toLowerCase(Locale.ENGLISH)          "UTF-8" -> new String "utf-8"
          CharsetCache.getCharset(String)
            String.toLowerCase(Locale.ENGLISH)        (already lower case, no copy)
            ConcurrentHashMap.get                     hashes and compares the new string

The two Tomcat 11.0.29 Request methods differ only in that lookup:

// setCharacterEncoding(String enc)
CharsetHolder holder = CharsetHolder.getInstance(enc);  // B2CConverter.getCharset(enc): lowercase + map lookup
holder.validate();                                      // UnsupportedEncodingException if unknown
coyoteRequest.setCharsetHolder(holder);

// setCharacterEncoding(Charset charset)               (Servlet 6.1)
coyoteRequest.setCharsetHolder(CharsetHolder.getInstance(charset));   // no lookup

CharacterEncodingFilter could avoid some overhead by using setCharacterEncoding(Charset) when possible.

Activity

  1. added
    status: waiting-for-triage
    on Nov 24, 2026
  2. added
    in: web
    type: enhancement
    and removed
    status: waiting-for-triage
    on Nov 24, 2026
  3. added this to the 7.1.1 milestone on Nov 24, 2026
  4. added a commit that references this issue on Nov 24, 2026
    ba71b27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: webtype: enhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions