Thursday, July 27, 2006

A great optimisation/bugfix..

..with a catch.

This week I've been posting about various speedups I've been making by implementing various opcodes in the new dynarec engine. Although I have implemented most of the commonly used opcodes now, after my previous post I decided to add some temporary logging to the emulator to see how often the remaining unhandled opcodes were being used. Two that immediately jumped out at me were JAL (Jump And Link, which is used to perform a function call) and JR (Jump Register, which is used to return from a function call.)

These two instructions are very heavily used, and I was surprised to realise that I'd not implemented them! They're pretty easy to code - in fact, due to the way I construct the instruction traces that are fed into the dynamic recompiler I could effectively ignore the JAL instruction and JR just required a couple of lines of code.

So far so good. I was expecting a modest speedup - maybe another 2-3% on top of all the previous changes I've made this week. After compiling and running the new code, I was amazed to see an improvement of over 10%! Surprised with the figures I was seeing, I did a full rebuild and checked the results again with several different roms. They all showed the same kind of speedup.

I've been programming (and more importantly debugging) long enough now when I should trust my instincts - call it my programming 'Spider-Sense' tingling if you will, but I knew something didn't quite add up :). In situations like this in the past, rather than taking an unexpected speedup for granted I've spent time investigating the root cause to find out exactly what's going on. At the very least I'll simply satisfy my own curiosity, but often I'll find some useful information along the way too (e.g. other related improvements and optimisations etc.)

So I started looking through the code and rerunning a few roms to try and get a handle on what was causing such a significant improvement. After a short while I realised that all the roms were now generating a lot more potential traces for the dynarec engine to consider for recompiling. This confused me even more, because this behaviour should slow the emulator down rather than speed it up. Another puzzling thing was that the only observable behaviour of my changes should be the speed of emulation - but it looked like my change was somehow changing the flow of execution in the rom.

After bit more head scratching and debugging, I finally realised what had happened. In making my changes, I had inadvertently fixed a bug in the dynarec engine that was causing the recompiled code to jump back out to the interpreter whenever a JAL instruction was encountered! This bug had been in the dynarec engine since the first day or so, but because its only side effect was to slow down the emulator rather than something more obvious (i.e. a crash!) it had remained undetected for a couple of months.

So I had figured out what the reason for the 10% speedup was, and I could finally get to bed safe in the knowledge that I had fixed a nasty, subtle bug along the way. Brilliant!

It was only then that I noticed a couple of new problems that I hadn't seen before: The emulator began hanging in places that had previously been fine - such as Peach's letter at the start of Mario 64. On the occasions the emulator managed to get past that point, I found out that Mario wouldn't move or jump (but strangely the c-buttons and pause menu worked fine)

:(

I've spent the last couple of evenings trying to figure out why fixing one bug is causing another. I've finally managed to find a way of reliably reproducing a hang within a few seconds of starting the emulator up. This is important because my best chance of identifying and fixing the problem is to be able to run the PC build of the emulator with my 'fragment simulator' enabled. The simulator is very slow however (about 100x slower than running the emulator normally), which is why it's important to find a way of reliably reproducing the bug very early on in the emulation.

So that's what I've been up to over the past couple of days, and why I haven't been able to reply to people's emails or comments on this blog. Now that I can reproduce the bug in the fragment simulator I'm confident that I can get to the bottom of it. I'll keep you posted with any developments and try to go through emails/comments just as soon as I've cracked it.

-StrmnNrmn

25 comments:

StevIEmw said...

Good work Strmnrm, great to hear your fixing the problems, keep up the good work!!!

Mario Kart God said...

Awesome to ear!!! Keep it up!

Mario Kart God said...

Just one question StrmnNrmn, how hard is this issue to totally fix? And does this mean you have the emulator going about 28%-32% faster than R6?!?!?!

RyanMWolfe said...

I hope R7 comes out right after you fix this problem and then that will keep us happy until R8.


Thank You StrnNrnm

PS. you are the Best i love the N64 and
PSP no i get them both

RyanMWolfe said...

Now Typo

StrmnNrmn said...

mario kart god: I'm not sure how hard it's going to be. It's quite tricky, but it's not a showstopper.
Yes - it's looking like it's going to be noticeably faster at the moment. I still have a lot of optimisations to make too!

StrmnNrmn said...

ryanmwolfe: Even once I fix this issue, there are a few additional optimisations and compatibility fixes I want to work on for R7. I'll keep you up to date with my plans on this blog though.

GeEkPiE said...

good good, keep up the good work

Minotaur said...

Awesome, dude! ^_^ Those are some mighty great improvements.

tsurumaru said...

Great to hear Strmnnrmn, good luck in getting to the bottom of the problem.

Kind regards.

Mario Kart God said...

Thanks StrmnNrmn! Glad your keeping us updated now!

Mario Kart God said...
This comment has been removed by a blog administrator.
Mario Kart God said...

Yo StrmnNrmn, when do you think R7 will come out. Can you give us an estimate please.

Minotaur said...

StrmnNrmn, what exactly is a fragment simulator and what exactly does it do?

Mario Kart God said...

StrmnNrmn can you please do me a favor and check if Mario Kart has any freezes also. I wouldn't ask you if I wasn't an addict of Mario Kart.

BigMace said...

Fantastic news, keep up the great work. I am just dying for the next release.

Medieval Ã…nubis said...

LOL
Sounds like you had the emergency brakes on ever since the first release .

Very Good Work Though I am very impressed

Mario Kart God said...

I am so excited for R7!!! Please release it, lets say once you get another 10-15% speed increase please.

salomon st said...

i would rather wait for a release that is good mario kart god i mean he said he has further optimizations to make im curious as to see how zelda runs now but how much of a speed increase r u looking for on this release r u going to make it as fast as u think u can get on this release or do it gradual through a few releases?

PSDroideka said...

good luck ironing it out man. the main thing i want in R7 is to be able to play zelda oot without it quitting to ask the settings for the rom! and to be able to return to the menu (ie pressing select then X)

GeEkPiE said...

owwwww, damnit

two massive annoying probs,

1. i downloaded zelda oot but it took like 3 hours coz my connections crappy and after waiting that long its all in italian.

2. i cant put it on my memstick coz iv only got a 32mb one and the rom + the emu is only just too big by only a few kb

ANNOYING

kersplatty said...

w000t good work hope you sort outthe bug soon, N-sixty-foouuuuurrr! wow jus imagine r10 will be like fullspeed and r11 with sound :O once u get decent speeds will you implement a media engine?

Mario Kart God said...

I'm sure he will after everything is pretty much perfect.

johnybon said...

Truly great news Strmnrm. Just a couple of questions - how long do you think things will contunue to work with eloader (will media engine implementation spell doom, or would you consider offering a non ME alternative?), and how much does the feedback you get help to keep you motivated?

mobilemob said...

Interesting website with a lot of resources and detailed explanations.
»