C#C
C#16mo ago
Pdawg

Optimizing the pipeline on my Z80 emulator

Hello! I've come to ask you all a question about optimization of lambda expressions. Currently, my pipeline looks like this:
private readonly Action[] instructionTable = new Action[256];

// in ctor, for example:
instructionTable[0x48] = () => LD_R_R(C, B); // LD C, B
instructionTable[0x49] = () => LD_R_R(C, C); // LD C, C
instructionTable[0x4A] = () => LD_R_R(C, D); // LD C, D

private void LD_R_R(byte dest, byte source)
{
    Registers.RegisterSet[dest] = Registers.RegisterSet[source];
    LogInstructionExec($"0x{_currentInstruction:X2}: LD {Registers.RegisterName(dest)}, {Registers.RegisterName(source)}:0x{Registers.RegisterSet[source]:X2}");
}

// calling:
_currentInstruction = Fetch();
switch (_currentInstruction)
{
    case 0xDD:
        DDInstructionTable[_currentInstruction](); break;
    case 0xFD:
        FDInstructionTable[_currentInstruction](); break;
    case 0xED:
        EDInstructionTable[_currentInstruction](); break;
    case 0xCB:
        CBInstructionTable[_currentInstruction](); break;

    default:
        instructionTable[_currentInstruction](); break;
}


This is nice because it's very reusable, but, lambda indirection to convert the LD_R_R(C, B) into an anonymous call takes time. In this case, it seems like lambdas are pretty slow. How should I go about optimizing this while still keeping it reusable?
Was this page helpful?