microsoft / Detours

Detours is a software package for monitoring and instrumenting API calls on Windows. It is distributed in source code form.
MIT License
5.02k stars 981 forks source link

Reduce the executable code size. #191

Open jdp1024 opened 3 years ago

jdp1024 commented 3 years ago

In the original version of Detours, s_rceCopyTable and s_rceCopyTable0F are two big arrays, both consisting of 256 COPYENTRY's, occupying 256(8+8)2=8192 bytes. To reduce the executable code size, those two arrays are changed to use the index to a COPYENTRY array instead of the actual entity.

s_rceCopyTable and s_rceCopyTable0F now are arrays of 256 BYTEs, the COPYENTRY array consists of only the 45 unique operands. All these occupy 2562+45(8+8)=1232 bytes, thus we save 7060 bytes with almost no performance sacrifice.

Microsoft Reviewers: Open in CodeFlow
ghost commented 3 years ago

CLA assistant check
All CLA requirements met.

bgianfo commented 3 years ago

@jaykrell I think you were working on this code in the recent past? Do you have any thoughts on this?

jaykrell commented 3 years ago

It is a micro optimization but yes makes sense just from the description. There are 512 multi-word entries, composed of only a few unique values.

jaykrell commented 3 years ago

I should point out. I changed all of this code so the main handler function is templatized, and the table is filled with (mostly?) instantiated pointers to it. So that ends up around 512*8=2k and perhaps faster, perhaps a little larger. That version shipped in a product. Those could also be compressed in your way, to the 512 bytes plus a little extra, and then even smaller tables than yours, but larger code. Galen's response at the time though, was that Detour's users do not understand C++ templates, so do not use them.

AtariDreams commented 1 year ago

Any updates on this?