Skip to content

Erase MilkdropShader's sampler and texsize declarations without scanning the whole shader each time - #1048

Open
Hyphen-05 wants to merge 1 commit into
projectM-visualizer:masterfrom
Hyphen-05:milkdropshader-regex
Open

Hyphen-05 wants to merge 1 commit into
projectM-visualizer:masterfrom
Hyphen-05:milkdropshader-regex

Conversation

@Hyphen-05

Copy link
Copy Markdown

MilkdropShader::TranspileHLSLShader() removes sampler and texsize declarations with two
while (std::regex_search(...)) loops. Each pass constructs the regex again and searches the whole shader from the
start. On a Moto G Play (2023), 32-bit ARM with libc++, the two loops take about 7.5 ms per shader, so 15 ms on every
preset load. That is part of the stutter on preset switches reported in #913.

The two regexes are now built once when the library loads. That alone makes no measurable difference, because
constructing them only takes about 25 µs. The time is spent in regex_search trying the pattern at every position.
Both patterns start with a fixed word, so the new Utils::EraseRegexMatches() finds that word with
std::string::find() and only tries the regex there, using match_continuous. After each erase it searches again
from the start, like the old loop, because an erase can create a new match further up.

29 shaders from Cream of the Crop presets, per shader, on the same phone:

median 90th percentile
before 7.52 ms 10.50 ms
regexes built once only 7.55 ms 10.49 ms
this PR 0.19 ms 0.25 ms

The output is identical to the old loop for all 29 shaders, and for 300,000 random strings made from sampler, 2D,
3D, float4, texsize_, ( and whitespace.

Added unit tests for EraseRegexMatches(), including a case where an erase creates a new match earlier in the string.

…ing the whole shader each time

The regexes are built once when the library loads, and Utils::EraseRegexMatches() only tries
them where their fixed first word occurs. The result is the same as the old regex_search() loops.

@kblaschke kblaschke left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Thanks for the contribution!

@kblaschke kblaschke added the enhancement The feature is considered an enhancement. label Oct 14, 2026
@kblaschke kblaschke added this to the 4.2 milestone Oct 14, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement The feature is considered an enhancement.

Projects

Status: In Review

Development

Successfully merging this pull request may close these issues.

2 participants