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

refactor(optimizer): use Cell instead of RefCell for interior mutability #19883

Merged
merged 4 commits into from
Dec 23, 2024

Conversation

stdrc
Copy link
Member

@stdrc stdrc commented Dec 20, 2024

I hereby agree to the terms of the RisingWave Labs, Inc. Contributor License Agreement.

What's changed and what's your intention?

In OptimizerContext, we have some fields representing the last assigned optimizer element IDs, e.g. plan node ID. Previously we use RefCell for these fields, but what we actually want is just interior mutability, not runtime borrow checking. This PR changes these fields to use Cell instead.

Checklist

  • I have written necessary rustdoc comments.
  • I have added necessary unit tests and integration tests.
  • I have added test labels as necessary.
  • I have added fuzzing tests or opened an issue to track them.
  • My PR contains breaking changes.
  • My PR changes performance-critical code, so I will run (micro) benchmarks and present the results.
  • My PR contains critical fixes that are necessary to be merged into the latest release.

Documentation

  • My PR needs documentation updates.
Release note

Signed-off-by: Richard Chien <[email protected]>
…ilar to correct the semantics

Signed-off-by: Richard Chien <[email protected]>
Signed-off-by: Richard Chien <[email protected]>
Copy link
Member Author

stdrc commented Dec 20, 2024

This stack of pull requests is managed by Graphite. Learn more about stacking.

@stdrc stdrc changed the title reorder refactor(optimizer): use Cell instead of RefCell for Dec 20, 2024
@stdrc stdrc changed the title refactor(optimizer): use Cell instead of RefCell for refactor(optimizer): use Cell instead of RefCell for interior mutability Dec 20, 2024
@stdrc stdrc marked this pull request as ready for review December 20, 2024 09:25
Comment on lines +159 to +169
/// This should only be called in [`crate::optimizer::plan_node::reorganize_elements_id`].
pub(in crate::optimizer) fn reset_elem_ids(&self) {
self.last_plan_node_id.set(0);
self.last_correlated_id.set(0);
self.last_expr_display_id.set(0);
}

pub fn next_correlated_id(&self) -> CorrelatedId {
*self.next_correlated_id.borrow_mut() += 1;
*self.next_correlated_id.borrow()
pub(in crate::optimizer) fn restore_elem_ids(&self, backup: LastAssignedIds) {
self.last_plan_node_id.set(backup.last_plan_node_id);
self.last_correlated_id.set(backup.last_correlated_id);
self.last_expr_display_id.set(backup.last_expr_display_id);
Copy link
Member

Choose a reason for hiding this comment

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

What about just inlining these functions info reorganize_elements_id?

Copy link
Member Author

Choose a reason for hiding this comment

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

When adding a new id later, we can hardly remember that there's a reorganize_elements_id far away need to be updated. So I decided to hide all the ids inside LastAssignedIds.

Copy link
Member

Choose a reason for hiding this comment

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

I don't get you. We can still add an id in OptimizerContext but not in LastAssignedIds

Copy link
Member

Choose a reason for hiding this comment

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

Maybe you should replace the fields to LastAssignedIds in OptimizerContext

Copy link
Member Author

@stdrc stdrc Dec 23, 2024

Choose a reason for hiding this comment

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

The fact is that we missed last_correlated_id in reorganize_elements_id before. I want to reduce the possibility of future error, so I moved them to LastAssignedIds and put it right next to OptimizerContext. I also definitely considered directly using LastAssignedIds as a field in OptimizerContext, but IMO it's too annoying to access these ID fields via a long self.last_assigned_ids.last_plan_node_id. After all forgetting to add new id field to LastAssignedIds is not a critical issue, we just need to prevent it to a proper degree.

This PR doesn't aim to completely refactor the optimizer element IDs, otherwise I would extract an IdAllocator struct to allocate IDs instead of self.last_plan_node_id.update in OptimizerContext. Also I don't think use interior mutability here in OptimizerContext is a good idea. But I don't want to do that in this PR. It's just a quick fix to unblock my future work on other things.

@stdrc stdrc added this pull request to the merge queue Dec 23, 2024
github-merge-queue bot pushed a commit that referenced this pull request Dec 23, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Dec 23, 2024
@stdrc stdrc added this pull request to the merge queue Dec 23, 2024
@@ -255,12 +268,12 @@ impl std::fmt::Debug for OptimizerContext {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
Copy link
Member

Choose a reason for hiding this comment

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

Use .debug_struct?

Merged via the queue into main with commit 02b5309 Dec 23, 2024
35 of 37 checks passed
@stdrc stdrc deleted the rc/use-cell-instead-of-refcell branch December 23, 2024 05:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants