我很好奇是否有任何工具为此提供部分解决方案.由于performSelector,这是一个棘手的问题...但是工具应该至少能够提出候选人,使人类的工作变得更容易.
tl;dr:能否以某种方式确保(例如通过编写单元测试)某些东西被优化掉,例如整个循环?
确保生产版本中不包含某些内容的常用方法是将其包装为#if...#endif. 但我更喜欢继续使用 C++ 机制。即使在那里,我也喜欢保持简单的实现,而不是复杂的模板专业化,并认为“嘿,编译器无论如何都会优化它”。
上下文是汽车中的嵌入式软件(二进制大小很重要),编译器通常很差。它们在安全意义上得到了认证,但通常在优化方面表现不佳。
示例 1:在容器中,元素的销毁通常是一个循环:
for(size_t i = 0; i<elements; i++)
buffer[i].~T();
Run Code Online (Sandbox Code Playgroud)
这也适用于内置类型,例如int,因为标准允许显式调用任何标量类型的析构函数 (C++11 12.4-15)。在这种情况下,循环不执行任何操作并被优化掉。在 GCC 中是这样,但在另一个(Aurix)中不是,我在反汇编中看到了一个字面上的空循环!因此需要模板专门化来修复它。
示例 2:代码,仅用于调试、分析或故障注入等:
constexpr bool isDebugging = false; // somehow a global flag
void foo(int arg) {
if( isDebugging ) {
// Albeit 'dead' section, it may not appear in production binary!
// (size, security, safety...)
// 'if constexpr..' not an option (C++11)
std::cout << "Arg was " << arg << std::endl;
}
// …Run Code Online (Sandbox Code Playgroud) 有人请告诉我为什么我在其他分支中收到死代码警告if (projectId != null)?如果我做对了,翻译认为projectId永远不会是空的 - 是吗?在我看来,这是不可能的......
Integer projectId = null;
if (!sprintTaskConnections.isEmpty())
projectId = sprintTaskConnections.get(0).getProjectId();
// init name, state, startDate, endDate here
JiraSprint sprint = new JiraSprint(sprintInfo.getInt("id"), name, state, projectId, startDate, endDate);
if (projectId != null)
{
...
}
Run Code Online (Sandbox Code Playgroud)
即使我放了一个
sprintTaskConnections.add(new JiraSprintTaskConnection(1, 1, 1));
Run Code Online (Sandbox Code Playgroud)
或者a
sprintTaskConnections.clear();
Run Code Online (Sandbox Code Playgroud)
在...前面
if (!sprintTaskConnections.isEmpty())
projectId = sprintTaskConnections.get(0).getProjectId();
Run Code Online (Sandbox Code Playgroud)
结果总是一样的!
请帮助我,我现在还没有得到它!
我正在寻找一种方法来检查我的C项目,编译成ELF,是否有未使用的函数,并找到它们.这是声明的函数,但在我的代码中没有被调用.
解决方案可以是以下之一:
解决方案不能是以下之一:
gprof(有些函数需要几天才能调用它们,但在代码流中你可以看到它们最终被调用)谢谢
我的iOS项目用于dlsym动态指向可选的C库。没有该项目的可选as-in可以随我们一起运行,它只是添加了功能。
有关背景信息:在运行时在Objective-C中检测并使用可选的外部C库
问题是,XCode清除了“未使用”的库。使用dlsym方法没有直接引用我的第三方库,XCode删除了它。
我以为自己在“其他链接器标记”中找到了解决方案,在
-force_load "$(SRCROOT)/my_external.a"模拟器中效果很好。(-all_load效果也不错,但对我来说似乎太过分了)。
问题是当我移至真实设备时,此解决方法失败,并且库未加载(与相同-all_load)。
唯一起作用的是在XCode中禁用名为的功能Dead Code Stripping。
问题是:禁用或建议我的客户禁用此功能真的不好吗?如果是这样,还有更好的选择吗?
我对链接器如何消除未使用的函数和数据段的死代码有一个初步的了解。如果您使用正确的编译器和链接器标志,它将每个函数和数据成员放入其自己的部分中,那么当链接器链接它们时,它将看到,如果不直接引用,则没有任何链接到该部分,然后它将不会链接那一段进入最后的精灵。
我正在尝试协调它与函数指针的工作方式。例如,您可以有一个函数指针,其值基于用户输入。这样做可能不安全,但是编译器和链接器将如何处理这个问题?
我遇到了问题,我在Eclipse中收到了"死代码"警告,我真的不知道为什么.代码来自我的Connect Four项目,更确切地说,它来自Class,它检查是否有人获胜.此方法检查红色的所有水平获胜可能性.代码如下:
/**
* Method to check the horizontal winning possibilities for red
* @return true if red won or false if not
*/
public boolean checkHorRed(){
for(int line = 0; line < 6; line++) {
for(int column = 0; column < 4; column++) { //column++ is underlined and causes the "dead Code" warning
if(gw.buttons[line][column].getIcon().equals(gw.red));
if(gw.buttons[line][column+1].getIcon().equals(gw.red));
if(gw.buttons[line][column+2].getIcon().equals(gw.red));
if(gw.buttons[line][column+3].getIcon().equals(gw.red));
return true;
}
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
由于这种方法,甚至可以立即赢得比赛.这有点奇怪的是,类中看起来几乎相同的所有其他方法都不会导致任何问题.以下是检查黄色垂直获胜可能性的方法,以进行比较:
/**
* Method to check the vertical winning possibilities for yellow
* @return true …Run Code Online (Sandbox Code Playgroud) 我正在编写一个程序来解决类似sudoko的难题Hashiwokakero。我有一些看起来像这样的代码:
if (bridgesLeft[row][col] == 1)
{
doSomething();
}
else if (bridgesLeft[row][col] == 2)
{
doSomethingElse();
}
else if (bridgesLeft[row][col] == 3)
{
doAnotherThing();
}
...
Run Code Online (Sandbox Code Playgroud)
我意识到我在doSomethingElse()函数中放了一个错误,因此我没有删除该块,而是添加else if (bridgesLeft[row][col] == 2 && false)了保证该错误功能不会运行的信息,只是要确保那是我的错误来源。Xcode给我警告,说我的doSomethingElse()代码永远不会运行。它还给了我这个选择:
fix-it: Silence by adding parentheses to mark code as explicitly dead.
Run Code Online (Sandbox Code Playgroud)
单击此按钮会更改
else if (bridgesLeft[row][col] == 2 && false)
Run Code Online (Sandbox Code Playgroud)
至
else if (bridgesLeft[row][col] == /* DISABLES CODE */ (2) && false)
Run Code Online (Sandbox Code Playgroud)
如何将括号内的“ 2”标记为明显已死?这是什么意思?如果我保留括号,但取出&& false一部分,代码块仍会执行,因此实际上并没有使代码失效。
I am attempting to carry out liveness analysis and in order to do so I need to obtain the def and use sets for all of my nodes, where they are defined as follows:
def[n] = set of all variables defined at node n
use[n] = set of all variables used at node n
So for example in the line:
a = b + c
def[n] = {a}
use[n] = {b,c}
Run Code Online (Sandbox Code Playgroud)
How can I do this?
假设 V8 将完全消除死代码(结构如下)是否正确?
export const DEBUG = false
Run Code Online (Sandbox Code Playgroud)
import { DEBUG } from './module1.js'
if (DEBUG) {
// dead code eliminated?
}
Run Code Online (Sandbox Code Playgroud)
请不要评论“‘if’检查的开销非常小,你应该 XXX 而不是问这个问题”。我只是想知道 V8 是否能够做到这一点(是/否,当然最好有一些细节)。
dead-code ×10
c ×3
c++ ×3
xcode ×3
gcc ×2
ios ×2
java ×2
c++11 ×1
eclipse ×1
javascript ×1
llvm ×1
objective-c ×1
performance ×1
unit-testing ×1
utility ×1
v8 ×1