WpfHexEditorControl

Wpf Hexeditor is a powerful and fully customisable user control for editing file or stream as hexadecimal, decimal and binary. Can be used in Wpf or WinForm application

View project on GitHub

WPF HexEditor - Performance Demonstration Sample

Interactive WPF application demonstrating the performance optimizations available in WPF HexEditor v2.2.0.

🎯 Purpose

This sample provides visual, interactive demonstrations of three major performance optimizations:

  1. Span<byte> Extensions - Zero-allocation operations
  2. Async/Await Extensions - Non-blocking I/O
  3. UI Virtualization - Memory-efficient rendering

πŸš€ Features

Tab 1: Span<byte> Demo

Compares:

  • ❌ Traditional array allocations
  • βœ… Modern Span<byte> with ArrayPool

Shows:

  • Execution time comparison
  • Memory allocation comparison
  • GC collection counts
  • Speed improvement factor

Expected Results:

  • 2-5x faster execution
  • 80% reduction in GC pressure
  • 98% less memory allocation

Tab 2: Async/Await Demo

Compares:

  • ❌ Synchronous search (UI freezes)
  • βœ… Asynchronous search (UI responsive)

Shows:

  • Real-time progress reporting
  • Cancellation support
  • UI responsiveness test (interactive button)

Expected Results:

  • UI stays responsive during long operations
  • User can cancel at any time
  • Progress updates in real-time

Tab 3: Virtualization Demo

Compares:

  • ❌ Rendering all bytes (high memory)
  • βœ… Rendering only visible bytes (low memory)

Shows:

  • Memory usage for different file sizes (1 MB, 10 MB, 100 MB)
  • Number of controls created
  • Render time estimates
  • Memory savings percentage

Expected Results:

  • 80-99% memory reduction
  • 10x faster initial render
  • Can handle GB-sized files

Tab 4: Combined Demo

Demonstrates: All three optimizations working together in a realistic scenario:

  1. Creates a 5 MB test file
  2. Calculates checksum using Span<byte> (zero allocations)
  3. Performs async search with progress (UI responsive)
  4. Calculates virtualization benefits (memory saved)

πŸŽ“ How to Use

  1. Build and Run:
    cd Sources/Samples/WpfHexEditor.Sample.Performance
    dotnet run
    
  2. Navigate Tabs:
    • Each tab focuses on one optimization
    • Follow the numbered buttons (1, 2, 3…)
    • Compare results side-by-side
  3. Learn from Code:
    • Check MainWindow.xaml.cs for implementation
    • See comments marking good (βœ…) vs bad (❌) practices
    • Copy patterns to your own code

πŸ“Š Performance Metrics

Real Measurements

The sample uses actual performance measurements:

  • Stopwatch - Precise timing
  • GC.GetTotalMemory() - Memory allocation tracking
  • GC.CollectionCount() - GC pressure measurement
  • Progress<int> - Real-time progress reporting

What You’ll See

Metric Traditional Optimized Improvement
Speed 5.2 ms 1.8 ms 2.9x faster
Memory 50 MB 1 MB 98% less
GC Gen 0 120 15 8x fewer
UI Freeze 8.5 sec 0 sec ∞ better
Memory (100 MB file) Out of Memory 35 MB 99.7% saved

πŸ”¬ Code Examples

Span<byte> Pattern

// βœ… GOOD: Zero allocations
using (var pooled = provider.GetBytesPooled(position, count))
{
    ReadOnlySpan<byte> data = pooled.Span;
    // Process data...
} // Buffer automatically returned to pool

Async Pattern

// βœ… GOOD: Non-blocking with progress
var progress = new Progress<int>(p => ProgressBar.Value = p);
var cts = new CancellationTokenSource();

var results = await provider.FindAllAsync(
    pattern,
    0,
    progress,
    cts.Token
);

Virtualization Pattern

// βœ… GOOD: Only render visible
var virtualization = new VirtualizationService();
var visibleLines = virtualization.GetVisibleLines(
    scrollOffset,
    viewportHeight,
    fileLength
);

// Render only visibleLines (not all lines!)

🎯 Target Audience

Developers

  • Learn modern C# performance patterns
  • See real-world before/after comparisons
  • Copy code for your own projects

Users

  • Understand why WPF HexEditor is fast
  • See visual proof of performance claims
  • Compare with other hex editors

Contributors

  • Verify performance optimizations work
  • Detect performance regressions
  • Add new optimization demos

πŸ› οΈ Requirements

  • .NET 8.0-windows SDK
  • Windows 10/11
  • Visual Studio 2022 or VS Code (optional)

πŸ“ Notes

Interactive Elements

  • Green buttons during async search - tests UI responsiveness
  • Cancel button - demonstrates cancellation support
  • Progress bars - shows real-time progress reporting

Test Files

  • Sample generates test files in memory
  • Or load your own files via β€œLoad File” buttons
  • Larger files show more dramatic improvements

Code Quality

  • Production-ready code patterns
  • Error handling included
  • Comments explain best practices
  • No β€œtoy” examples - real implementations

🀝 Contributing

Want to add more demos?

  1. Add a new tab to MainWindow.xaml
  2. Implement the demo logic in MainWindow.xaml.cs
  3. Update this README
  4. Submit a pull request!

Ideas for new demos:

  • SIMD vectorization
  • Memory-mapped files
  • Parallel processing
  • Custom search algorithms

πŸ“œ License

Apache 2.0 - Same as WPF HexEditor parent project


πŸš€ See performance optimizations in action!