Skip to content

Rewrite msvc asm to not use non volatile registers - #87

Open
ZXShady wants to merge 1 commit into
anjo76:masterfrom
ZXShady:optimize_1_branch
Open

ZXShady wants to merge 1 commit into
anjo76:masterfrom
ZXShady:optimize_1_branch

Conversation

@ZXShady

@ZXShady ZXShady commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

I tested this change by running the as_test suite, and all tests passed.

While learning x64 assembly for a compiler project (with the goal of contributing more to AngelScript, as I really enjoy the language) and studying calling conventions, I spent time reading through AngelScript's implementation. It was a great learning resource, and while doing so I noticed this tiny small optimization.

I tried rewriting the code to not use any non volatile registers saving lots of pops and pushes. I did some small tests that called empty functions with 1 int and 4 int and 8 int and 1 float, 4 float and 8 float args and it seems to be faster by about 10%-15% but this is not representative of the real world as this is just calling an empty function it got slower at calling 0 arg functions since i removed the branch though but I don't think they are that common to special case.

Resources used:
https://learn.microsoft.com/en-us/cpp/build/x64-calling-convention?view=msvc-170#callercallee-saved-registers

@anjo76

anjo76 commented Aug 6, 2026

Copy link
Copy Markdown
Owner

Thanks for the contribution.

Did you actually notice any performance improvement with this optimization? 😄 While your change is accurate I doubt it really affected anything, as this is the type of optimization that the compiler would easily do by itself.

@anjo76 anjo76 self-assigned this Aug 6, 2026
@anjo76 anjo76 added the enhancement New feature or request label Aug 6, 2026
@ZXShady

ZXShady commented Aug 6, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the contribution.

Did you actually notice any performance improvement with this optimization? 😄 While your change is accurate I doubt it really affected anything, as this is the type of optimization that the compiler would easily do by itself.

It isn't strictly equal so the assembler optimizer cannot change it (also I don't think the assembler has any optimizations) , as it doesn't check The O flag (overflow) while jle does but as no overflow happens (it is a positive signed int) it is 1 less instruction for jle vs 2 seperate jumps.

Performance wise I did a small test but it was noise and I didn't notice anything but I had background apps running so. But for readability I think it is worth it.

@ZXShady

ZXShady commented Aug 7, 2026

Copy link
Copy Markdown
Contributor Author

I also tried rewriting the file to use no volatile registers, but I don't know whether it is readable using less instructions and stack in the process.

14f7521#diff-6bb1a1e324035a147c6f845c43b35e5a302837b28321eea42cf6124ed16fdb03

@anjo76

anjo76 commented Aug 7, 2026

Copy link
Copy Markdown
Owner

As long as the code comments explain at a high level what the assembly instructions are doing it should be OK.

@ZXShady

ZXShady commented Aug 7, 2026

Copy link
Copy Markdown
Contributor Author

Have you took a look at the diff?

Should I make a new pr for it or should I update this pr?

@ZXShady
ZXShady force-pushed the optimize_1_branch branch from 101488a to 4f1dd02 Compare August 8, 2026 07:00
@ZXShady ZXShady changed the title Optimize x64 1 conditional branch Rewrite msvc asm to not use non volatile registers Aug 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants