New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use lazy initialization and avoid static objects #2213
Conversation
…he order of initialization when linking statically.
src/DataStructures.cpp
Outdated
@@ -153,9 +154,16 @@ class ParameterInformation | |||
} | |||
}; | |||
|
|||
static ParameterInformation parameter_information; | |||
std::unique_ptr<ParameterInformation> parameter_information_p; | |||
ParameterInformation& get_parameter_information() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this be a const ref here?
src/DataStructures.cpp
Outdated
static PhaseInformation phase_information; | ||
|
||
std::unique_ptr<PhaseInformation> phase_information_p; | ||
PhaseInformation& get_phase_information() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same question as above
src/DataStructures.cpp
Outdated
static SchemeInformation scheme_information; | ||
|
||
std::unique_ptr<SchemeInformation> scheme_information_p; | ||
SchemeInformation& get_scheme_information() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
again
src/DataStructures.cpp
Outdated
@@ -541,9 +569,16 @@ class InputPairInformation | |||
} | |||
}; | |||
|
|||
static InputPairInformation input_pair_information; | |||
std::unique_ptr<InputPairInformation> input_pair_information_p; | |||
InputPairInformation& get_input_pair_information() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
etc.
src/DataStructures.cpp
Outdated
@@ -763,10 +800,17 @@ class BackendInformation | |||
} | |||
}; | |||
|
|||
static BackendInformation backend_information; | |||
std::unique_ptr<BackendInformation> backend_information_p; | |||
BackendInformation& get_backend_information() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
last one
I see the point; a similar approach was used to manage auto-registration of backends. Once we deal with the question about const ref, I think we can merge. |
The performance hit should only be a single check of the pointer, right? |
Thanks for reviewing the changes! |
Use lazy initialization and getter functions to avoid problems with the order of initialization when linking statically.
I have had some trouble with the functions that use global maps (get_param_information and friends) since they are not guaranteed to be initialised at runtime. In my case, some constants should be initialised early using these functions and I had mysterious crashes caused by empty maps.
This PR fixes that problem with almost no overhead.