Skip to content

Commit

Permalink
VM refactor phase 1
Browse files Browse the repository at this point in the history
Moves mutable state off the VM itself, and into a passed-in 'state' parameter.
  • Loading branch information
rdaum committed Jan 6, 2024
1 parent ed7de2d commit 555787e
Show file tree
Hide file tree
Showing 18 changed files with 765 additions and 679 deletions.
11 changes: 5 additions & 6 deletions crates/db/src/tuplebox/tuples/slotbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,7 @@ impl SlotBox {
allocator
.available_page_space
.iter()
.map(|(_, ps)| ps.pages())
.flatten()
.flat_map(|(_, ps)| ps.pages())
.collect()
}
}
Expand Down Expand Up @@ -384,7 +383,7 @@ impl Inner {
}

// Out of room, need to allocate a new page.
return self.alloc(relation_id, page_size);
self.alloc(relation_id, page_size)
}

fn finish_alloc(
Expand Down Expand Up @@ -489,7 +488,7 @@ impl PageSpace {
.entries
.binary_search_by(|entry| decode(*entry).1.cmp(&available));

return match found {
match found {
// Exact match, highly unlikely, but possible.
Ok(entry_num) => {
// We only want the lower 64 bits, ala
Expand All @@ -500,14 +499,14 @@ impl PageSpace {
// Out of room, our caller will need to allocate a new page.
Err(position) if position == self.entries.len() => {
// If we didn't find a page with enough space, then we need to allocate a new page.
return None;
None
}
// Found a page we add to.
Err(entry_num) => {
let pid = self.entries[entry_num] as u64;
Some((pid as PageId, entry_num))
}
};
}
}

fn finish(&mut self, offset: usize, page_remaining_bytes: usize) {
Expand Down
2 changes: 1 addition & 1 deletion crates/db/src/tuplebox/tuples/slotted_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ impl<'a> SlottedPage<'a> {

// Add the index entry and expand the index region.
let mut index_entry = self.get_index_entry_mut(self.header_mut().num_slots as SlotId);
index_entry.as_mut().alloc(content_position as usize, size);
index_entry.as_mut().alloc(content_position, size);

// Update the header
let header = self.header_mut();
Expand Down
2 changes: 1 addition & 1 deletion crates/db/src/tuplebox/tx/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl Transaction {
let mut working_set = self.working_set.write().await;
let commit_set = self
.db
.prepare_commit_set(commit_ts, &working_set.as_ref().unwrap())
.prepare_commit_set(commit_ts, working_set.as_ref().unwrap())
.await?;
match self.db.try_commit(commit_set).await {
Ok(_) => {
Expand Down
2 changes: 1 addition & 1 deletion crates/db/src/tuplebox/tx/working_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ impl WorkingSet {
// By performing the seek, we'll materialize the tuples into our local working set, which
// will in turn update the codomain index for those tuples.
for tuple in tuples_for_codomain {
let _ = self.seek_by_domain(&db, relation_id, tuple.domain()).await;
let _ = self.seek_by_domain(db, relation_id, tuple.domain()).await;
}

let relation = Self::get_relation_mut(relation_id, &self.schema, &mut self.relations);
Expand Down
55 changes: 26 additions & 29 deletions crates/kernel/src/builtins/bf_objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ async fn bf_create<'a>(bf_args: &mut BfCallState<'a>) -> Result<BfRet, Error> {
};

let tramp = bf_args
.vm
.exec_state
.top()
.bf_trampoline
.unwrap_or(BF_CREATE_OBJECT_TRAMPOLINE_START_CALL_INITIALIZE);
Expand Down Expand Up @@ -166,10 +166,10 @@ async fn bf_create<'a>(bf_args: &mut BfCallState<'a>) -> Result<BfRet, Error> {
verb_name: "initialize".to_string(),
location: new_obj,
this: new_obj,
player: bf_args.vm.top().player,
player: bf_args.exec_state.top().player,
args: vec![],
argstr: "".to_string(),
caller: bf_args.vm.top().this,
caller: bf_args.exec_state.top().this,
},
trampoline: Some(BF_CREATE_OBJECT_TRAMPOLINE_DONE),
command: None,
Expand All @@ -178,7 +178,7 @@ async fn bf_create<'a>(bf_args: &mut BfCallState<'a>) -> Result<BfRet, Error> {
}
BF_CREATE_OBJECT_TRAMPOLINE_DONE => {
// The trampoline argument is the object we just created.
let Some(new_obj) = bf_args.vm.top().bf_trampoline_arg.clone() else {
let Some(new_obj) = bf_args.exec_state.top().bf_trampoline_arg.clone() else {
panic!("Missing/invalid trampoline argument for bf_create");
};
Ok(Ret(new_obj))
Expand Down Expand Up @@ -211,7 +211,7 @@ async fn bf_recycle<'a>(bf_args: &mut BfCallState<'a>) -> Result<BfRet, Error> {
// object, so we'll do it manually here.

'outer: loop {
let tramp = bf_args.vm.top().bf_trampoline;
let tramp = bf_args.exec_state.top().bf_trampoline;
match tramp {
None => {
// Starting out, we need to call "recycle" on the object, if it exists.
Expand Down Expand Up @@ -255,10 +255,10 @@ async fn bf_recycle<'a>(bf_args: &mut BfCallState<'a>) -> Result<BfRet, Error> {
verb_name: "recycle".to_string(),
location: *obj,
this: *obj,
player: bf_args.vm.top().player,
player: bf_args.exec_state.top().player,
args: vec![],
argstr: "".to_string(),
caller: bf_args.vm.top().this,
caller: bf_args.exec_state.top().this,
},
trampoline: Some(BF_RECYCLE_TRAMPOLINE_CALL_EXITFUNC),
trampoline_arg: Some(contents),
Expand All @@ -267,9 +267,9 @@ async fn bf_recycle<'a>(bf_args: &mut BfCallState<'a>) -> Result<BfRet, Error> {
}
Err(WorldStateError::VerbNotFound(_, _)) => {
// Short-circuit fake-tramp state change.
bf_args.vm.top_mut().bf_trampoline =
bf_args.exec_state.top_mut().bf_trampoline =
Some(BF_RECYCLE_TRAMPOLINE_CALL_EXITFUNC);
bf_args.vm.top_mut().bf_trampoline_arg = Some(contents);
bf_args.exec_state.top_mut().bf_trampoline_arg = Some(contents);
// Fall through to the next case.
}
Err(e) => {
Expand All @@ -282,15 +282,16 @@ async fn bf_recycle<'a>(bf_args: &mut BfCallState<'a>) -> Result<BfRet, Error> {
// Check the arguments, which must be a list of objects. IF it's empty, we can
// move onto DONE_MOVE, if not, take the head of the list, and call :exitfunc on it
// (if it exists), and then back to this state.
let contents = bf_args.vm.top().bf_trampoline_arg.clone().unwrap();
let contents = bf_args.exec_state.top().bf_trampoline_arg.clone().unwrap();
let Variant::List(contents) = contents.variant() else {
panic!("Invalid trampoline argument for bf_recycle");
};
'inner: loop {
debug!(?obj, contents = ?contents, "Calling :exitfunc for objects contents");
if contents.is_empty() {
bf_args.vm.top_mut().bf_trampoline_arg = None;
bf_args.vm.top_mut().bf_trampoline = Some(BF_RECYCLE_TRAMPOLINE_DONE_MOVE);
bf_args.exec_state.top_mut().bf_trampoline_arg = None;
bf_args.exec_state.top_mut().bf_trampoline =
Some(BF_RECYCLE_TRAMPOLINE_DONE_MOVE);
continue 'outer;
}
let (head_obj, contents) = contents.pop_front();
Expand All @@ -306,7 +307,7 @@ async fn bf_recycle<'a>(bf_args: &mut BfCallState<'a>) -> Result<BfRet, Error> {
.await
else {
// If there's no :exitfunc, we can just move on to the next object.
bf_args.vm.top_mut().bf_trampoline_arg = Some(contents);
bf_args.exec_state.top_mut().bf_trampoline_arg = Some(contents);
continue 'inner;
};
// Call :exitfunc on the head object.
Expand All @@ -317,10 +318,10 @@ async fn bf_recycle<'a>(bf_args: &mut BfCallState<'a>) -> Result<BfRet, Error> {
verb_name: "exitfunc".to_string(),
location: *head_obj,
this: *head_obj,
player: bf_args.vm.top().player,
player: bf_args.exec_state.top().player,
args: vec![v_objid(*obj)],
argstr: "".to_string(),
caller: bf_args.vm.top().this,
caller: bf_args.exec_state.top().this,
},
trampoline: Some(BF_RECYCLE_TRAMPOLINE_CALL_EXITFUNC),
trampoline_arg: Some(contents),
Expand Down Expand Up @@ -399,7 +400,7 @@ async fn bf_move<'a>(bf_args: &mut BfCallState<'a>) -> Result<BfRet, Error> {
// 3 => return v_none

let mut tramp = bf_args
.vm
.exec_state
.top()
.bf_trampoline
.unwrap_or(BF_MOVE_TRAMPOLINE_START_ACCEPT);
Expand Down Expand Up @@ -428,10 +429,10 @@ async fn bf_move<'a>(bf_args: &mut BfCallState<'a>) -> Result<BfRet, Error> {
verb_name: "accept".to_string(),
location: *whereto,
this: *whereto,
player: bf_args.vm.top().player,
player: bf_args.exec_state.top().player,
args: vec![v_objid(*what)],
argstr: "".to_string(),
caller: bf_args.vm.top().this,
caller: bf_args.exec_state.top().this,
},
trampoline: Some(BF_MOVE_TRAMPOLINE_MOVE_CALL_EXITFUNC),
trampoline_arg: None,
Expand Down Expand Up @@ -459,7 +460,7 @@ async fn bf_move<'a>(bf_args: &mut BfCallState<'a>) -> Result<BfRet, Error> {
// Accept verb has been called, and returned. Check the result. Should be on stack,
// unless short-circuited, in which case we assume *false*
let result = if !shortcircuit {
bf_args.vm.top().peek_top().unwrap()
bf_args.exec_state.top().peek_top().unwrap()
} else {
v_int(0)
};
Expand Down Expand Up @@ -504,10 +505,10 @@ async fn bf_move<'a>(bf_args: &mut BfCallState<'a>) -> Result<BfRet, Error> {
verb_name: "exitfunc".to_string(),
location: original_location,
this: original_location,
player: bf_args.vm.top().player,
player: bf_args.exec_state.top().player,
args: vec![v_objid(*what)],
argstr: "".to_string(),
caller: bf_args.vm.top().this,
caller: bf_args.exec_state.top().this,
},
command: None,
trampoline: Some(BF_MOVE_TRAMPOLINE_CALL_ENTERFUNC),
Expand Down Expand Up @@ -548,10 +549,10 @@ async fn bf_move<'a>(bf_args: &mut BfCallState<'a>) -> Result<BfRet, Error> {
verb_name: "enterfunc".to_string(),
location: *whereto,
this: *whereto,
player: bf_args.vm.top().player,
player: bf_args.exec_state.top().player,
args: vec![v_objid(*what)],
argstr: "".to_string(),
caller: bf_args.vm.top().this,
caller: bf_args.exec_state.top().this,
},
command: None,
trampoline: Some(3),
Expand Down Expand Up @@ -667,7 +668,7 @@ async fn bf_set_player_flag<'a>(bf_args: &mut BfCallState<'a>) -> Result<BfRet,

// If the object was player, update the VM's copy of the perms.
if *obj == bf_args.task_perms().await.map_err(world_state_err)?.who {
bf_args.vm.set_task_perms(*obj);
bf_args.exec_state.set_task_perms(*obj);
}

Ok(Ret(v_none()))
Expand All @@ -685,11 +686,7 @@ async fn bf_players<'a>(bf_args: &mut BfCallState<'a>) -> Result<BfRet, Error> {
.map_err(world_state_err)?;

Ok(Ret(v_list(
players
.iter()
.map(v_objid)
.collect::<Vec<_>>()
.as_slice(),
players.iter().map(v_objid).collect::<Vec<_>>().as_slice(),
)))
}
bf_declare!(players, bf_players);
Expand Down
Loading

0 comments on commit 555787e

Please sign in to comment.