Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Martmists-GH opened this issue Sep 10, 2021 · 3 comments
Labels
App: EmuHawk Relating to EmuHawk frontend Enhancement For feature requests or possible improvements Meta Relating to code organisation or to things that aren't code re: APIHawk Relating to EmuHawk's public .NET API or to the creation of external tools

Comments

@Martmists-GH
Copy link

Martmists-GH commented Sep 10, 2021

Current code:

public List<byte> ReadByteRange(long addr, int length, string domain = null)
{
var d = NamedDomainOrCurrent(domain);
if (addr < 0) LogCallback($"Warning: Attempted reads on addresses {addr}..-1 outside range of domain {d.Name} in {nameof(ReadByteRange)}()");
var lastReqAddr = addr + length - 1;
var indexAfterLast = Math.Min(lastReqAddr, d.Size - 1) - addr + 1;
var bytes = new byte[length];
for (var i = addr < 0 ? -addr : 0; i != indexAfterLast; i++) bytes[i] = d.PeekByte(addr + i);
if (lastReqAddr >= d.Size) LogCallback($"Warning: Attempted reads on addresses {d.Size}..{lastReqAddr} outside range of domain {d.Name} in {nameof(ReadByteRange)}()");
return bytes.ToList();
}

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 YoshiRulz added App: EmuHawk Relating to EmuHawk frontend Enhancement For feature requests or possible improvements Meta Relating to code organisation or to things that aren't code re: APIHawk Relating to EmuHawk's public .NET API or to the creation of external tools labels Sep 10, 2021
@YoshiRulz
Copy link
Member

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

@aineros85
Copy link

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
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
App: EmuHawk Relating to EmuHawk frontend Enhancement For feature requests or possible improvements Meta Relating to code organisation or to things that aren't code re: APIHawk Relating to EmuHawk's public .NET API or to the creation of external tools
Projects
None yet
Development

No branches or pull requests

3 participants