Skip to content

perf: share anchored variable names instead of copying them per Transaction - #3799

Open
ziebarthw wants to merge 3 commits into
owasp-modsecurity:v3/masterfrom
ziebarthw:perf/variable-names
Open

ziebarthw wants to merge 3 commits into
owasp-modsecurity:v3/masterfrom
ziebarthw:perf/variable-names

Conversation

@ziebarthw

@ziebarthw ziebarthw commented Dec 2, 2026 •

Copy link
Copy Markdown

Every Transaction builds ~80 anchored variables, and each copied its name into std::strings: once in AnchoredVariable/AnchoredSetVariable, and twice more in the VariableValue an AnchoredVariable embeds (m_key, m_keyWithCollection). Most names are longer than the small-string buffer, so creating and destroying a Transaction paid well over a hundred heap allocations for names that never change.

  • The names live once, as static constants in the new headers/modsecurity/variable_names.h. AnchoredVariable and AnchoredSetVariable keep a const reference to theirs. Both delete the std::string&& constructor, so a string literal (which would dangle) is a compile error, as suggested in the review of Perf/rule remove lookup #3615. The three variables added in 3.0.17 get constants.
  • VariableValue gets a constructor tagged BorrowKey that keeps a pointer to the key instead of copying it; getKey()/getKeyWithCollection() return it, and copies borrow it too. AnchoredVariable uses it with its name.

Creating and destroying a Transaction (C API, small rule set, -O2, glibc, single thread), this branch against v3/master (44d0938):

per transaction v3/master this PR
heap allocations 190 34
instructions (callgrind) ~157,000 ~74,000
time ~41.8 µs ~32.0 µs

The gain is in transaction setup and teardown. With a full CRS rule set, rule evaluation dominates a request, so test/benchmark shows little difference; it matters where setup is a large share of the work (high request rates, small rule sets, requests that end early).

This changes the C++ API/ABI of AnchoredVariable, AnchoredSetVariable, VariableValue and Transaction.

Tests: the regression suite passes (730 run, 0 failed), also under AddressSanitizer. Split out of #3615 as requested there.

Summary by CodeRabbit

  • Refactor
    • Transaction variables retain shared names and keys by reference rather than duplicating them. The set and ordering of transaction variables remain unchanged.
    • Referenced names and keys must remain valid for the lifetime of the objects that use them. Constructing anchored variables with temporary names and copying anchored set variables are no longer permitted.

ziebarthw and others added 2 commits December 2, 2026 15:42
… copies

Every Transaction built TransactionAnchoredVariables, whose ~80
AnchoredVariable / AnchoredSetVariable members each copied their name
into a std::string. Most names are longer than the small-string buffer,
so each Transaction paid dozens of heap allocations (and frees) for
names that never change.

The names now live once, as static constants in the new
headers/modsecurity/variable_names.h, and AnchoredVariable /
AnchoredSetVariable keep a const reference to theirs. Both delete the
constructor taking std::string&&, so a string literal (which would bind
to a temporary and dangle) is a compile error; MULTIPART_DUPLICATE_PART_HEADER,
MULTIPART_FILENAME_CHARSET and MULTIPART_FILENAME_LANGUAGE get constants.

This changes the C++ API/ABI of AnchoredVariable, AnchoredSetVariable
and Transaction (member types and layout).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01K6LVc4WrRHJUqiUKmMqSEJ
Every AnchoredVariable embeds a VariableValue (m_var), which copied the
variable's name twice more, into m_key and m_keyWithCollection.

VariableValue gets a constructor tagged BorrowKey that keeps a pointer
to the key instead of copying it; getKey() and getKeyWithCollection()
return the borrowed key when there is one, and copies of the value
borrow it too. AnchoredVariable uses it with its name, a static
variable_names:: constant that outlives every value and copy.

This changes the C++ API/ABI of VariableValue (a new member).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01K6LVc4WrRHJUqiUKmMqSEJ
@coderabbitai

coderabbitai Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The change stores anchored variable names and selected VariableValue keys by reference. It adds shared constants for anchored transaction variable names, rejects temporary names for anchored variables, and updates transaction initialization and key access to use borrowed values.

Changes: Anchored Variable Name and Key References

Layer / File(s) Summary
Shared names and anchored-variable references
headers/modsecurity/variable_names.h, headers/modsecurity/anchored_variable.h, headers/modsecurity/anchored_set_variable.h, headers/modsecurity/transaction.h, src/Makefile.am
The new variable_names constants provide names for anchored transaction variables. AnchoredVariable and AnchoredSetVariable store names by reference and reject temporary names. Transaction initialization uses the constants, and the new header is installed.
Borrowed keys in variable values
headers/modsecurity/variable_value.h, src/anchored_variable.cc
VariableValue can store a borrowed key pointer, preserves it in pointer-copy construction, and returns it from key accessors when set. AnchoredVariable initializes its value with the borrowed-key tag.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Refactor

Suggested reviewers: fzipi

Merge Risk: Minimal · up to 8b504

The change reduces per-transaction allocation and setup cost by sharing anchored variable names. No concrete merge-blocking risk was identified. Callers that pass temporary strings to the affected classes will fail to compile, and this is intended.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: anchored variable names are now shared (stored once as constants and referenced) instead of being copied for each Transaction. This matches the raw summary, which show…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: anchored variable names are now shared (stored once as constants and referenced) instead of being copied for each Transaction. This matches the raw summary, which shows AnchoredVariable and AnchoredSetVariable holding const references, new variable_names constants, and a BorrowKey constructor on VariableValue. The title's "perf:" prefix matches the stated goal of reducing transaction setup costs. The title is specific and concise, and it does not need to cover the deleted rvalue constructors or the BorrowKey addition.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 1f13e5cd-3b71-4bcf-bae9-eb645de7adbf
Commits

Reviewing files that changed from the base of the PR and between 1f5fda5 and 8b5042b.

Files selected for processing: 1

  • headers/modsecurity/variable_names.h

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

namespace modsecurity::variable_names { ... } (C++17) instead of two
nested blocks, as SonarCloud suggests, and one column for the constants.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01K6LVc4WrRHJUqiUKmMqSEJ
@sonarqubecloud

sonarqubecloud Bot commented Dec 2, 2026

Copy link
Copy Markdown

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants