Skip to content
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

Merged
merged 6 commits into from Nov 29, 2023

Conversation

jowr
Copy link
Member

@jowr jowr commented Feb 7, 2023

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.

…he order of initialization when linking statically.
@@ -153,9 +154,16 @@ class ParameterInformation
}
};

static ParameterInformation parameter_information;
std::unique_ptr<ParameterInformation> parameter_information_p;
ParameterInformation& get_parameter_information() {
Copy link
Contributor

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?

static PhaseInformation phase_information;

std::unique_ptr<PhaseInformation> phase_information_p;
PhaseInformation& get_phase_information() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same question as above

static SchemeInformation scheme_information;

std::unique_ptr<SchemeInformation> scheme_information_p;
SchemeInformation& get_scheme_information() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

again

@@ -541,9 +569,16 @@ class InputPairInformation
}
};

static InputPairInformation input_pair_information;
std::unique_ptr<InputPairInformation> input_pair_information_p;
InputPairInformation& get_input_pair_information() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

etc.

@@ -763,10 +800,17 @@ class BackendInformation
}
};

static BackendInformation backend_information;
std::unique_ptr<BackendInformation> backend_information_p;
BackendInformation& get_backend_information() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

last one

@ibell
Copy link
Contributor

ibell commented Feb 8, 2023

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.

@ibell
Copy link
Contributor

ibell commented Feb 8, 2023

The performance hit should only be a single check of the pointer, right?

@jowr
Copy link
Member Author

jowr commented Mar 13, 2023

Thanks for reviewing the changes!

@jowr jowr added this to the v6.5.1 milestone Nov 29, 2023
@jowr jowr merged commit ce1277f into master Nov 29, 2023
1 check passed
@jowr jowr modified the milestones: v6.5.1, v6.6.0 Nov 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants