Methods that return constant values in C and C++ even if they come from variables, is it possible?

Asked

Viewed 78 times

1

In a series of optimizations in a code written in C++ adopted a class to store application parameterization data and some pseuconstants let’s say so.

What actually happens is that when the application initializes it gets in a file all parameters and window rendering information.

There is a certain point in the very critical application that should not take more than 30ms and that sequentially run because it generates 30 frames per second at most. What weighs most in this code is not dealing with such variables, but I don’t want to consume the resources with its manipulation since once loaded from the configuration file they won’t change.

I thought first-hand about using the volatile directive, but this is not the case, as they are neither variables nor constant that will be externally changed by other threads or external devices.

I thought to make a Casting during the obtaining of the data, but as said such information are obtained with certain frequency does not make effective the use of Casting.

Almost all variables are of type Std::string and integers.

The architecture used is Cortex-A Armv8 (Cortex-A53)

The compiler is GCC 4.8.2 (linux).

In summary I could say that outside this class all data will be as constants, but internally they are variables, when this class is initialized.

Source code of the config class

See for example the function variable "fullWindowName", once defined by reading the configuration file, will never be changed during execution.

https://gist.github.com/carlosdelfino/3d825370dcca03fea0db67cbe73c00b1

  • 1

    I did not understand what the difficulty is. In fact neither what it is doing and finding difficulty concretely. By my understanding, and I may have been mistaken, this is not possible and does not make sense.

  • It’s hard to answer a question like that without a proper example. Do you want to optimize access to a variable or something? I understood that the fact of being loaded from a file only prevents declaring the values in the code for optimization during compilation, but once the values loaded in the variables, what else needs to be improved? All I can do is volatile has nothing to do with it, it just serves prevent optimizations, and that if there is need to do cast every time you access, it’s because the type of the variable is probably declared wrong.

  • This question needs data to be answered. Without a clear example there is nothing to be done. Manipulating variables or constants should take exactly the same time, as they are only memory positions. This shows how much this question needs clarification. Have you ever used any performance verification tool to identify at what exact point of the code the loss of performance occurs? Usually programmers spend most of their time optimizing parts of the code they wouldn’t need.

  • Undoubtedly @Loudenvier, optimization takes a lot of time, almost as much as planning takes in relation to the execution of a project. In case I will post a question Link to the config class that I currently use, she is giant I know, are more than 500 lines in general, and still need a lot of adjustment.

  • Thanks @bigown, I’m almost convinced that it won’t really change much the adjustments to such a point, but by conscience disengagement I thought it best to ask.

  • @Andrésassi the big problem of casting is that some functions for example wait Std::string and others wait for const char*, being the same data. is a discrepancy between used libraries.

  • About the use of Volatile, it is common in microcontrollers or embedded systems that directly access a memory area and this is pointed to a variable that it is treated as constvolatile since the hardware will be responsible for updating itla, the code then must understand that it will be changed externally,

  • 1

    @Delfino, in the case of std::string for const char*, use the method c_str and the cost is nil for being inline (is just C++ syntax, does not generate any extra instruction in compiled code). The volatile serves to deactivate optimizations in relation to the variable, and therefore it is necessary for hardware registers, otherwise the compiler would throw out or reorder use of seemingly useless variables (because he is not aware of the hardware), and this seems to have no relation to the question. Please ask the question a reduced example that makes your problem evident.

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.