Skip to content

Commit

Permalink
Merge pull request #627 from Veykril/veykril/push-vypmluoqzkkx
Browse files Browse the repository at this point in the history
Some simplifications
  • Loading branch information
Veykril authored Dec 13, 2024
2 parents b0cc7dc + 54a147b commit f4ea167
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 27 deletions.
3 changes: 2 additions & 1 deletion src/cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ pub struct Cycle {
participants: CycleParticipants,
}

pub(crate) type CycleParticipants = Arc<Vec<DatabaseKeyIndex>>;
// We want `Cycle`` to be thin
pub(crate) type CycleParticipants = Arc<Box<[DatabaseKeyIndex]>>;

impl Cycle {
pub(crate) fn new(participants: CycleParticipants) -> Self {
Expand Down
2 changes: 1 addition & 1 deletion src/revision.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub(crate) struct AtomicRevision {
}

impl AtomicRevision {
pub(crate) fn start() -> Self {
pub(crate) const fn start() -> Self {
Self {
data: AtomicUsize::new(START),
}
Expand Down
42 changes: 17 additions & 25 deletions src/runtime.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use std::{
panic::panic_any,
sync::{atomic::AtomicUsize, Arc},
sync::{
atomic::{AtomicBool, Ordering},
Arc,
},
thread::ThreadId,
};

use crossbeam::atomic::AtomicCell;
use parking_lot::Mutex;

use crate::{
Expand All @@ -18,13 +20,10 @@ use self::dependency_graph::DependencyGraph;
mod dependency_graph;

pub struct Runtime {
/// Stores the next id to use for a snapshotted runtime (starts at 1).
next_id: AtomicUsize,

/// Set to true when the current revision has been canceled.
/// This is done when we an input is being changed. The flag
/// is set back to false once the input has been changed.
revision_canceled: AtomicCell<bool>,
revision_canceled: AtomicBool,

/// Stores the "last change" revision for values of each duration.
/// This vector is always of length at least 1 (for Durability 0)
Expand All @@ -35,7 +34,7 @@ pub struct Runtime {
/// revisions[i + 1]`, for all `i`. This is because when you
/// modify a value with durability D, that implies that values
/// with durability less than D may have changed too.
revisions: Vec<AtomicRevision>,
revisions: Box<[AtomicRevision; Durability::LEN]>,

/// The dependency graph tracks which runtimes are blocked on one
/// another, waiting for queries to terminate.
Expand Down Expand Up @@ -81,10 +80,7 @@ impl<V> StampedValue<V> {
impl Default for Runtime {
fn default() -> Self {
Runtime {
revisions: (0..Durability::LEN)
.map(|_| AtomicRevision::start())
.collect(),
next_id: AtomicUsize::new(1),
revisions: Box::new([const { AtomicRevision::start() }; Durability::LEN]),
revision_canceled: Default::default(),
dependency_graph: Default::default(),
table: Default::default(),
Expand All @@ -96,7 +92,6 @@ impl std::fmt::Debug for Runtime {
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fmt.debug_struct("Runtime")
.field("revisions", &self.revisions)
.field("next_id", &self.next_id)
.field("revision_canceled", &self.revision_canceled)
.field("dependency_graph", &self.dependency_graph)
.finish()
Expand Down Expand Up @@ -131,11 +126,11 @@ impl Runtime {
}

pub(crate) fn load_cancellation_flag(&self) -> bool {
self.revision_canceled.load()
self.revision_canceled.load(Ordering::Acquire)
}

pub(crate) fn set_cancellation_flag(&self) {
self.revision_canceled.store(true);
self.revision_canceled.store(true, Ordering::Release);
}

pub(crate) fn table(&self) -> &Table {
Expand All @@ -150,7 +145,7 @@ impl Runtime {
let r_old = self.current_revision();
let r_new = r_old.next();
self.revisions[0].store(r_new);
self.revision_canceled.store(false);
self.revision_canceled.store(false, Ordering::Release);
r_new
}

Expand Down Expand Up @@ -277,19 +272,16 @@ impl Runtime {
// (at least for this execution, not necessarily across executions),
// no matter where it started on the stack. Find the minimum
// key and rotate it to the front.
let min = v
if let Some((_, index, _)) = v
.iter()
.map(|key| (key.ingredient_index.debug_name(db), key))
.enumerate()
.map(|(idx, key)| (key.ingredient_index.debug_name(db), idx, key))
.min()
.unwrap()
.1;
let index = v.iter().position(|p| p == min).unwrap();
v.rotate_left(index);

// No need to store extra memory.
v.shrink_to_fit();
{
v.rotate_left(index);
}

Cycle::new(Arc::new(v))
Cycle::new(Arc::new(v.into_boxed_slice()))
};
tracing::debug!("cycle {cycle:?}, cycle_query {cycle_query:#?}");

Expand Down

0 comments on commit f4ea167

Please sign in to comment.