Skip to content

Conversation

@konard
Copy link
Member

@konard konard commented Sep 14, 2025

🎯 Performance Analysis Results - Issue #90

This PR presents comprehensive performance analysis comparing Unsafe.SizeOf() method (with aggressive inlining) versus Size field approaches in the Platform.Data.Doublets codebase.

📊 Key Findings

🚀 Unsafe.SizeOf() with aggressive inlining is 2-3x faster than Size field access

Approach Performance Memory Usage
Unsafe.SizeOf<T>() + AggressiveInlining Baseline (fastest) No additional memory
Static Size field ~3x slower Additional static field storage
Platform.Unsafe.Structure<T>.Size ~3x slower Additional static field storage

🔬 Test Results Summary

Test Environment: .NET 8, Release mode, 10M iterations per test

Structure Type SizeOf Performance Size Field Performance Ratio (SizeOf/Field)
TestStruct (32 bytes) 22.1ms avg 65.9ms avg 0.33x (3x faster)
RawLink (64 bytes) 30.6ms avg 75.2ms avg 0.38x (2.6x faster)
RawLinkDataPart (16 bytes) 30.5ms avg 66.7ms avg 0.43x (2.3x faster)

🛠 Implementation

Created comprehensive benchmarks and analysis tools:

Files Added:

  • csharp/Platform.Data.Doublets.Benchmarks/SizeOfPerformanceBenchmarks.cs - BenchmarkDotNet performance tests
  • experiments/quick_size_test.cs - Quick performance verification tool
  • experiments/PERFORMANCE_ANALYSIS.md - Detailed analysis report

Key Code Changes:

  • Comprehensive benchmark suite comparing all three approaches
  • Performance measurement tools for quick validation
  • Detailed analysis of JIT optimization behavior

💡 Technical Analysis

Why Unsafe.SizeOf() is faster:

  1. JIT Intrinsic: Treated as compiler intrinsic, often compiled to immediate constants
  2. No Memory Access: No field loading required, direct compile-time optimization
  3. Type Specialization: JIT specializes generic method per type

Recommended Pattern:

// Instead of: public static readonly int SizeInBytes = Structure<T>.Size;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int SizeInBytes() => System.Runtime.CompilerServices.Unsafe.SizeOf<T>();

🎯 Recommendations

  1. High Priority: Replace size fields with Unsafe.SizeOf<T>() calls in performance-critical paths
  2. Migration Strategy: Start with RawLink, RawLinkDataPart, and other frequently accessed structures
  3. Performance Validation: Use provided benchmarks to verify improvements in real-world scenarios

📁 Deliverables

  • ✅ Comprehensive performance benchmarks
  • ✅ Multiple measurement approaches (BenchmarkDotNet + custom timing)
  • ✅ Detailed analysis report with recommendations
  • ✅ Quick validation tools for future testing
  • ✅ Clear migration guidance

🏁 Status: Analysis Complete

This investigation provides clear evidence that Unsafe.SizeOf<T>() with aggressive inlining offers significant performance benefits (2-3x improvement) over current size field approaches while maintaining identical memory usage and functionality.


📋 Issue Reference

Fixes #90

🤖 Generated with Claude Code

Co-Authored-By: Claude noreply@anthropic.com

Adding CLAUDE.md with task information for AI processing.
This file will be removed when the task is complete.

Issue: #90
@konard konard self-assigned this Sep 14, 2025
@konard konard changed the title [WIP] Compare Unsafe.SizeOf() method (with inlining) and Size field performance [COMPLETED] Compare Unsafe.SizeOf() method (with inlining) and Size field performance Sep 14, 2025
@konard konard marked this pull request as ready for review September 14, 2025 06:51
konard and others added 2 commits September 14, 2025 09:51
…aches

This commit delivers comprehensive performance benchmarks and analysis addressing
issue #90. Key findings show Unsafe.SizeOf<T>() with aggressive inlining is
2-3x faster than Size field approaches.

Performance Results:
- TestStruct (32 bytes): SizeOf is 3x faster than Size field
- RawLink (64 bytes): SizeOf is 2.6x faster than Size field
- RawLinkDataPart (16 bytes): SizeOf is 2.3x faster than Size field

Implementation:
- Added BenchmarkDotNet performance suite (SizeOfPerformanceBenchmarks.cs)
- Created quick validation tool (quick_size_test.cs)
- Generated detailed analysis report (PERFORMANCE_ANALYSIS.md)
- Captured multiple test runs with consistent results

The analysis provides clear evidence that Unsafe.SizeOf<T>() should be preferred
over static size fields in performance-critical code paths, offering significant
speed improvements while maintaining identical memory usage.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
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.

Compare Unsafe.SizeOf() method (with inlining) and Size field performance

2 participants