Skip to content

fix: regex repeat compile - #2358

Merged
Calcium-Ion merged 1 commit into
QuantumNous:mainfrom
seefs001:fix/regrex-repeat-compile
Dec 9, 2025
Merged

fix: regex repeat compile#2358
Calcium-Ion merged 1 commit into
QuantumNous:mainfrom
seefs001:fix/regrex-repeat-compile

Conversation

@seefs001

@seefs001 seefs001 commented Dec 2, 2025

Copy link
Copy Markdown
Collaborator

Summary by CodeRabbit

  • Chores
    • Optimized internal pattern matching and string handling for improved efficiency.
    • Updated dependencies to the latest versions.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai

coderabbitai Bot commented Dec 2, 2025

Copy link
Copy Markdown
Contributor

Walkthrough

This pull request refactors code for performance optimization and improved maintainability. Precompiled package-level regexes are introduced in two files to avoid per-call regex compilation. A mutation in json.go is removed by using a trimmed variable. The math/rand dependency is replaced with lo.RandomString, and the go.mod dependency for lo is bumped from v1.39.0 to v1.52.0.

Changes

Cohort / File(s) Change Summary
Regex precompilation for performance
common/str.go, relay/common/override.go
Introduces package-level precompiled regex patterns (maskURLPattern, maskDomainPattern, maskIPPattern, negativeIndexRegexp) to replace local inline regex compilations, reducing per-call overhead.
Dependency and utility refactoring
common/str.go
Replaces math/rand with lo.RandomString from the lo package; adds guard check for non-positive string lengths.
Input data handling
common/json.go
Refactors GetJsonType to avoid mutating input data; creates trimmed variable and derives firstChar from it rather than directly modifying the input.
Dependency management
go.mod
Updates github.com/samber/lo from v1.39.0 to v1.52.0.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

  • Focus on the lo.RandomString implementation in common/str.go, particularly the non-positive length guard and any behavior differences from math/rand
  • Verify precompiled regex patterns in common/str.go and relay/common/override.go are correctly defined and match the original inline patterns
  • Confirm the go.mod dependency bump to v1.52.0 has no breaking changes affecting the codebase

Possibly related PRs

Poem

🐰 Regex compiled once, not thrice each call,
No mutation of data—clean code for all,
Lo strings hop faster than rand's old way,
Optimization hops through the code today!

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 12.50% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix: regex repeat compile' directly and clearly summarizes the main change: addressing repeated regex compilations by precompiling regex patterns at the package level across multiple files.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
common/str.go (1)

15-19: Precompiled regex patterns improve performance.

The package-level precompiled regex patterns are a good optimization. However, note that:

  • The maskURLPattern has a permissive pattern [^\s/$.?#].[^\s]* that might match more than intended URLs.
  • The maskIPPattern doesn't validate IP octet ranges (e.g., 999.999.999.999 would match), but this is acceptable for masking purposes as false positives will just result in extra masking.

Consider tightening the URL pattern if you want more precise matching:

-	maskURLPattern    = regexp.MustCompile(`(http|https)://[^\s/$.?#].[^\s]*`)
+	maskURLPattern    = regexp.MustCompile(`(https?://[^\s]+)`)
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7d1bad1 and 896e4ac.

⛔ Files ignored due to path filters (1)
  • go.sum is excluded by !**/*.sum
📒 Files selected for processing (4)
  • common/json.go (1 hunks)
  • common/str.go (4 hunks)
  • go.mod (1 hunks)
  • relay/common/override.go (2 hunks)
🔇 Additional comments (6)
relay/common/override.go (2)

14-15: LGTM! Precompiled regex improves performance.

Introducing a package-level precompiled regex for negative index matching is a good optimization. The pattern \.(-\d+) correctly matches negative indices in JSON paths.


190-218: LGTM! Correct usage of precompiled regex.

The refactored processNegativeIndex function correctly uses the precompiled negativeIndexRegexp instead of compiling the regex on every call, which improves performance without changing functionality.

common/json.go (1)

25-30: LGTM! Cleaner implementation with trimmed variable.

The refactored code correctly stores the trimmed result in a variable and reuses it, which is cleaner and avoids potential multiple calls to TrimSpace. The logic remains functionally equivalent.

common/str.go (2)

176-239: LGTM! Correctly uses precompiled regex patterns.

The MaskSensitiveInfo function now correctly uses the precompiled regex patterns (maskURLPattern, maskDomainPattern, maskIPPattern) instead of compiling them on each call. This is a good performance optimization with no functional changes.


28-33: Verify security implications of non-cryptographic randomness source.

The refactored GetRandomString now uses lo.RandomString, which relies on math/rand rather than cryptographically secure randomness. While the defensive guard for non-positive lengths and code simplification are improvements, if this function is used for security-sensitive purposes (tokens, session IDs, security keys), consider:

  • Reviewing call sites to determine if cryptographic randomness is required
  • If security-sensitive, switch to crypto/rand directly instead of lo.RandomString
  • If for non-security purposes (e.g., temporary IDs, display strings), the current approach is acceptable
go.mod (1)

36-36: No compatibility concerns; v1.52.0 maintains backward compatibility.

The github.com/samber/lo library maintains SemVer guarantees with no breaking changes before v2.0.0. Both RandomString and AlphanumericCharset APIs are available and stable in v1.52.0, making this a safe upgrade within the v1.x series.

Likely an incorrect or invalid review comment.

@Calcium-Ion
Calcium-Ion merged commit 5212fbd into QuantumNous:main Dec 9, 2025
1 check passed
ennnnny pushed a commit to ennnnny/new-api that referenced this pull request Mar 17, 2026
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