Skip to content

Refactor: use a compiled pattern for skin hash validation#5523

Merged
onebeastchris merged 4 commits into
GeyserMC:masterfrom
TheMeinerLP:feature/improve_memory_usage_for_skin_pattern
Sep 29, 2025
Merged

Refactor: use a compiled pattern for skin hash validation#5523
onebeastchris merged 4 commits into
GeyserMC:masterfrom
TheMeinerLP:feature/improve_memory_usage_for_skin_pattern

Conversation

@TheMeinerLP
Copy link
Copy Markdown
Contributor

Using Pattern.compile() vs matches() for Regular Expressions in Java

This PR optimizes the memory usage for reset pattern in Java.

Why Pattern.compile() is Preferred Over matches()

When working with regular expressions in Java, using Pattern.compile() is significantly more efficient than matches() for several reasons:

  1. Performance Optimization: matches() recompiles the regex pattern on each call, while Pattern.compile() only compiles it once.

  2. Memory Efficiency: Precompiled patterns reduce memory usage and garbage collection overhead.

  3. Reusability: A compiled pattern can be reused across multiple operations.

According to the Java documentation, String.matches() internally creates a new Pattern object each time it's called.

// Inefficient - pattern recompiled on each iteration
for (String s : strings) {
    boolean r = s.matches("regex");
}

// Efficient - pattern compiled once
Pattern pattern = Pattern.compile("regex");
for (String s : strings) {
    boolean r = pattern.matcher(s).matches();
}

@TheMeinerLP TheMeinerLP marked this pull request as draft May 5, 2025 17:32
@onebeastchris
Copy link
Copy Markdown
Member

How's this looking? Ready for review? :)
It's only a minor optimization, but, won't hurt to have either way

@TheMeinerLP TheMeinerLP marked this pull request as ready for review September 8, 2025 08:05
@onebeastchris onebeastchris merged commit 30a44a0 into GeyserMC:master Sep 29, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants