Compiling with -Wsuggest-attribute=pure
makes GCC suggest potential functions that can be marked with __attribute__ ((pure))
for optimization purposes.
Here's the definition of pure
on GCC's documentation:
Many functions have no effects except the return value and their return value depends only on the parameters and/or global variables. Such a function can be subject to common subexpression elimination and loop optimization just as an arithmetic operator would be. These functions should be declared with the attribute pure.
I'm creating a small game engine, where I have an input_context
class that contains an input_state
member. The input_context
class updates the input_state
member every frame by getting the global input state from the operating system.
It also contains several "getters" to query the input state.
Simplified example:
class input_context
{
private:
input_state _input_state;
public:
void update()
{
os::fill_input_state(_input_state);
}
auto mouse_x() const noexcept
{
return _input_state._mouse_x;
}
auto mouse_y() const noexcept
{
return _input_state._mouse_y;
}
auto is_key_down(keycode k) const noexcept
{
// `_keys` is an array of `bool` values.
return _input_state._keys[k];
}
};
GCC is telling me that all these "getter methods", like mouse_x()
, mouse_y()
and is_key_down()
, are candidates for __attribute__ ((pure))
.
Should I mark these methods as pure
?
I don't think so, but GCC's suggestion makes me wonder about it.
I'm not sure how to interpret GCC's definition of pure
- it says that function who rely only on the parameters and/or global variables should be marked as such.
In a way, the global OS input state could be interpreted as a global variable.
On the other hand, the "getter methods" always return different values, depending on the
_input_state
member variable.