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

Fix memory leak of Rules, use bool phony_ member to identify phony edges #2523

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jhasse
Copy link
Collaborator

@jhasse jhasse commented Nov 7, 2024

Not sure if this is worth it and what the performance impact is.

}

bool Rule::IsPhony() const {
return phony_;
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Can we just store the address of this Rule in State::phony_rule_ and implement State::IsPhony(const Rule* rule) { return rule == phony_rule_; } instead? This would avoid adding 8 bytes to each Rule instance due to padding.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

you mean like it was before? That doesn't work anymore since then the Rule would be deleted twice in std::map<std::string, std::unique_ptr<const Rule>> rules_;, wouldn't it?

Copy link
Contributor

Choose a reason for hiding this comment

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

Note necessarily with something like:

// state.h
struct State {
  ...
  const Rule* phony_rule() const { return phony_rule_; }
  
  Rule* phony_rule_ = nullptr;
  ...
};

// state.cc
State::State() {
  ...
  // Add the phony rule to it.
  phony_rule_ = new Rule("phony");
  bindings_.AddRule(phony_rule_);  // passes ownership to bindings_.
}

The caveat is that you need a State pointer whenever your want to implement rule->IsPhony(), but this is a minor modification to the rest of the code.

Frankly, this could go in a different PR. The memory leak fix is more important, so feel free to submit if you want.

@digit-google
Copy link
Contributor

Apart from my nit, this LGTM.

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.

2 participants