Skip to content

Conversation

polyglot-k
Copy link
Contributor

@polyglot-k polyglot-k commented Sep 19, 2025

Description:

  • Removed usage of external libraries (StringUtils) and Math.min().
  • Minimized intermediate String objects to reduce GC pressure.
  • Used StringBuilder with pre-allocated capacity for better memory efficiency.
  • Implemented manual handling of \n and \r replacements and truncated output after 80 characters.
  • Ensured the method works efficiently for both short and long content strings.
  • This change improves performance for high-frequency logging without affecting readability for standard logs.

Key Changes:

@Override
public String toString() {
  int maxLen = 80;
  int len = Math.min(content.length(), maxLen);
  
  StringBuilder sb = new StringBuilder(len + 20);
  
  for (int i = 0; i < len; i++) {
	  char c = content.charAt(i);
	  switch (c){
		  case '\n' -> sb.append("\\n");
		  case '\r' -> sb.append("\\r");
		  default -> sb.append(c);
	  }
  }
  
  if (content.length() > maxLen) {
	  sb.append("...(truncated)");
  }
  
  return "SockJsFrame content='" + sb + "'";
}

Benefits:

  • Reduced memory allocation and GC overhead.
  • Avoids dependency on external libraries.
  • Efficient handling for large strings.
  • Preserves readability for logs.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Sep 19, 2025
@spring-projects spring-projects deleted a comment from polyglot-k Sep 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting-for-triage An issue we've not yet triaged or decided on
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants