Skip to content

[repo-assist] perf: cache compiled Regex per pattern in JsonSchema.validate - #1806

Open
github-actions[bot] wants to merge 1 commit into
mainfrom
repo-assist/perf-jsonschema-regex-cache-e9b63f897ac9499a
Open

github-actions[bot] wants to merge 1 commit into
mainfrom
repo-assist/perf-jsonschema-regex-cache-e9b63f897ac9499a

Conversation

@github-actions

Copy link
Copy Markdown
Contributor

🤖 This PR was created by Repo Assist, an automated AI assistant.

Problem

JsonSchema.validate constructed a brand-new System.Text.RegularExpressions.Regex on every single string-value validation call when the schema had a pattern constraint:

| Some pattern ->
    let regex = System.Text.RegularExpressions.Regex(pattern)
    ...

For a schema validating many values against the same pattern (e.g. array items, or the same property repeated across many objects), this recompiled the identical regex from scratch on every call — wasted CPU with no benefit, since the pattern string itself never changes for a given schema node.

Fix

Added a small ConcurrentDictionary<string, Regex> cache (patternRegexCache) keyed by the pattern string, with a getPatternRegex helper that compiles a pattern once and reuses it thereafter. validate now calls getPatternRegex pattern instead of constructing a Regex inline.

Rationale / Trade-offs

  • No public API or behavior change — same match results, same ValidationResult outputs.
  • No new dependencies — System.Collections.Concurrent is part of the BCL.
  • Minor, bounded memory cost: one compiled Regex cached per distinct pattern string seen at runtime (schemas typically have a small, fixed set of patterns).
  • Thread-safe via ConcurrentDictionary, matching the library's existing use of a Regex per validation call site (previously recreated per-call, now created once).

Test Status

  • dotnet run --project build/build.fsproj -t Build: ✅ succeeded, 0 errors (pre-existing warnings only, unrelated to this change)
  • dotnet run --project build/build.fsproj -t CheckFormat: ✅ passed ("No files need formatting")
  • dotnet test tests/FSharp.Data.Core.Tests/FSharp.Data.Core.Tests.fsproj --filter "FullyQualifiedName~JsonSchema": ✅ 43/43 passed, including validate validates string pattern
  • dotnet run --project build/build.fsproj -t RunTests (full suite): ✅ 489 + 3143 + 2 tests passed, 0 failed

Also included

  • A RELEASE_NOTES.md entry (8.2.1) documenting the perf fix, per repository conventions.

Warning

Firewall blocked 6 domains

The following domains were blocked by the firewall during workflow execution:

  • api.nuget.org
  • api.worldbank.org
  • dc.services.visualstudio.com
  • raw.githubusercontent.com
  • schemas.microsoft.com
  • tomasp.net

To allow these domains, add them to the network.allowed list in your workflow frontmatter:

network:
  allowed:
    - defaults
    - "api.nuget.org"
    - "api.worldbank.org"
    - "dc.services.visualstudio.com"
    - "raw.githubusercontent.com"
    - "schemas.microsoft.com"
    - "tomasp.net"

See Network Configuration for more information.

Generated by 🌈 Repo Assist, see workflow run. Learn more.
Comment /repo-assist to run again

Add this agentic workflow to your repo

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/repo-assist.md@5d11aa2a05ce2c943c085acb7b12b583f83ed375

JsonSchema.validate previously constructed a brand-new System.Text.RegularExpressions.Regex
on every call for a schema with a string 'pattern' constraint. This meant every string value
validated against the same schema (e.g. array items, or repeated object properties) recompiled
the identical regex from scratch.

This change caches compiled Regex instances in a ConcurrentDictionary keyed by pattern string,
so the same pattern is only compiled once per process. No behavior change - same match results.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@dsyme
dsyme marked this pull request as ready for review September 28, 2026 10:16
@dsyme

dsyme commented Sep 28, 2026

Copy link
Copy Markdown
Contributor

/repo-assist fix the CI failure due to code formatting here

@github-actions

github-actions Bot commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor Author

✗ Repo Assist encountered failed, see workflow run.

Warning

Firewall blocked 6 domains

The following domains were blocked by the firewall during workflow execution:

  • api.nuget.org
  • api.worldbank.org
  • dc.services.visualstudio.com
  • raw.githubusercontent.com
  • schemas.microsoft.com
  • tomasp.net

To allow these domains, add them to the network.allowed list in your workflow frontmatter:

network:
  allowed:
    - defaults
    - "api.nuget.org"
    - "api.worldbank.org"
    - "dc.services.visualstudio.com"
    - "raw.githubusercontent.com"
    - "schemas.microsoft.com"
    - "tomasp.net"

See Network Configuration for more information.

Generated by 🌈 Repo Assist, see workflow run. Learn more.

@github-actions

Copy link
Copy Markdown
Contributor Author

🤖 This is an automated response from Repo Assist.

I investigated the build-ubuntu CI failure (CheckFormat: CsvRuntime.fs needs formatting) reported on attempt 2 of commit 3783699.

Findings — this looks like a transient/flaky failure, not a real formatting issue:

  • The independent check-format job on the same commit passed cleanly ("No files need formatting").
  • Locally, restoring the exact pinned Fantomas version (7.0.1 from .config/dotnet-tools.json) and running dotnet fantomas <all source files> --check reports "No files need formatting, 0 errored" — CsvRuntime.fs is correctly formatted.
  • CsvRuntime.fs at the PR head commit is byte-for-byte identical to the version in the working tree (no CRLF/encoding drift), and passes fantomas --check individually and repeatedly (5/5 runs).

Since I can't reproduce the failure and there's no actual formatting drift to fix, I haven't pushed a code change — that would be an unnecessary/unrelated diff. Re-running the build-ubuntu job should clear it. If it recurs consistently, it may indicate resource contention on the ubuntu-latest runner (fantomas running concurrently with the test build in the same job) rather than a genuine style violation.

Warning

Firewall blocked 6 domains

The following domains were blocked by the firewall during workflow execution:

  • api.nuget.org
  • api.worldbank.org
  • dc.services.visualstudio.com
  • raw.githubusercontent.com
  • schemas.microsoft.com
  • tomasp.net

To allow these domains, add them to the network.allowed list in your workflow frontmatter:

network:
  allowed:
    - defaults
    - "api.nuget.org"
    - "api.worldbank.org"
    - "dc.services.visualstudio.com"
    - "raw.githubusercontent.com"
    - "schemas.microsoft.com"
    - "tomasp.net"

See Network Configuration for more information.

Generated by 🌈 Repo Assist, see workflow run. Learn more.
Comment /repo-assist to run again

Add this agentic workflow to your repo

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/repo-assist.md@5d11aa2a05ce2c943c085acb7b12b583f83ed375

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant