Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ cranelift-jit = "0.93"
fast-float = "0.2"
bumpalo = { version = "3.19.0", features = ["collections"] }
target-lexicon = "0.12.2"
ordermap = "0.5.10"

[dev-dependencies]
assert_cmd = "2.0.3"
Expand Down
18 changes: 9 additions & 9 deletions src/interp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::runtime::{self, Float, Int, Line, LineReader, Str, UniqueStr};

use crossbeam::scope;
use crossbeam_channel::bounded;
use hashbrown::HashMap;
use ordermap::map::OrderMap;
use rand::{self, rngs::StdRng, Rng, SeedableRng};
use regex::bytes::Regex;

Expand Down Expand Up @@ -51,12 +51,12 @@ pub(crate) struct Slots {
pub int: Vec<Int>,
pub float: Vec<Float>,
pub strs: Vec<UniqueStr<'static>>,
pub intint: Vec<HashMap<Int, Int>>,
pub intfloat: Vec<HashMap<Int, Float>>,
pub intstr: Vec<HashMap<Int, UniqueStr<'static>>>,
pub strint: Vec<HashMap<UniqueStr<'static>, Int>>,
pub strfloat: Vec<HashMap<UniqueStr<'static>, Float>>,
pub strstr: Vec<HashMap<UniqueStr<'static>, UniqueStr<'static>>>,
pub intint: Vec<OrderMap<Int, Int>>,
pub intfloat: Vec<OrderMap<Int, Float>>,
pub intstr: Vec<OrderMap<Int, UniqueStr<'static>>>,
pub strint: Vec<OrderMap<UniqueStr<'static>, Int>>,
pub strfloat: Vec<OrderMap<UniqueStr<'static>, Float>>,
pub strstr: Vec<OrderMap<UniqueStr<'static>, UniqueStr<'static>>>,
}

/// A Simple helper trait for implement aggregations for slot values and variables.
Expand All @@ -83,8 +83,8 @@ impl<'a> Agg for UniqueStr<'a> {
}
}
}
impl<K: std::hash::Hash + Eq, V: Agg + Default> Agg for HashMap<K, V> {
fn agg(mut self, other: HashMap<K, V>) -> HashMap<K, V> {
impl<K: std::hash::Hash + Eq, V: Agg + Default> Agg for OrderMap<K, V> {
fn agg(mut self, other: OrderMap<K, V>) -> OrderMap<K, V> {
for (k, v) in other {
let entry = self.entry(k).or_default();
let v2 = mem::take(entry);
Expand Down
27 changes: 14 additions & 13 deletions src/runtime/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::common::{FileSpec, Result};
use grep_cli::CommandReader;
use hashbrown::HashMap;
use ordermap::map::{OrderMap, RawEntryApiV1};
use regex::bytes::Regex;
use std::cell::{Cell, RefCell};
use std::fs::File;
Expand Down Expand Up @@ -584,7 +585,7 @@ where
// NB These are repr(transparent) because we pass them around as void* when compiling with LLVM.
#[repr(transparent)]
#[derive(Debug)]
pub(crate) struct SharedMap<K, V>(pub(crate) Rc<RefCell<HashMap<K, V>>>);
pub(crate) struct SharedMap<K, V>(pub(crate) Rc<RefCell<OrderMap<K, V>>>);

impl<K, V> Default for SharedMap<K, V> {
fn default() -> SharedMap<K, V> {
Expand All @@ -610,7 +611,7 @@ impl<K: Hash + Eq, V> SharedMap<K, V> {
}
pub(crate) fn iter<F, R>(&self, f: F) -> R
where
F: FnOnce(hashbrown::hash_map::Iter<K, V>) -> R,
F: FnOnce(ordermap::map::Iter<'_, K, V>) -> R,
{
f(self.0.borrow().iter())
}
Expand Down Expand Up @@ -647,24 +648,24 @@ impl<K: Hash + Eq + Clone, V: Inc + Default + Clone> SharedMap<K, V> {
// When sending SharedMaps across threads we have to clone them and clone their contents, as Rc is
// not thread-safe (and we don't want to pay the cost of Arc clones during normal execution).
pub(crate) struct Shuttle<T>(T);
impl<'a> From<Shuttle<HashMap<Int, UniqueStr<'a>>>> for IntMap<Str<'a>> {
fn from(sh: Shuttle<HashMap<Int, UniqueStr<'a>>>) -> Self {
impl<'a> From<Shuttle<OrderMap<Int, UniqueStr<'a>>>> for IntMap<Str<'a>> {
fn from(sh: Shuttle<OrderMap<Int, UniqueStr<'a>>>) -> Self {
SharedMap(Rc::new(RefCell::new(
sh.0.into_iter().map(|(x, y)| (x, y.into_str())).collect(),
)))
}
}

impl<'a> From<Shuttle<HashMap<UniqueStr<'a>, Int>>> for StrMap<'a, Int> {
fn from(sh: Shuttle<HashMap<UniqueStr<'a>, Int>>) -> Self {
impl<'a> From<Shuttle<OrderMap<UniqueStr<'a>, Int>>> for StrMap<'a, Int> {
fn from(sh: Shuttle<OrderMap<UniqueStr<'a>, Int>>) -> Self {
SharedMap(Rc::new(RefCell::new(
sh.0.into_iter().map(|(x, y)| (x.into_str(), y)).collect(),
)))
}
}

impl<K, V> SharedMap<K, V> {
fn borrow_mut(&self) -> impl std::ops::DerefMut<Target = HashMap<K, V>> + '_ {
fn borrow_mut(&self) -> impl std::ops::DerefMut<Target = OrderMap<K, V>> + '_ {
// Unlike the full std::collections APIs, we are careful not to hand out any references
// internal to a SharedMap from a public function. That means that functions which mutate
// the map are "Cell"-like, in that they swap out values or drop them in, but never hold
Expand Down Expand Up @@ -699,7 +700,7 @@ impl<K: Hash + Eq, V: Clone> SharedMap<K, V> {
impl<K: Hash + Eq + Clone, V: Clone + Default> SharedMap<K, V> {
pub(crate) fn get(&self, k: &K) -> V {
self.borrow_mut()
.raw_entry_mut()
.raw_entry_mut_v1()
.from_key(k)
.or_insert_with(|| (k.clone(), V::default()))
.1
Expand All @@ -708,7 +709,7 @@ impl<K: Hash + Eq + Clone, V: Clone + Default> SharedMap<K, V> {
}

impl<'a> IntMap<Str<'a>> {
pub(crate) fn shuttle(&self) -> Shuttle<HashMap<Int, UniqueStr<'a>>> {
pub(crate) fn shuttle(&self) -> Shuttle<OrderMap<Int, UniqueStr<'a>>> {
Shuttle(
self.0
.borrow()
Expand All @@ -720,7 +721,7 @@ impl<'a> IntMap<Str<'a>> {
}

impl<'a> StrMap<'a, Int> {
pub(crate) fn shuttle(&self) -> Shuttle<HashMap<UniqueStr<'a>, Int>> {
pub(crate) fn shuttle(&self) -> Shuttle<OrderMap<UniqueStr<'a>, Int>> {
Shuttle(
self.0
.borrow()
Expand All @@ -740,8 +741,8 @@ impl<K: Hash + Eq + Clone, V> SharedMap<K, V> {
}
}

impl<K: Hash + Eq, V> From<HashMap<K, V>> for SharedMap<K, V> {
fn from(m: HashMap<K, V>) -> SharedMap<K, V> {
impl<K: Hash + Eq, V> From<OrderMap<K, V>> for SharedMap<K, V> {
fn from(m: OrderMap<K, V>) -> SharedMap<K, V> {
SharedMap(Rc::new(RefCell::new(m)))
}
}
Expand All @@ -752,7 +753,7 @@ impl<K: Hash + Eq, V> FromIterator<(K, V)> for SharedMap<K, V> {
T: IntoIterator<Item = (K, V)>,
{
SharedMap(Rc::new(RefCell::new(
iter.into_iter().collect::<HashMap<K, V>>(),
iter.into_iter().collect::<OrderMap<K, V>>(),
)))
}
}
Expand Down