Skip to content

AddParam(value) and AddParams(object) allocate no more on a repeat call than AddParam("@name", value) - #104

Merged
electricessence merged 8 commits into
masterfrom
perf/derived-name-cost
Nov 10, 2026
Merged

electricessence merged 8 commits into
masterfrom
perf/derived-name-cost

Conversation

@electricessence

@electricessence electricessence commented Nov 10, 2026 •

Copy link
Copy Markdown
Collaborator

A user measured AddParams(object) with three members at about 2.1 KB more per call than three named AddParam calls. The procedure's names were already remembered, but the matching was redone on every execution.

What was redone on every call

  • The cache key was a new string (server + database + procedure).
  • Two List<Param> copies of the parameters.
  • Candidate names for each argument (a list, an array, substrings, joins).
  • A copy of the real names without their @, for each argument.
  • A substring per @ name in the two same-name loops.
  • A text command parsed its SQL again.
  • The command's options block was created only to set a flag.

What is cached now, where, and for how long

  • The cache key is a small struct of the three parts, compared part by part (ignoring case). No string is built.
  • A NameSet holds a procedure's real names, the names without @ (once), and the name each argument expression matched. It lives as long as the procedure's entry: 5 minutes, then it is read again. A match that throws (an ambiguity) is not stored, so it throws on every call. Limits and the weak owner are unchanged.
  • A text command's NameSet is kept on the command text string (a ConditionalWeakTable, no string.IsInterned gate): a literal stays cached, a released string's entry goes with it.
  • The last member and the candidate names of an argument expression are kept on the expression string, which is the same literal at every call site.
  • The same-name check works on spans, and applying the names uses one pooled array (only the used part is cleared) instead of two lists. Results, order and error messages are the same.
  • The flag that marks derived parameters is a field on the command, so AddParam(value) no longer creates the options block.
  • Not changed: boxing. Param.Value and DbParameter.Value are object, so the named AddParam boxes as well.

Bytes per call (in-memory fakes, after warm-up)

Case Before: derived Before: named After: derived After: named
Stored procedure, 3 plain locals 3440 552 560 560
Stored procedure, 3 member chains 4024 552 560 560
Stored procedure, AddParams with 3 members 3440 552 560 560
Text command, literal SQL, 3 arguments 2856 552 560 560
Stored procedure, async, 3 plain locals 3440 552 560 560
Stored procedure, async, AddParams 3440 552 560 560
Text command, SQL built on every call 3096 792 1720 800

The named side went from 552 to 560 bytes because of the command size (below). The new test file asserts derived <= named + 16 for each case except the dynamic-SQL one.

Command size: conn.Command(sql) allocates 56 bytes before and 64 after (the flag field adds 8). The saving is far larger, so the change stays.

SQL built on every call

When the SQL text is a new string object each time, its entry is used only while that string lives. It costs 1720 bytes per call (about 920 more than the named call), plus a GC handle that the allocated bytes do not show. That trade is accepted: memory for speed, and the weak table cannot grow out of control because each entry goes with its string. A string built once and reused, or reused before it is collected, is a hit. There is no string.IsInterned gate, by decision.

Notes

  • Key equality is part by part, so it is slightly stricter than the old concatenation (a newline inside a part could collide before).
  • DataSource and Database of a real provider may allocate; the fakes return constants, so that is not measured.
  • The same SQL text in another case, or as another string object built per call, is its own weak-table entry.
  • No public API change; PublicAPI.*.txt are untouched.

Checks

  • New tests: allocation budgets above, plus the same procedure on another database or server, stale names read again (old matches not reused), an ambiguity on every call, dropped object member, caller name wins, collision messages pinned, two string objects with one SQL text, the text's weak entry does not keep it alive, and 1,000 concurrent executions on one name set.
  • Full gate on the last commit: Release and Debug builds 0 warnings and 0 errors; 9243 tests pass in each; 86 sample checks pass; style and analyzers 0 findings in all projects; DocFX 0 warnings.
  • SARIF and Roslynator scans: no finding in a file this change touches.
  • An independent review of the change against its plan passed. It asked for two more small tests and one missing space, which are the last three commits: an AddParams member whose name cannot be derived (a verbatim @default) throws ArgumentException on every call, and a text command's collision message when the names are not known is pinned.

🤖 Generated with Claude Code

electricessence and others added 8 commits November 9, 2026 15:30
…named ones

The procedure's names and what each argument matched are remembered in one set, the cache key is a struct instead of a string, and the unique-name resolution no longer copies the parameter list.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…be derived, and a text command's collision when the names are not known

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…mber test

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@electricessence
electricessence merged commit 0234d1a into master Nov 10, 2026
2 checks passed
@electricessence
electricessence deleted the perf/derived-name-cost branch November 10, 2026 01:13
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.

1 participant