Skip to content

Commit

Permalink
add rank by size optimization + bump sea-orm-cli
Browse files Browse the repository at this point in the history
This commit adds the rank by size optimization into the embedded
union-find data structure of the group sets. It also bumps the version
number of `sea-orm-cli` to `1.1.2`.
  • Loading branch information
connortsui20 committed Dec 9, 2024
1 parent 4d58221 commit 32b3cbe
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 16 deletions.
2 changes: 1 addition & 1 deletion optd-mvp/src/entities/fingerprint.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.0
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.2
use sea_orm::entity::prelude::*;

Expand Down
3 changes: 2 additions & 1 deletion optd-mvp/src/entities/group.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.0
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.2
use sea_orm::entity::prelude::*;

Expand All @@ -10,6 +10,7 @@ pub struct Model {
pub status: i8,
pub winner: Option<i32>,
pub cost: Option<i64>,
pub set_size: i32,
pub parent_id: Option<i32>,
pub next_id: Option<i32>,
}
Expand Down
2 changes: 1 addition & 1 deletion optd-mvp/src/entities/logical_children.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.0
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.2
use sea_orm::entity::prelude::*;

Expand Down
2 changes: 1 addition & 1 deletion optd-mvp/src/entities/logical_expression.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.0
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.2
use sea_orm::entity::prelude::*;

Expand Down
2 changes: 1 addition & 1 deletion optd-mvp/src/entities/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.0
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.2
pub mod prelude;

Expand Down
2 changes: 1 addition & 1 deletion optd-mvp/src/entities/physical_children.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.0
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.2
use sea_orm::entity::prelude::*;

Expand Down
2 changes: 1 addition & 1 deletion optd-mvp/src/entities/physical_expression.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.0
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.2
use sea_orm::entity::prelude::*;

Expand Down
2 changes: 1 addition & 1 deletion optd-mvp/src/entities/prelude.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.0
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.2
#![allow(unused_imports)]

Expand Down
23 changes: 15 additions & 8 deletions optd-mvp/src/memo/persistent/implementation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,7 @@ where
// The expression does not exist yet, so we need to create a new group and new expression.
let group = group::ActiveModel {
status: Set(0), // `GroupStatus::InProgress` status.
set_size: Set(1),
..Default::default()
};

Expand Down Expand Up @@ -676,18 +677,24 @@ where
left_group_id: GroupId,
right_group_id: GroupId,
) -> OptimizerResult<GroupId> {
// Without a rank / size field, we have no way of determining which set is better to merge
// into the other. So we will arbitrarily choose to merge the left group into the right
// group here. If rank is added in the future, then merge the smaller set into the larger.
let mut left_root_id = self.get_root_group(left_group_id).await?;
let mut left_root = self.get_group(left_root_id).await?;
let mut left_size = left_root.set_size;

let mut right_root_id = self.get_root_group(right_group_id).await?;
let mut right_root = self.get_group(right_root_id).await?;
let mut right_size = left_root.set_size;

// Rank/size optimization: merge the smaller set into the larger set.
if left_size > right_size {
std::mem::swap(&mut left_root_id, &mut right_root_id);
std::mem::swap(&mut left_root, &mut right_root);
std::mem::swap(&mut left_size, &mut right_size);
}

let left_root_id = self.get_root_group(left_group_id).await?;
let left_root = self.get_group(left_root_id).await?;
// A `None` next pointer means it should technically be pointing to itself.
let left_next = left_root.next_id.unwrap_or(left_root_id.0);
let mut active_left_root = left_root.into_active_model();

let right_root_id = self.get_root_group(right_group_id).await?;
let right_root = self.get_group(right_root_id).await?;
// A `None` next pointer means it should technically be pointing to itself.
let right_next = right_root.next_id.unwrap_or(right_root_id.0);
let mut active_right_root = right_root.into_active_model();
Expand Down
2 changes: 2 additions & 0 deletions optd-mvp/src/migrator/memo/m20241127_000001_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ pub enum Group {
Status,
Winner,
Cost,
SetSize,
ParentId,
NextId,
}
Expand All @@ -109,6 +110,7 @@ impl MigrationTrait for Migration {
.on_delete(ForeignKeyAction::SetNull)
.on_update(ForeignKeyAction::Cascade),
)
.col(integer(Group::SetSize))
.col(integer_null(Group::ParentId))
.foreign_key(
ForeignKey::create()
Expand Down

0 comments on commit 32b3cbe

Please sign in to comment.