[AI Task] [Tizen.Sensor] Cache Marshal.SizeOf in Sensor batch event hot path#7739
[AI Task] [Tizen.Sensor] Cache Marshal.SizeOf in Sensor batch event hot path#7739JoonghyunCho wants to merge 2 commits into
Conversation
…7658) Sensor.updateBatchEvents is invoked from native sensor callbacks and called Marshal.SizeOf<Interop.SensorEventStruct>() on every loop iteration. The struct layout is fixed at compile time, so the size is now computed once into a static readonly field, removing per-event reflection/marshalling metadata lookups from the batch hot path. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
|
|
There was a problem hiding this comment.
Code Review
This pull request optimizes the Sensor class by caching the size of Interop.SensorEventStruct in a static readonly field, avoiding repeated calls to Marshal.SizeOf inside the updateBatchEvents loop. The reviewer suggested a further performance optimization to pre-allocate the capacity of the BatchedEvents list to prevent multiple internal array reallocations during the loop.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| for (int i = 0; i < events_count; i++) | ||
| { | ||
| BatchedEvents.Add(Interop.IntPtrToEventStruct(currentPtr)); | ||
| currentPtr += Marshal.SizeOf<Interop.SensorEventStruct>(); | ||
| currentPtr += s_sensorEventStructSize; | ||
| } |
There was a problem hiding this comment.
Since updateBatchEvents is on a hot path and BatchedEvents is cleared and repopulated on every call, we can optimize memory allocations by ensuring the underlying List<T> has sufficient capacity before the loop. This avoids multiple internal array reallocations and copying overhead during the loop.
Since BatchedEvents is initialized as a List<Interop.SensorEventStruct>, we can cast it and set its Capacity if it is currently smaller than events_count.
if (BatchedEvents is List<Interop.SensorEventStruct> list && list.Capacity < events_count)
{
list.Capacity = (int)events_count;
}
for (int i = 0; i < events_count; i++)
{
BatchedEvents.Add(Interop.IntPtrToEventStruct(currentPtr));
currentPtr += s_sensorEventStructSize;
}|
🤖 [AI Review] Reviewed — no findings. Scope checked:
No 🔴 critical issues, no 🟡 suggestions to flag. Automated review — final merge decision rests with human reviewers. |
Pre-size BatchedEvents list capacity in updateBatchEvents to avoid repeated internal array reallocations in the batch event hot path Applied-Human-Comments: 3524742585
|
🤖 [AI Review] |
Summary
Sensor.updateBatchEventsruns on the native sensor callback hot path and calledMarshal.SizeOf<Interop.SensorEventStruct>()on every loop iteration while marshalling batched events. Batch sensors (e.g. HRM Batch, motion sensors) can deliver tens to hundreds of events per callback at 100Hz+, so the per-iteration reflection/marshalling metadata lookup accumulates measurable overhead.Since the layout of
Interop.SensorEventStructis fixed at compile time, the size is now computed once into astatic readonlyfield.Changes
src/Tizen.Sensor/Tizen.Sensor/Sensor.csprivate static readonly int s_sensorEventStructSize = Marshal.SizeOf<Interop.SensorEventStruct>();updateBatchEventsadvances the event pointer by the cached size instead of callingMarshal.SizeOf<T>()per iteration (N calls → 0 per batch, where N =events_count).Mode
Refactoring
Verification
dotnet buildon Tizen.Sensor — 0 errors)sdb deviceslists no targets)Fixes #7658
🤖 Generated with Claude Code