开发者

my Dll size blown out by introducing large switch cases, how can I reduce my Dll size(MSVC C++)?

开发者 https://www.devze.com 2023-02-10 21:47 出处:网络
My Dll size is of 2 MB. Now I have a situation where I need to add a switch case with 2500 case arms.. each case invoking a function. So total 2500 different functions are being called through this sw

My Dll size is of 2 MB. Now I have a situation where I need to add a switch case with 2500 case arms.. each case invoking a function. So total 2500 different functions are being called through this switch case with 2500 cases in it. (The entire code for 2500 functions is already exist in the DLL of 2MB. Extra code included is only switch cases). Now my issue is that the size of the Dll is increased by 15 MB in the Release build resulting DLL size into 17 MB which is huge as per my requirements. Please su开发者_JAVA技巧ggest me some way where i can maintain my Dll size to the minimum by handling switch cases properly.. any alternative methods..

I am using MSVC 2005, c/c++. I invoked optimization to minimize size(/01), /ltcg, (/OPT:REF), (/OPT:ICF),etc ..all best possible optimization features. (Not using precompiled headers)

Your suggestions are much needed

Thanks in Advance Anil


If all the functions have the same signature, consider populating a map or similar data structure with function pointers with what you're switching on as the lookup key. No human being is going to be able to understand a switch statement with 2500 cases, so I would recommend not having one in your code.


Almost certainly you have a lot of duplicated code in your case blocks (via macros / inline functions / STL). Change your case to use a non-inline function with arguments, to reduce each case block to merely "HandleGenericCase(x);". Don't use STL or other template/inlined code in your case blocks.

Without seeing your code, it will be hard to give a good answer in which case "verify the specific cause of bloat" is really the best advice.


Even if the switch statement takes 100 bytes per case, that is only 1/4 MB, and it is probably a lot less.

Can you get a map file of the dll? I would do that and just sample it at random a few times to see what kinds of functions are in there. My bet is it's full of stuff you don't really need, often generated by templates.

Also, I bet those 2500 functions contain a lot of gas. If the switch statement is the only way they are called, they could be expanded in-line right in the switch statement, saving a lot of entry/exit code.


With The intial trials i came to conclusion that theres nothing much when we try with Function pointers.. i found contiguous switch cases and function pointers are almost same as per my requirement where as maps are blowing my Dll size abit more. Here the issue is not with the switch cases as i thought but with the code that i have written in the functions that i am calling.. I have used many STLs, some common code.. and still fews things like strings.. that i am looking at which causing code bloat. It may take some time for me to make my code efficient. This Answer is to give an idea for beginners like me and a request for the experienced people to suggest some points where and how code can bloat.(Ofcourse I dont forget google) Still I am left with many loops and gaps .. I need to find what happens when code generated by the compiler.. its very interesting to know

0

精彩评论

暂无评论...
验证码 换一张
取 消