Skip to content

Avoid unnecessary regex compilation in hot paths - #951

Merged
Intelli merged 1 commit into
PlayPro:masterfrom
Smorki:optimize-regex-compilation
Sep 10, 2026
Merged

Avoid unnecessary regex compilation in hot paths#951
Intelli merged 1 commit into
PlayPro:masterfrom
Smorki:optimize-regex-compilation

Conversation

@Smorki

@Smorki Smorki commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

Description

Two small optimizations to avoid unnecessary regex pattern compilation on frequently-called methods.

1. BlockBreakLogger.log() — replace replaceFirst with replace

The lectern block data adjustment uses String.replaceFirst("has_book=true", "has_book=false"), which internally compiles a Pattern on every invocation. Since the search string is a literal (no regex metacharacters) and appears at most once in a BlockData string, String.replace(CharSequence, CharSequence) is functionally identical and avoids the pattern compilation.

This method is called for every block break on the server, which is one of the highest-frequency events CoreProtect logs.

Change: replaceFirstreplace (1 character).

2. Config.getInt() — cache the digit-strip pattern

getInt() calls configured.replaceAll("[^0-9]", "") on every integer config read. Each call recompiles the regex. Extracting the pattern to a private static final Pattern field eliminates repeated compilation.

Change: Added NON_DIGIT_PATTERN static field and updated the call site to use it.

@Smorki

Smorki commented Aug 23, 2026

Copy link
Copy Markdown
Contributor Author

can anyone review?

@Intelli
Intelli merged commit a27026c into PlayPro:master Sep 10, 2026
3 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