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

Optimize microinstructionData #14

Merged
merged 2 commits into from
Jul 27, 2022
Merged

Optimize microinstructionData #14

merged 2 commits into from
Jul 27, 2022

Conversation

lilweege
Copy link
Contributor

Addresses #11.

A profile of the code revealed a couple inefficiencies. Most of the time in update was spent copying and iterating over vector. The following are a few of the culprits.

Copying a vector:

vector<bool> mcode = microinstructionData.at(clamp(microcodeLocation, 0, 2047));

Copying again and iterating:
int readInstr = BinaryVecRangeToInt(mcode, 9, 11);

int BinaryVecRangeToInt(vector<bool> vec, int min, int max)
{
int result = 0;
int base = 1;
for (unsigned int i = max; i >= min; --i)
{
result += vec[i] * base;
base *= 2;
}
return result;
}

mcode is always 14 bits long, which means replacing each vector<bool> with uint16_t is low hanging fruit in terms of optimization. Extracting the int from the bits becomes as simple as a few shifts and a bitmask.

This change is enough to make a noticable improvement to most programs. For instance, on my machine the time to run rainbow_gradient.armstrong drops from ~3.5s to <0.5s (in the debug build).

There are many unnecessary copies of strings and vectors, these slow the program down
After profiling, it turns out that decoding ints from microinstructionData is a major bottleneck. vector wasn't ever necessary, since all instructions are the same number of bits (14). Manipulating bits is much faster than iterating over a vector.
@lilweege lilweege changed the title Optimizations Optimize microinstructionData Jul 26, 2022
@sam-astro
Copy link
Owner

Wow, that is a huge improvement, thank you!

@sam-astro sam-astro merged commit 9c77be7 into sam-astro:dev Jul 27, 2022
@lilweege lilweege deleted the lilweege/optimization branch August 3, 2022 03:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants