unicorn-engine / unicorn

Unicorn CPU emulator framework (ARM, AArch64, M68K, Mips, Sparc, PowerPC, RiscV, S390x, TriCore, X86)
http://www.unicorn-engine.org
GNU General Public License v2.0
7.33k stars 1.31k forks source link

Optimize memory handling #1963

Open PhilippTakacs opened 4 weeks ago

PhilippTakacs commented 4 weeks ago

By using the memory snapshots with real examples we found some bottlenecks. Therefor I have implemented some optimizations.

We found that the flatview creation is quite expensive (around 1/4 of all runtime including emulation) when you have a lot of memory regions. By permanently creating new regions with CoW we have a lot of regions and rebuild for each cow the flatview again. I have added a update function does only change affected ranges.

Also we found that the find_ram_offset_last optimization doesn't work after a restore. To fix this the context now includes also the ramblock_freed bool.

This also includes two smaller optimizations: only clear the affected tlb on memory_cow and save the last ramblock of the ram_list

It's currently only a draft, because we need to do a few more tests to see if the optimization works as expected.

PhilippTakacs commented 1 day ago

I have additional added the flatview to the context. This way flatview doesn't need to be rebuild on context_restore.

I have tested and profiled this. It does in fact save us relevant time.