Skip to content

Commit

Permalink
Remove uses of Lazy from statics that don't need it anymore
Browse files Browse the repository at this point in the history
Yay for const Mutex::new, RwLock::new, BTreeMap::new.
  • Loading branch information
glandium committed Nov 3, 2023
1 parent 57b0577 commit 0e0e619
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
14 changes: 8 additions & 6 deletions src/graft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Mutex;

use bstr::ByteSlice;
use once_cell::sync::Lazy;

use crate::cinnabar::GitChangesetId;
use crate::git::{CommitId, TreeId};
Expand All @@ -28,11 +27,14 @@ pub fn grafted() -> bool {

static DID_SOMETHING: AtomicBool = AtomicBool::new(false);

static GRAFT_TREES: Lazy<Mutex<BTreeMap<TreeId, Vec<CommitId>>>> =
Lazy::new(|| Mutex::new(BTreeMap::new()));
static GRAFT_TREES: Mutex<BTreeMap<TreeId, Vec<CommitId>>> = Mutex::new(BTreeMap::new());

pub fn graft_finish() -> Option<bool> {
Lazy::get(&GRAFT_TREES).map(|_| grafted() || DID_SOMETHING.load(Ordering::Relaxed))
if GRAFT_TREES.lock().unwrap().is_empty() {
None
} else {
Some(grafted() || DID_SOMETHING.load(Ordering::Relaxed))
}
}

pub fn init_graft() {
Expand Down Expand Up @@ -68,12 +70,12 @@ pub fn graft(
tree: TreeId,
parents: &[GitChangesetId],
) -> Result<Option<CommitId>, GraftError> {
if Lazy::get(&GRAFT_TREES).is_none() {
let mut graft_trees = GRAFT_TREES.lock().unwrap();
if graft_trees.is_empty() {
return Ok(None);
}

let changeset = raw_changeset.parse().unwrap();
let mut graft_trees = GRAFT_TREES.lock().unwrap();
let graft_trees_entry = graft_trees.get_mut(&tree).ok_or(GraftError::NoGraft)?;
let candidates = graft_trees_entry
.iter()
Expand Down
3 changes: 1 addition & 2 deletions src/libgit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use getset::{CopyGetters, Getters};
use hex_literal::hex;
use itertools::EitherOrBoth;
use itertools::Itertools;
use once_cell::sync::Lazy;

use crate::git::{BlobId, CommitId, GitObjectId, GitOid, RecursedTreeEntry, TreeId};
use crate::oid::{Abbrev, ObjectId};
Expand Down Expand Up @@ -921,7 +920,7 @@ mod refs {
}
}

static REFS_LOCK: Lazy<RwLock<()>> = Lazy::new(|| RwLock::new(()));
static REFS_LOCK: RwLock<()> = RwLock::new(());

pub fn for_each_ref_in<E, S: AsRef<OsStr>, F: FnMut(&OsStr, CommitId) -> Result<(), E>>(
prefix: S,
Expand Down
2 changes: 1 addition & 1 deletion src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -992,7 +992,7 @@ pub fn get_tags() -> TagSet {
tags
}

static BUNDLE_BLOB: Lazy<Mutex<Option<object_id>>> = Lazy::new(|| Mutex::new(None));
static BUNDLE_BLOB: Mutex<Option<object_id>> = Mutex::new(None);

#[no_mangle]
pub unsafe extern "C" fn store_changesets_metadata(result: *mut object_id) {
Expand Down

0 comments on commit 0e0e619

Please sign in to comment.