由于引入大型开关盒,我的 Dll 大小已经过大,如何减小我的 Dll 大小(MSVC C++)?
我的 Dll 大小为 2 MB。现在我遇到的情况是,我需要添加一个带有 2500 个案例臂的开关案例。每个案例都调用一个函数。因此,通过这个包含 2500 个案例的 switch case,总共调用了 2500 个不同的函数。 (2MB的DLL中已经存在2500个函数的全部代码。额外包含的代码只是switch case)。现在我的问题是,在发布版本中,Dll 的大小增加了 15 MB,导致 DLL 大小变为 17 MB,这对于我的要求来说是巨大的。请建议我一些方法,通过正确处理开关案例,我可以将 Dll 大小保持在最小..任何替代方法..
我正在使用 MSVC 2005,c/c++。我调用优化来最小化大小(/01)、/ltcg、(/OPT:REF)、(/OPT:ICF)等..所有可能的最佳优化功能。 (不使用预编译头)
非常需要您的建议,
提前致谢 阿尼尔
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 suggest 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果所有函数都具有相同的签名,请考虑使用您要打开的函数指针作为查找键来填充
map
或类似的数据结构。没有人能够理解包含 2500 个案例的 switch 语句,因此我建议您的代码中不要包含这样的语句。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.几乎可以肯定,您的 case 块中有很多重复的代码(通过宏/内联函数/STL)。将您的 case 更改为使用带有参数的非内联函数,将每个 case 块减少为仅“HandleGenericCase(x);”。不要在 case 块中使用 STL 或其他模板/内联代码。
如果没有看到你的代码,就很难给出一个好的答案,在这种情况下“验证膨胀的具体原因”确实是最好的建议。
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.
即使 switch 语句每种情况占用 100 个字节,也只有 1/4 MB,而且可能要少得多。
你能得到dll的映射文件吗?我会这样做,然后随机采样几次,看看里面有什么类型的函数。我敢打赌,它充满了你并不真正需要的东西,通常是由模板生成的。
另外,我敢打赌这 2500 个函数包含大量气体。如果 switch 语句是调用它们的唯一方式,则可以在 switch 语句中直接内联扩展它们,从而节省大量入口/出口代码。
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.
通过初步试验,我得出的结论是,当我们尝试使用函数指针时,没有什么太大的作用。我发现连续的 switch 情况和函数指针几乎与我的要求相同,而映射则使我的 Dll 大小变得更大。这里的问题不是像我想象的那样是 switch case 而是我在调用的函数中编写的代码..我使用了很多 STL,一些通用代码..还有一些像字符串这样的东西..我我正在寻找导致代码膨胀的原因。我可能需要一些时间才能使我的代码变得高效。这个答案是为像我这样的初学者提供一个想法,并请求有经验的人提出一些代码膨胀的点和方式。(当然我不会忘记谷歌)但我仍然留下了许多循环和差距..我需要看看编译器生成代码时会发生什么......这很有趣
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