BizHawk icon indicating copy to clipboard operation
BizHawk copied to clipboard

Make ReadByteRange use a wrapper class instead of copying the entire thing into a new byte array

Open Martmists-GH opened this issue 2 years ago • 2 comments

Current code: https://github.com/TASVideos/BizHawk/blob/master/src/BizHawk.Client.Common/Api/Classes/MemoryApi.cs#L246-L256

when a readbyterange is used every frame for large amounts, this can greatly reduce performance (60fps to 2fps). This is partially due to the memory allocation every frame, but also because all the data is copied over. It would likely be more efficient to read them when they are accessed. This has the drawback of not accounting for bytes written to that range since reading, so maybe add a nullable parameter to specify whether to lazy-load the range.

The reason for this request is that I often have to access memory quite often in a single frame, resulting in a ~20fps decrease just from all the memory.read_XXX calls, and having it available faster would be nice.

Martmists-GH avatar Sep 10 '21 12:09 Martmists-GH

For the record, I once looked into using Span for this, but the problem goes deeper than ApiHawk.

YoshiRulz avatar Sep 10 '21 12:09 YoshiRulz

It looks like the data is copied a few times. Once into the array within ReadByteRange, again in the .ToList() call, then again at the call site. Perhaps add a ReadByteRangeArray overload that specifically returns a byte array to eliminate these extra copies.

aineros85 avatar Jul 16 '22 03:07 aineros85

I just removed a copy. I've also opened #3472 which adds a Span overload to reduce allocations (it's not magic, there is still a copy, but it's something).

YoshiRulz avatar Dec 04 '22 00:12 YoshiRulz