Skip to content

Avoid unnecessary regex compilation in hot paths - #951

Open
Smorki wants to merge 1 commit into
PlayPro:masterfrom
Smorki:optimize-regex-compilation
Open

Avoid unnecessary regex compilation in hot paths#951
Smorki wants to merge 1 commit into
PlayPro:masterfrom
Smorki:optimize-regex-compilation

Conversation

@Smorki

@Smorki Smorki commented Aug 11, 2026

Copy link
Copy Markdown

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.

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.

1 participant