You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Server: pool the Read result arrays until the response is sent (R2) - #5207
Pool the per-request result storage of the server Read service (R2 of the publish/write load work). The pooled storage is the DataValue[] results, the ServiceResult[] operation errors and the per-request attribute Dictionary<NodeId, Variant[]>. The arrays go back to the pool only after the transport has encoded or dropped the response.
Pooled buffer.ReadResultBuffer (new, Opc.Ua.Server/NodeManager) holds the arrays and the dictionary. It reuses the D5 PooledNotificationBuffer<T> bucket pool (16..4096 entries, at most 1024 buffers / 16 MiB per bucket). A request larger than the biggest bucket gets an exact buffer that is not retained.
Release point.StandardServer answers with an internal PooledReadResponse : ReadResponse (same pattern as LeasedPublishResponse). Its ReuseCore() clears Results and returns the buffer once. Transports call IPooledEncodeable.Reuse() only after encode or drop. Clone() copies the values and owns no buffer. A response that is never reused (an in-process caller, say) leaves its buffer to the GC.
Node managers get fixed-count IList views of exactly the request's length, because a rented array is larger and node managers may size by values.Count.
Failure paths. The buffer is returned and cleared on any exception (parameter validation, response budget, cancellation, a node manager throwing). The errors array and the attribute dictionary are cleared as soon as the dispatch finishes.
Not pooled. A server whose MasterNodeManager.ReadAsync is overridden, or that uses its own IMasterNodeManager, keeps the exact-array path, because an override may hold on to the results. The check is AOT-safe and falls back to "not pooled".
The dictionary is pooled, not made lazy. It is passed non-null into public GetPermissionMetadataAsync, and null there means "permissions only".
No public API changes. MasterNodeManager.ReadAsync still returns exact arrays.
Measurements
Paired A/B, read with 20 sessions × 100 nodes, 6 rounds. The base is 826dadcaa, which already includes #5193.
Metric
Base
R2
Change
Bytes per request
19,954
13,960
−30.0 % (all 12 runs)
CPU per request
211.6 µs
211.0 µs
−0.3 %
Values/s
7.51 M
7.38 M
−1.7 % (server cores −1.4 %, within drift)
GC pause per 15 s, default GC
404 ms
399 ms
flat
GC pause per 15 s, DATAS off (4 rounds)
54 ms
39 ms
−27 % (gen0 96 → 66)
With default settings the DATAS GC shrinks the gen0 budget as allocation drops, so the collection count and the pause stay flat. Lock contention went from about 500 to 820 events per 15 s against roughly 1.1 M requests, probably from the pool's queues.
Tests
New ReadResultBufferTests: values survive encoding after the buffer is returned and its arrays are overwritten. Also covers a single return on a double Reuse, Clone, the list semantics, dispatch-state clearing, and oversized requests.
New AsyncCustomNodeManagerTests.ReadPooling (all three node manager types): the pooled read matches an exact read. The buffer is returned on a mid-read failure (BadResponseTooLarge), on rejected parameters or a wrong buffer size, and on cancellation. Overridden ReadAsync is not pooled.
New ReadResultPoolingServerTests: ReferenceServer reads go through the pool, then encode, Reuse and decode across rounds.
Local runs on net10.0: Server.Tests passed 12,529. The 9 failures are file-system tests outside the Read path (8 need symlink privilege, 1 hit a teardown file lock). Client.Tests passed 3,998 with 2 skipped. The new tests also pass on net48.
Types of changes
Enhancement (non-breaking change which adds functionality)
Checklist
I have read the CONTRIBUTING doc.
I have added tests that prove my fix is effective or that my feature works.
The Read service allocated a DataValue[] for the response, a ServiceResult[]
for the operation results and a permission-attribute Dictionary per request.
StandardServer now rents all three from a pooled ReadResultBuffer
(PooledNotificationBuffer buckets) and answers with a PooledReadResponse whose
Results are a slice of the buffer. The transports call Reuse() once the
response has been encoded or dropped (as for LeasedPublishResponse), which
clears and returns the buffer.
- Node managers get fixed-length IList views of exactly the request's count,
so code that sizes by values.Count is unaffected by the larger pooled arrays.
- The operation results and attribute cache are cleared as soon as the
dispatch ends; the buffer is returned when the read fails (validation,
response budget, cancellation, node manager exception).
- Pooling is only used when MasterNodeManager.ReadAsync is not overridden; an
override (or a custom IMasterNodeManager) keeps the exact-array path, since
it may keep the results. MasterNodeManager.ReadAsync and the public
response types are unchanged.
- A response that is never reused (mutator replaced it, in-process caller)
leaves its buffer to the GC; Clone() copies the values.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The reason will be displayed to describe this comment to others. Learn more.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
The public ReadResponse.MemberwiseClone() bypasses ownership-aware cloning and shares the buffer, allowing reuse to clear the original response’s results and return the same buffer twice, corrupting another request.
The generated ReadResponse.MemberwiseClone is public and non-virtual, so a
copy made through a ReadResponse reference bypassed the Clone override and
shared the buffer reference. Reusing that copy returned the buffer and
cleared the original's results, and reusing the original returned it again.
The response now remembers itself as the owner, and a copy only drops its
reference.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
The clone path redundantly copies results already copied by base.Clone(), doubling request-sized result-array allocation and copying for every cloned response.
base.Clone() already copies Results through the generated ReadResponse.MemberwiseClone() and CoreUtils.Clone(ArrayOf<DataValue>). Copying the original results again here allocates a second request-sized array and immediately discards the first one, doubling result-array allocation and copying for every cloned response. Keep the results produced by the base clone and only remove its buffer ownership.
…onse
The generated ReadResponse.MemberwiseClone already deep-copies Results, so
the second copy in PooledReadResponse.Clone only allocated a request-sized
array to throw away the first one.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
❌ Patch coverage is 93.21267% with 15 lines in your changes missing coverage. Please review.
✅ Project coverage is 83.85%. Comparing base (826dadc) to head (0551484). ⚠️ Report is 3 commits behind head on master.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Proposed changes
Pool the per-request result storage of the server Read service (R2 of the publish/write load work). The pooled storage is the
DataValue[]results, theServiceResult[]operation errors and the per-request attributeDictionary<NodeId, Variant[]>. The arrays go back to the pool only after the transport has encoded or dropped the response.ReadResultBuffer(new,Opc.Ua.Server/NodeManager) holds the arrays and the dictionary. It reuses the D5PooledNotificationBuffer<T>bucket pool (16..4096 entries, at most 1024 buffers / 16 MiB per bucket). A request larger than the biggest bucket gets an exact buffer that is not retained.StandardServeranswers with an internalPooledReadResponse : ReadResponse(same pattern asLeasedPublishResponse). ItsReuseCore()clearsResultsand returns the buffer once. Transports callIPooledEncodeable.Reuse()only after encode or drop.Clone()copies the values and owns no buffer. A response that is never reused (an in-process caller, say) leaves its buffer to the GC.IListviews of exactly the request's length, because a rented array is larger and node managers may size byvalues.Count.MasterNodeManager.ReadAsyncis overridden, or that uses its ownIMasterNodeManager, keeps the exact-array path, because an override may hold on to the results. The check is AOT-safe and falls back to "not pooled".GetPermissionMetadataAsync, andnullthere means "permissions only".No public API changes.
MasterNodeManager.ReadAsyncstill returns exact arrays.Measurements
Paired A/B, read with 20 sessions × 100 nodes, 6 rounds. The base is
826dadcaa, which already includes #5193.With default settings the DATAS GC shrinks the gen0 budget as allocation drops, so the collection count and the pause stay flat. Lock contention went from about 500 to 820 events per 15 s against roughly 1.1 M requests, probably from the pool's queues.
Tests
ReadResultBufferTests: values survive encoding after the buffer is returned and its arrays are overwritten. Also covers a single return on a doubleReuse,Clone, the list semantics, dispatch-state clearing, and oversized requests.AsyncCustomNodeManagerTests.ReadPooling(all three node manager types): the pooled read matches an exact read. The buffer is returned on a mid-read failure (BadResponseTooLarge), on rejected parameters or a wrong buffer size, and on cancellation. OverriddenReadAsyncis not pooled.ReadResultPoolingServerTests: ReferenceServer reads go through the pool, then encode,Reuseand decode across rounds.Types of changes
Checklist
🤖 Generated with Claude Code