TASEmulators / BizHawk

BizHawk is a multi-system emulator written in C#. BizHawk provides nice features for casual gamers such as full screen, and joypad support in addition to full rerecording and debugging tools for all system cores.
http://tasvideos.org/BizHawk.html
Other
2.2k stars 384 forks source link

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

Open Martmists-GH opened 3 years ago

Martmists-GH commented 3 years ago

Current code: https://github.com/TASEmulators/BizHawk/blob/3ea71a2dda118b0cadd43c75ee6a577d01f5c6a9/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.

YoshiRulz commented 3 years ago

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

aineros85 commented 2 years ago

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.

YoshiRulz commented 1 year ago

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).