Questions tagged [compiler-optimization]

Compiler optimization involves adapting a compiler to reduce run-time or object size or both. This can be accomplished using compiler arguments (i.e. CFLAGS, LDFLAGS), compiler plugins (DEHYDRA for instance) or direct modifications to the compiler (such as modifying source code).

0
votes
0answers
27 views

reduce executable size mixing dynamically / static compilations

I am coding a project using c++ under visual c++ 2017 , but I would like to know if I could compile dinamically my project , and add #pragma comment(lib,"ws2_32") only once for all the project my ...
5
votes
1answer
73 views

Can compilers (specifically rustc) really simplify triangle-summation to avoid a loop? How?

On page 322 of Programming Rust by Blandy and Orendorff is this claim: ...Rust...recognizes that there's a simpler way to sum the numbers from one to n: the sum is always equal to n * (n+1) / 2. ...
19
votes
1answer
278 views

Why might a C++ compiler duplicate a function exit basic block?

Consider the following snippet of code: int* find_ptr(int* mem, int sz, int val) { for (int i = 0; i < sz; i++) { if (mem[i] == val) { return &mem[i]; } } ...
-1
votes
0answers
16 views

what protection mechanism has Microsoft Visual Studio added when compiling a c/C++ program? And what does its disassembly code shows?

Recently I am working on disassemble a program written by c++ using ms visual studio 2017. I can not even find the main entrance. There are so many jumps and junk codes. Can anyone explain what code ...
11
votes
2answers
269 views

Is there a flaw in how clang implements char8_t or does some dark corner of the standard prohibit optimization?

clang 8.0.0 introduces support for the char8_t type from c++20. However, I would expect the following functions to have the same compiler output #include <algorithm> bool compare4(char const* ...
4
votes
2answers
64 views

Can I rely on the compiler finding and optimizing simple boolean loop invariants?

I have a loop like the one below which has an invariant, here the never changing value of scaleEveryValueByTwo. Can I rely on the compiler finding this invariant and not checking the condition in ...
3
votes
2answers
49 views

Does JavaScript optimise multiple pure filters/maps/etc?

I have this JavaScript code written in functional style: someArray .filter((element) => element) .map((element) => element.property) .map((property) => doSomethingWithIt) Now, a naïve ...
3
votes
0answers
34 views

embed string via header that cannot be optimized away

While developing a header-only library, I'd like to make sure that a given string is embedded in all binaries that use my header, even if the compiler is configured to optimize away unused constants, ...
2
votes
1answer
93 views

Irritating performance decrease by a simple additional branch

In my project I have a function in which a code path should be conditionally skipped for performance reasons. If the condition is true, I have an increase of up to 50 % as expected. But if the ...
2
votes
2answers
65 views

Why do Compilers put data inside .text(code) section of the PE and ELF files and how does the CPU distinguish between data and code?

So i am referencing this paper : Binary Stirring: Self-randomizing Instruction Addresses of Legacy x86 Binary Code https://www.utdallas.edu/~hamlen/wartell12ccs.pdf Code interleaved with data: ...
2
votes
1answer
41 views

Different behavior when I running a program compiled with G ++ in Docker

The behavior of the executable is different if it is run inside the docker, or on the host. But this only happens when we change the optimization level of G++. Compiler: g++ (Ubuntu 7.3.0-27ubuntu1~...
1
vote
1answer
46 views

Optimization bug in Apple's LLVM, or bug in code?

I have some iOS C++ code that compiles correctly on my local machine (LLVM 9.0) but compiles incorrectly on my build server (LLVM 10.0). The project is generated via CMake (same version on both) so ...
1
vote
2answers
77 views

how can I get clang to vectorize a simple loop?

I have the following loop: float* s; float* ap; float* bp; ... // initialize s, ap, bp for(size_t i=0;i<64;++i) { s[i] = ap[i]+bp[i]; } Seems like a good candidate for vectorization. Though ...
-1
votes
2answers
49 views

The relevance of scopes in conjunction with compiler optimization

I have a library, which does heavy loop-unrolling on compile-time. I am also using a Bench-marking tool, which works by instantiating a struct inside a scope, and at destruction it measures the time ...
1
vote
0answers
37 views

Can I improve runtime by compiling multiple C++ classes into the same object file [duplicate]

I'm working on a large code base with thousands of small classes in individual header and source files which are compiled into a single shared object as follows g++ -o Class1.o Class1.cxx g++ -o ...
24
votes
3answers
1k views

List<T>.RemoveAll() efficiency / compiler optimisation

Regarding efficiency, does anyone know if the compiler is clever enough to not create the array containing 1, 3, 5 for each iteration of the loop in the following code? var foo = new List<int> {...
1
vote
2answers
88 views

Taking really long to compile my C++ code

I have a really long initializer list, something like this. std::vector<Inventory> inventory_list = {Inventory(0,30.0), Inventory(1,30.0), Inventory(2,30.0), Inventory(3,30.0), ...
4
votes
1answer
67 views

Traversal of Bounding Volume Hierachy in Shaders

I am working on a path tracer using vulkan compute shaders. I implemented a tree representing a bounding volume hierachy. The idea of the BVH is to minimize the amount of objects a ray intersection ...
0
votes
2answers
38 views

Is there some flags for gcc to avoid link with unused shared libraries?

echo "void main(){}" |gcc -xc -lm -lpthread -o test - && ldd test Here is an example. I don't use any math function in fact. But gcc still links with libm.so finally. Are there any ...
1
vote
0answers
34 views

How can I prettify my compiled binary of FFmpeg?

After many weeks and a lot of effort, I finally managed to use a Cygwin and MinGW-w64 toolchain to cross-compile a working binary of FFmpeg for Windows, complete with the superior but elusive fdk_aac ...
5
votes
1answer
154 views

Why doesn't this simple function get de-virtualized?

Consider the following code: struct A { virtual A& operator+=(const A& other) noexcept = 0; }; void foo_inner(int *p) noexcept { *p += *p; } void foo_virtual_inner(A *p) noexcept { *p += ...
0
votes
0answers
38 views

Cannot synchronize std::thread via std::vector [duplicate]

Code I've got an algorithm to run using threads. When several parts of the algorithm are coming to a specific step, I want the main thread to know this. I work under Xubuntu 18.04. Code of the main ...
0
votes
0answers
32 views

Disable compiler optimization with GCC 5.4.0 and RelWithDebInfo build type

When building this project in RelWithDebInfo build type, the compilation takes so much time that I just abort the process. There are some big source code (e.g.) and __attribute__((optimize("O0"))) is ...
-2
votes
2answers
56 views

Will a C++ compiler inline a for-loop with a small number of terms?

Suppose I have a class Matrix5x5 (with suitably overloaded index operators) and I write a method trace for calculating the sum of its diagonal elements: double Matrix5x5::trace(void){ double t(0....
0
votes
1answer
39 views

ANTLR doesn't recognize the wrong keyword

I'm noob in ANTLR. I'm building a compiler for a simple language, but I don't understand why, if I write the wrong keybord, the compiler doesn't give me the right error. That's my grammar: ...
1
vote
3answers
123 views

Will a C/C++ compiler optimise code by reusing a recently calculated function result?

Suppose I have a function double F(double x) and let's assume for the sake of this example that calls to F are costly. Suppose I write a function f that calculates the square root of F: double f(...
4
votes
1answer
193 views

How effectively can function-local lambdas be inlined by C++ compilers?

Background As an organizational strategy, I like to define function-local lambdas in complicated functions. It's good for encapsulating multi-step logic, repeated operations, etc. (the sorts of ...
2
votes
2answers
125 views

How to repeat a code segment without the use of function or class for high performance loop in C++

My program in C++11 is performing an online processing of serialized data and the loop need to run over millions of memory positions. Efficiency on the computation is a must and my concern is that by ...
0
votes
0answers
32 views

Code is being modified at AAR file creation Android

I have 2 projects, one of which I am creation an AAR file and combining it in my main project(implementing in the Gradle), when I debug I can go into the functions and see the implementation, some of ...
3
votes
3answers
125 views

Complex compiler output for simple constructor

I have a struct X with two 64-bit integer members, and a constructor: struct X { X(uint64_t a, uint64_t b) { a_ = a; b_ = b; } uint64_t a_, b_; }; When I look at the ...
0
votes
2answers
14 views

Sibling calls don’t appear in stack trace?

I just stumbled upon a line in the Wikipedia article on stack traces. It says: Sibling calls do not appear in a stack trace. What exactly does this mean? I thought all stack frames appeared in a ...
0
votes
0answers
40 views

Difference in tracking debug information in GCC and OpenJDK compilers

I noticed that when I compile code with GCC -Og, gdb is often unable to tell the value of variables during breakpoints because they are "optimized out". I know that information exists in some form as ...
3
votes
2answers
106 views

compiler optimization: move variable from stack to register

Here is the code: #include <cstring> #include <cstdint> #include <cstddef> uint64_t uint5korr(const std::byte *p) { uint64_t result= 0; std::memcpy(&result, p, 5); return ...
1
vote
0answers
29 views

Is it more effecient to use a class over an enum in typescript?

export class CarModels { public readonly Models: [] = ['chevy', 'ford']; } export enum CarModels { Models = 'chevy', 'ford' } what is more efficient for the typescript compiler and why? by ...
-2
votes
0answers
42 views

Does Golang compile unused method and/or functions?

I found this post claiming that Golang linker automatically disregards unused functions. The post seems to claim that methods are the same except that it's a bit more difficult for the compiler to ...
-1
votes
0answers
17 views

Optimize a compiler by keeping in memory the already compiled code that has not been modified

I'm looking to optimize my compiler's parser, and with the experience I've accumulated over the years, I understood (or rather guessed) that some compilers compiled code that was already compiled ...
4
votes
3answers
197 views

Cost of namespaces

What is the cost of defining namespaces in c++? By that I specifically mean compile-time/run-time increase, and/or memory footprint. Let's say I have trivial program with a header file and a .cpp with ...
2
votes
1answer
52 views

multiple access to bitfields without changing value, performance?

given a struct with bit-fields, for example : struct { long f0: 6; long f1: 6; long f2: 2; long f3: 2; } S; should there be a performance difference between the following codes: 1-...
-1
votes
2answers
65 views

Direct inclusion of template slower than separate instantiation

I have a simple template header containing 3 templated functions (no declarations, just definitions and marked static inline), two of these functions being 5000 lines long. These long functions are ...
10
votes
2answers
182 views

Does const allow for (theoretical) optimization here?

Consider this snippet: void foo(const int&); int bar(); int test1() { int x = bar(); int y = x; foo(x); return x - y; } int test2() { const int x = bar(); const int y = ...
-3
votes
1answer
85 views

Why does this if/else statement appear to be optimized away?

This is the code in question: void DeckTug::StickCallback(unsigned long long evtID, DWORD value) { long int val = value; if (evtID == stickXInputID || evtID == stickAxisXInputID) ...
0
votes
0answers
97 views

Why is my tail call-optimized implementation faster than the normal recursive if it's not supported in c#?

I recently got introduced to the concept of tail-call optimization and as far as I can read it's not inherently supported in the .NET compiler but there is a workaround called "Trampolining" which I'...
0
votes
0answers
12 views

Is there a way to disable -early-cse-memssa optimization in clang?

I've got a large C++ file that takes seconds to compile with clang -O0, but over 3 hours with clang -O1. Running with -ftime-report, I can see that over 90% of that time is spent in Early CSE w/memssa ...
2
votes
1answer
23 views

In GCC, what are “tree optimizations” vs “RTL optimizations”?

When I file a bug against GCC about a missed optimization, I am always at a loss whether I should file it under "rtl-optimizations" or "tree-optimizations". I looked at the components page on the GCC ...
1
vote
1answer
92 views

Why is '-lm' used explicitly only when passing variables to 'math.h' functions?

First of all, I have read this post Why do you need an explicit `-lm` compiler option & this gcc: why is the -lm flag needed to link the math library?. I wanna know why It doesn't happen in case ...
0
votes
1answer
39 views

Table of optimization levels of the GNU C++ compiler g++, accurate?

Although I know each and every program is a different scenario, I have a rather specific question considering the below table. Optimization levels of the GNU C++ compiler g++ Ox WHAT IS BEING ...
1
vote
0answers
59 views

gcc and clang not willing to lazily initialize a partially-used struct

Consider the following code: __attribute__((noinline)) int foo1(int x, int y) { return x; } int bar1(int* a) { int b = foo1(a[5], a[10]); return b * b; } Even though foo1 is not inlined,...
2
votes
0answers
64 views

Optimizing R code to decrease iteration time

I have written a code to do a numerical optimization for three parameters. My problem is that the code uses 1h30mins for one iteration, and the optimization do not converge untill 150-200 iterations. ...
0
votes
0answers
32 views

C# Jitter: Read Introduction [duplicate]

If you have a construct like this, it seems possible according to the linked article (https://msdn.microsoft.com/en-us/magazine/jj883956.aspx paragraph Read Introduction) ... var temp = myRef; temp....
0
votes
0answers
30 views

let data = data; to change mutability [duplicate]

I am sometimes tempted to write *** fn main() { // Initialize as mutable. let mut data = String::new(); // ... data.push_str("data progressively built up"); // ... // Move ...