[PR #5527] [client] Precompile anonymizer regex patterns at package scope #23230

Open
opened 2026-08-05 06:06:58 -04:00 by saavagebueno · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/netbirdio/netbird/pull/5527
Author: @matiasinsaurralde
Created: 3/6/2026
Status: 🔄 Open

Base: mainHead: chore/anonymize-precompile-regexps


📝 Commits (1)

  • 02c2304 [client] Precompile anonymizer regex patterns at package scope

📊 Changes

1 file changed (+9 additions, -11 deletions)

View changed files

📝 client/anonymize/anonymize.go (+9 -11)

📄 Description

Avoid compiling regexes on every anonymization call by promoting them to package-level compiled variables.

This reduces per-call CPU and allocation overhead in log/string anonymization paths.

Describe your changes

  • Moved regex patterns in client/anonymize/anonymize.go to package-level compiled variables:
    • domainKeyRegex
    • ipv4Regex
    • ipv6Regex
    • schemeURIRegex
  • Removed per-call regexp.MustCompile(...) inside:
  • AnonymizeString
  • AnonymizeSchemeURI
  • Kept anonymization behavior unchanged while reducing repeated regex compile work in hot log/string paths.
  • Verified with:
    • go test ./client/anonymize

N/A (performance refactor)

Stack

Checklist

  • Is it a bug fix
  • Is a typo/documentation fix
  • Is a feature enhancement
  • It is a refactor
  • Created tests that fail without the change (if possible)

By submitting this pull request, you confirm that you have read and agree to the terms of the Contributor License Agreement.

Documentation

Select exactly one:

  • I added/updated documentation for this change
  • Documentation is not needed for this change (explain why)
  • Reason: internal implementation-only refactor with no API/CLI behavior or user-facing workflow changes.

Docs PR URL (required if "docs added" is checked)

Summary by CodeRabbit

  • Refactor
    • Optimized regex pattern initialization for improved application performance.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/netbirdio/netbird/pull/5527 **Author:** [@matiasinsaurralde](https://github.com/matiasinsaurralde) **Created:** 3/6/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `chore/anonymize-precompile-regexps` --- ### 📝 Commits (1) - [`02c2304`](https://github.com/netbirdio/netbird/commit/02c23040971dbdd68ef494edac23399be0eeba0f) [client] Precompile anonymizer regex patterns at package scope ### 📊 Changes **1 file changed** (+9 additions, -11 deletions) <details> <summary>View changed files</summary> 📝 `client/anonymize/anonymize.go` (+9 -11) </details> ### 📄 Description Avoid compiling regexes on every anonymization call by promoting them to package-level compiled variables. This reduces per-call CPU and allocation overhead in log/string anonymization paths. ## Describe your changes - Moved regex patterns in client/anonymize/anonymize.go to package-level compiled variables: - `domainKeyRegex` - `ipv4Regex` - `ipv6Regex` - `schemeURIRegex` - Removed per-call `regexp.MustCompile(...)` inside: - `AnonymizeString` - `AnonymizeSchemeURI` - Kept anonymization behavior unchanged while reducing repeated regex compile work in hot log/string paths. - Verified with: - `go test ./client/anonymize` ## Issue ticket number and link N/A (performance refactor) ## Stack <!-- branch-stack --> ### Checklist - [ ] Is it a bug fix - [ ] Is a typo/documentation fix - [ ] Is a feature enhancement - [x] It is a refactor - [ ] Created tests that fail without the change (if possible) > By submitting this pull request, you confirm that you have read and agree to the terms of the [Contributor License Agreement](https://github.com/netbirdio/netbird/blob/main/CONTRIBUTOR_LICENSE_AGREEMENT.md). ## Documentation Select exactly one: - [ ] I added/updated documentation for this change - [x] Documentation is **not needed** for this change (explain why) - [ ] Reason: internal implementation-only refactor with no API/CLI behavior or user-facing workflow changes. ### Docs PR URL (required if "docs added" is checked) - <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Refactor** * Optimized regex pattern initialization for improved application performance. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
saavagebueno added the pull-request label 2026-08-05 06:06:58 -04:00
Sign in to join this conversation.
No Label pull-request
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: DYNR/netbird#23230