Skip to content

High performance ArraySegment<T> for C#.

Notifications You must be signed in to change notification settings

unitycoder/ArraySegmentX

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 

Repository files navigation

ArraySegmentX

High performance ArraySegment<T> for C#.

The Bottleneck

Consider the following code for built in ArraySegment.

int n = segment.Array[segment.Offset + i];

IL generates two call instructions because .Array and .Offset are properties: image

ArraySegmentX does not need two extra call instructions: image

This is a micro optimization. In most cases, it doesn't matter. For heavy number crunching like in Mirror's delta compression, it does matter.

Benchmarks

Test.cs simple benchmarks with 1 million iterations

  • byte[]: 1.5s
  • ArraySegment: 3.6s
  • ArraySegmentX: 1.5s

=> 2.4x faster

Mirror's delta compression with 10k iterations, 1000 bytes, 1% changes:

  • byte[]: 1.7s
  • ArraySegment: 6.4s
  • ArraySegmentX: 1.7s

=> 3.76x faster

Convenience [] access

Unity's ArraySegment does not support direct [] access:

// supported
int n = segment.Array[segment.Offset + i];
  
// not supported
int n = segment[i];

ArraySegmentX adds [] access for covenience.

NOTE that this requires an IL call instruction. Use the long way where performance matters.

About

High performance ArraySegment<T> for C#.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages