Skip to content

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.

@jhasse jhasse merged commit 6b5d7a7 into ninja-build:master Jan 15, 2025
11 checks passed
@jhasse jhasse deleted the dont-leak-rules branch January 15, 2025 18:40
@jhasse jhasse added this to the 1.13.0 milestone Jan 28, 2025
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