nickbabcock / Pfim

.NET Standard targa (.tga) and direct draw surface (.dds) image decoder
https://nickbabcock.github.io/Pfim/
MIT License
110 stars 18 forks source link

Reduce allocations during image decoding. #118

Closed RoosterDragon closed 3 months ago

RoosterDragon commented 3 months ago

Related to 7632a0a313601b7269f0a3ea481a9e22a00028f9. A caller may use IImageAllocator to amortize allocations for the decoded image buffer when decoding multiple images. However, several other transient allocations are made which create memory traffic. By reducing these transient allocations, we reduce the number of garbage collections required for a performance boost.

We achieve this via a few approaches:

This provides the following benchmark improvements.


Before

Method Payload Mean Error StdDev StdErr Median Gen0 Allocated
Pfim 32bit.dds 2.015 us 0.0112 us 0.0088 us 0.0025 us 2.016 us 0.0381 496 B
Pfim dxt1.dds 38.347 us 0.7373 us 0.8491 us 0.1899 us 38.417 us 2.8687 36480 B
Pfim dxt3.dds 43.061 us 0.6037 us 0.5647 us 0.1458 us 42.982 us 2.8687 36480 B
Pfim dxt5.dds 48.005 us 0.5501 us 0.5146 us 0.1329 us 47.931 us 2.8687 36520 B
Method Payload Mean Error StdDev StdErr Median Gen0 Allocated
Pfim 24bit-rle.tga 3.180 us 0.0371 us 0.0347 us 0.0090 us 3.183 us 0.0191 272 B
Pfim 32bit-rle.tga 3.441 us 0.0266 us 0.0222 us 0.0062 us 3.436 us 0.0191 272 B
Pfim 32bit.tga 2.012 us 0.0107 us 0.0100 us 0.0026 us 2.012 us 0.0191 272 B

After

Method Payload Mean Error StdDev StdErr Median Gen0 Allocated
Pfim 32bit.dds 2.034 us 0.0242 us 0.0226 us 0.0058 us 2.032 us 0.0343 472 B
Pfim dxt1.dds 32.704 us 0.6247 us 0.6135 us 0.1534 us 32.393 us - 456 B
Pfim dxt3.dds 40.869 us 0.1294 us 0.1211 us 0.0313 us 40.860 us - 456 B
Pfim dxt5.dds 43.623 us 0.2501 us 0.2340 us 0.0604 us 43.594 us - 456 B
Method Payload Mean Error StdDev StdErr Median Gen0 Allocated
Pfim 24bit-rle.tga 2.965 us 0.0502 us 0.0469 us 0.0121 us 2.973 us 0.0191 248 B
Pfim 32bit-rle.tga 3.332 us 0.0531 us 0.0443 us 0.0123 us 3.329 us 0.0191 248 B
Pfim 32bit.tga 2.367 us 0.0404 us 0.0358 us 0.0096 us 2.358 us 0.0191 248 B
nickbabcock commented 3 months ago

Nicely done, the reduction in memory allocations speak for themselves. LGTM, I can look to merge this soon.