Open Martmists-GH opened 3 years ago
For the record, I once looked into using Span
for this, but the problem goes deeper than ApiHawk.
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.
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).
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.