compiler-optimization

break condition to OR and AND operators in an IF Statement

佐手、 提交于 2021-02-20 17:03:00
问题 The If statement and any other boolean comparison is smart enought to stop at first FALSE value when evaluating A and B and C and D and at first TRUE value when evaluating A or B or C or D . What is the name of this behavior? Is this a compiler optimization? If so, there is a way to disable it with some compiler directive? 回答1: This is called 'boolean short-circuit evaluation', a form of 'lazy evaluation'. You can tell the compiler either to use or not to use this feature using compiler

break condition to OR and AND operators in an IF Statement

青春壹個敷衍的年華 提交于 2021-02-20 17:00:10
问题 The If statement and any other boolean comparison is smart enought to stop at first FALSE value when evaluating A and B and C and D and at first TRUE value when evaluating A or B or C or D . What is the name of this behavior? Is this a compiler optimization? If so, there is a way to disable it with some compiler directive? 回答1: This is called 'boolean short-circuit evaluation', a form of 'lazy evaluation'. You can tell the compiler either to use or not to use this feature using compiler

GCC placing register args on the stack with a gap below local variables?

烂漫一生 提交于 2021-02-19 06:22:28
问题 I tried to look at the assembly code for a very simple program. int func(int x) { int z = 1337; return z; } With GCC -O0, every C variable has a memory address that's not optimized away, so gcc spills its register arg: (Godbolt, gcc5.5 -O0 -fverbose-asm) func: pushq %rbp # movq %rsp, %rbp #, movl %edi, -20(%rbp) # x, x movl $1337, -4(%rbp) #, z movl -4(%rbp), %eax # z, D.2332 popq %rbp # ret What is the reason that the function parameter x gets placed on the stack below the local variables?

GCC placing register args on the stack with a gap below local variables?

为君一笑 提交于 2021-02-19 06:22:26
问题 I tried to look at the assembly code for a very simple program. int func(int x) { int z = 1337; return z; } With GCC -O0, every C variable has a memory address that's not optimized away, so gcc spills its register arg: (Godbolt, gcc5.5 -O0 -fverbose-asm) func: pushq %rbp # movq %rsp, %rbp #, movl %edi, -20(%rbp) # x, x movl $1337, -4(%rbp) #, z movl -4(%rbp), %eax # z, D.2332 popq %rbp # ret What is the reason that the function parameter x gets placed on the stack below the local variables?

Javac missing optimization for effective final

百般思念 提交于 2021-02-19 01:36:30
问题 Fact: javac is programmed to detect if a variable is final or if it can be treated as effectively final . Proof: This code illustrates this. public static void finalCheck() { String str1 = "hello"; Runnable r = () -> { str1 = "hello"; }; } This fails to compile because compiler is able to detect String reference str1 is being re-assigned in function. Now Situation 1: Javac does great optimization for final String instances by avoiding to create StringBuilder and related operations. Proof This

Why is using structure Vector3I instead of three ints much slower in C#?

主宰稳场 提交于 2021-02-18 21:43:34
问题 I'm processing lots of data in a 3D grid so I wanted to implement a simple iterator instead of three nested loops. However, I encountered a performance problem: first, I implemented a simple loop using only int x, y and z variables. Then I implemented a Vector3I structure and used that - and the calculation time doubled. Now I'm struggling with the question - why is that? What did I do wrong? Example for reproduction: using BenchmarkDotNet.Attributes; using BenchmarkDotNet.Running; using

Why is using structure Vector3I instead of three ints much slower in C#?

十年热恋 提交于 2021-02-18 21:42:26
问题 I'm processing lots of data in a 3D grid so I wanted to implement a simple iterator instead of three nested loops. However, I encountered a performance problem: first, I implemented a simple loop using only int x, y and z variables. Then I implemented a Vector3I structure and used that - and the calculation time doubled. Now I'm struggling with the question - why is that? What did I do wrong? Example for reproduction: using BenchmarkDotNet.Attributes; using BenchmarkDotNet.Running; using

Java Compiler replacing StringBuilder with + concatenation

ぐ巨炮叔叔 提交于 2021-02-18 13:39:51
问题 Here's some simple Java code: String s = new StringBuilder().append("a").append("b").append("c").toString(); I compile it with JRE 1.6, and I observe the following in the decompiled class file: String s = "a" + "b" + "c"; I had the following questions: Why does the compiler choose '+' over StringBuilder? Do we have any official Java specification that justifies this behavior? Does it really make sense to use StringBuilder in such cases, where we know compiler is going to change it anyway? 回答1

Java Compiler replacing StringBuilder with + concatenation

梦想与她 提交于 2021-02-18 13:39:07
问题 Here's some simple Java code: String s = new StringBuilder().append("a").append("b").append("c").toString(); I compile it with JRE 1.6, and I observe the following in the decompiled class file: String s = "a" + "b" + "c"; I had the following questions: Why does the compiler choose '+' over StringBuilder? Do we have any official Java specification that justifies this behavior? Does it really make sense to use StringBuilder in such cases, where we know compiler is going to change it anyway? 回答1

How can I disable constant folding in Perl?

十年热恋 提交于 2021-02-10 14:14:41
问题 Given a command like, perl -MO=Deparse -E'use constant FOO => 42; print FOO()' How can I disable constant folding such that print 42; Shows me print FOO(); Or the like. Ideally, I would like this to be a compiler option that works for all of Perl . You can see this talked about in this thread on the perl mailing list, [perl #97942] [PATCH] Add -DO option to disable optimizations and disable constant folding and the peephole optimizer when used.. I tried -DO and it didn't work. If that option