Skip to content

Use lazy-regex for static regexes - #911

Merged
NSoiffer merged 1 commit into
daisy:mainfrom
moritz-gross:use-crate--lazy-regex
Oct 26, 2026
Merged

NSoiffer merged 1 commit into
daisy:mainfrom
moritz-gross:use-crate--lazy-regex

Conversation

@moritz-gross

@moritz-gross moritz-gross commented Oct 24, 2026 •

Copy link
Copy Markdown
Collaborator

Replace the LazyLock::new(|| Regex::new(r"...").unwrap()) boilerplate with lazy_regex!(r"...") for all 84 static regexes in braille.rs, canonicalize.rs, interface.rs, tts.rs, infer_intent.rs, chemistry.rs, xpath_functions.rs and speech.rs. The macro checks each pattern at compile time, so an invalid regex fails the build instead of panicking on first use.

No functional change: regexes are still compiled lazily by the regex crate. Regexes built at runtime (number separators, braille contractions) and non-regex LazyLocks are unchanged.


this removes quite a bit boilerplate, so I think this should be worth it (unless the memory footprint blows up from it, which I don't think will happen)

Replace the `LazyLock::new(|| Regex::new(r"...").unwrap())` boilerplate
with `lazy_regex!(r"...")` for all 84 static regexes in braille.rs,
canonicalize.rs, interface.rs, tts.rs, infer_intent.rs, chemistry.rs,
xpath_functions.rs and speech.rs. The macro checks each pattern at
compile time, so an invalid regex fails the build instead of panicking
on first use.

No functional change: regexes are still compiled lazily by the regex
crate. Regexes built at runtime (number separators, braille contractions)
and non-regex LazyLocks are unchanged.
@github-actions

Copy link
Copy Markdown
Linux library size: 0.59 MiB (+0.59%)
Revision Release liblibmathcat.so
Base (3aee7a4) 0.59 MiB (614,768 bytes)
PR (cb41d43) 0.59 MiB (618,368 bytes)
Change +3,600 bytes (+0.59%)

Built with default features, Rust 1.96.0, and Ubuntu 24.04. Workflow run.

@moritz-gross

moritz-gross commented Oct 24, 2026 •

Copy link
Copy Markdown
Collaborator Author

Claude says the failures on Windows are due to a race condition in prefs.rs, specifically copy_dir_all in line 1176, but I did not yet really look into the details here.
That means the failures are unrelated to the changes in this PR.

@NSoiffer

Copy link
Copy Markdown
Collaborator

I've been trying to come up with a fix for the update test. I did some work on it just before going into the hospital and it has just recently started showing up in the builds. It wasn't a problem before I made some improvements to something (I can't remember what). Can you ask Claude to figure out what change started causing the problem?

I like this change. For some reason it adds +3,600 bytes (+0.59%). Any idea why? If anything, I would have thought it would be slightly smaller. Maybe the macro is too general in what it generates?

@moritz-gross

Copy link
Copy Markdown
Collaborator Author

I only added Windows to CI pretty recently, so most likely the flaky test didnt show up as it just wasn't run.

I was also surprised to see the binary size increase. I can look into it.

@moritz-gross

Copy link
Copy Markdown
Collaborator Author

ok looks like the current way is broken anyway and throws away most of MathCAT:

the library-size workflow isn't really measuring MathCAT: liblibmathcat.so doesn't make any functions available to outside programs, so the linker removes almost all of MathCAT's code and we end up measuring mostly the Rust standard library (about 614 KB with or without LTO). With a small test crate that calls the main API functions, the way MathCATForPython and MathCATForC do, the library came out around 4.7 MB with LTO and 5.9 MB without it. I'd suggest having the workflow measure a small crate like that instead, built with lto = true like the real libraries and stripped before measuring, so the numbers show how a PR actually changes the size.

@moritz-gross

Copy link
Copy Markdown
Collaborator Author

How I see it, we need to properly do the binary size at some point, and we don't see it as a blocker here, as it is misleading.

Then (apart from the Windows issue), this should be good to go.

@NSoiffer

Copy link
Copy Markdown
Collaborator

Ok. But it sounds like fixing the size measurement needs to be added as an issue.

@NSoiffer
NSoiffer merged commit 17f34fb into daisy:main Oct 26, 2026
22 of 24 checks passed
@github-project-automation github-project-automation Bot moved this from Triage to Done in MathCAT Project Board Oct 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants