Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate the FxHashMap() and FxHashSet() constructor function hack #55114

Merged
merged 9 commits into from
Oct 20, 2018
20 changes: 8 additions & 12 deletions src/bootstrap/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,19 +169,21 @@ impl Ord for Interned<String> {
}
}

struct TyIntern<T> {
struct TyIntern<T: Hash + Clone + Eq> {
items: Vec<T>,
set: HashMap<T, Interned<T>>,
}

impl<T: Hash + Clone + Eq> TyIntern<T> {
fn new() -> TyIntern<T> {
impl<T: Hash + Clone + Eq> Default for TyIntern<T> {
nikomatsakis marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be derived?

Copy link
Member

@RalfJung RalfJung Oct 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No; the derived impl would have an additional where T: Default.

A comment saying so might be a good idea though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Get it

fn default() -> Self {
TyIntern {
items: Vec::new(),
set: HashMap::new(),
set: Default::default(),
}
}
}

impl<T: Hash + Clone + Eq> TyIntern<T> {
fn intern_borrow<B>(&mut self, item: &B) -> Interned<T>
where
B: Eq + Hash + ToOwned<Owned=T> + ?Sized,
Expand Down Expand Up @@ -212,19 +214,13 @@ impl<T: Hash + Clone + Eq> TyIntern<T> {
}
}

#[derive(Default)]
pub struct Interner {
strs: Mutex<TyIntern<String>>,
paths: Mutex<TyIntern<PathBuf>>,
}

impl Interner {
fn new() -> Interner {
Interner {
strs: Mutex::new(TyIntern::new()),
paths: Mutex::new(TyIntern::new()),
}
}

pub fn intern_str(&self, s: &str) -> Interned<String> {
self.strs.lock().unwrap().intern_borrow(s)
}
Expand All @@ -238,7 +234,7 @@ impl Interner {
}

lazy_static! {
pub static ref INTERNER: Interner = Interner::new();
pub static ref INTERNER: Interner = Interner::default();
}

/// This is essentially a HashMap which allows storing any type in its input and
Expand Down
54 changes: 23 additions & 31 deletions src/libarena/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,9 @@ impl<T> TypedArenaChunk<T> {

const PAGE: usize = 4096;

impl<T> TypedArena<T> {
impl<T> Default for TypedArena<T> {
/// Creates a new `TypedArena`.
#[inline]
pub fn new() -> TypedArena<T> {
fn default() -> TypedArena<T> {
TypedArena {
// We set both `ptr` and `end` to 0 so that the first call to
// alloc() will trigger a grow().
Expand All @@ -127,7 +126,9 @@ impl<T> TypedArena<T> {
_own: PhantomData,
}
}
}

impl<T> TypedArena<T> {
/// Allocates an object in the `TypedArena`, returning a reference to it.
#[inline]
pub fn alloc(&self, object: T) -> &mut T {
Expand Down Expand Up @@ -296,15 +297,17 @@ pub struct DroplessArena {

unsafe impl Send for DroplessArena {}

impl DroplessArena {
pub fn new() -> DroplessArena {
impl Default for DroplessArena {
fn default() -> DroplessArena {
DroplessArena {
ptr: Cell::new(0 as *mut u8),
end: Cell::new(0 as *mut u8),
chunks: RefCell::new(vec![]),
chunks: Default::default(),
}
}
}

impl DroplessArena {
pub fn in_arena<T: ?Sized>(&self, ptr: *const T) -> bool {
let ptr = ptr as *const u8 as *mut u8;
for chunk in &*self.chunks.borrow() {
Expand Down Expand Up @@ -419,18 +422,13 @@ impl DroplessArena {
}
}

#[derive(Default)]
// FIXME(@Zoxc): this type is entirely unused in rustc
pub struct SyncTypedArena<T> {
lock: MTLock<TypedArena<T>>,
}

impl<T> SyncTypedArena<T> {
#[inline(always)]
pub fn new() -> SyncTypedArena<T> {
SyncTypedArena {
lock: MTLock::new(TypedArena::new())
}
}

#[inline(always)]
pub fn alloc(&self, object: T) -> &mut T {
// Extend the lifetime of the result since it's limited to the lock guard
Expand All @@ -452,18 +450,12 @@ impl<T> SyncTypedArena<T> {
}
}

#[derive(Default)]
pub struct SyncDroplessArena {
lock: MTLock<DroplessArena>,
}

impl SyncDroplessArena {
#[inline(always)]
pub fn new() -> SyncDroplessArena {
SyncDroplessArena {
lock: MTLock::new(DroplessArena::new())
}
}

#[inline(always)]
pub fn in_arena<T: ?Sized>(&self, ptr: *const T) -> bool {
self.lock.lock().in_arena(ptr)
Expand Down Expand Up @@ -508,7 +500,7 @@ mod tests {

#[test]
pub fn test_unused() {
let arena: TypedArena<Point> = TypedArena::new();
let arena: TypedArena<Point> = TypedArena::default();
assert!(arena.chunks.borrow().is_empty());
}

Expand Down Expand Up @@ -546,7 +538,7 @@ mod tests {
}
}

let arena = Wrap(TypedArena::new());
let arena = Wrap(TypedArena::default());

let result = arena.alloc_outer(|| Outer {
inner: arena.alloc_inner(|| Inner { value: 10 }),
Expand All @@ -557,15 +549,15 @@ mod tests {

#[test]
pub fn test_copy() {
let arena = TypedArena::new();
let arena = TypedArena::default();
for _ in 0..100000 {
arena.alloc(Point { x: 1, y: 2, z: 3 });
}
}

#[bench]
pub fn bench_copy(b: &mut Bencher) {
let arena = TypedArena::new();
let arena = TypedArena::default();
b.iter(|| arena.alloc(Point { x: 1, y: 2, z: 3 }))
}

Expand All @@ -584,7 +576,7 @@ mod tests {

#[test]
pub fn test_noncopy() {
let arena = TypedArena::new();
let arena = TypedArena::default();
for _ in 0..100000 {
arena.alloc(Noncopy {
string: "hello world".to_string(),
Expand All @@ -595,15 +587,15 @@ mod tests {

#[test]
pub fn test_typed_arena_zero_sized() {
let arena = TypedArena::new();
let arena = TypedArena::default();
for _ in 0..100000 {
arena.alloc(());
}
}

#[test]
pub fn test_typed_arena_clear() {
let mut arena = TypedArena::new();
let mut arena = TypedArena::default();
for _ in 0..10 {
arena.clear();
for _ in 0..10000 {
Expand All @@ -628,7 +620,7 @@ mod tests {
fn test_typed_arena_drop_count() {
let counter = Cell::new(0);
{
let arena: TypedArena<DropCounter> = TypedArena::new();
let arena: TypedArena<DropCounter> = TypedArena::default();
for _ in 0..100 {
// Allocate something with drop glue to make sure it doesn't leak.
arena.alloc(DropCounter { count: &counter });
Expand All @@ -640,7 +632,7 @@ mod tests {
#[test]
fn test_typed_arena_drop_on_clear() {
let counter = Cell::new(0);
let mut arena: TypedArena<DropCounter> = TypedArena::new();
let mut arena: TypedArena<DropCounter> = TypedArena::default();
for i in 0..10 {
for _ in 0..100 {
// Allocate something with drop glue to make sure it doesn't leak.
Expand All @@ -667,7 +659,7 @@ mod tests {
fn test_typed_arena_drop_small_count() {
DROP_COUNTER.with(|c| c.set(0));
{
let arena: TypedArena<SmallDroppable> = TypedArena::new();
let arena: TypedArena<SmallDroppable> = TypedArena::default();
for _ in 0..100 {
// Allocate something with drop glue to make sure it doesn't leak.
arena.alloc(SmallDroppable);
Expand All @@ -679,7 +671,7 @@ mod tests {

#[bench]
pub fn bench_noncopy(b: &mut Bencher) {
let arena = TypedArena::new();
let arena = TypedArena::default();
b.iter(|| {
arena.alloc(Noncopy {
string: "hello world".to_string(),
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/dep_graph/cgu_reuse_tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ pub struct CguReuseTracker {
impl CguReuseTracker {
pub fn new() -> CguReuseTracker {
let data = TrackerData {
actual_reuse: FxHashMap(),
expected_reuse: FxHashMap(),
actual_reuse: Default::default(),
expected_reuse: Default::default(),
};

CguReuseTracker {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/dep_graph/dep_tracking_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl<M: DepTrackingMapConfig> DepTrackingMap<M> {
DepTrackingMap {
phantom: PhantomData,
graph,
map: FxHashMap(),
map: Default::default(),
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/librustc/dep_graph/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,11 @@ impl DepGraph {
DepGraph {
data: Some(Lrc::new(DepGraphData {
previous_work_products: prev_work_products,
dep_node_debug: Lock::new(FxHashMap()),
dep_node_debug: Lock::new(Default::default()),
current: Lock::new(CurrentDepGraph::new()),
previous: prev_graph,
colors: Lock::new(DepNodeColorMap::new(prev_graph_node_count)),
loaded_from_cache: Lock::new(FxHashMap()),
loaded_from_cache: Lock::new(Default::default()),
})),
fingerprints: Lrc::new(Lock::new(fingerprints)),
}
Expand Down Expand Up @@ -209,7 +209,7 @@ impl DepGraph {
|key| OpenTask::Regular(Lock::new(RegularOpenTask {
node: key,
reads: SmallVec::new(),
read_set: FxHashSet(),
read_set: Default::default(),
})),
|data, key, task| data.borrow_mut().complete_task(key, task))
}
Expand Down Expand Up @@ -353,7 +353,7 @@ impl DepGraph {
let (result, open_task) = ty::tls::with_context(|icx| {
let task = OpenTask::Anon(Lock::new(AnonOpenTask {
reads: SmallVec::new(),
read_set: FxHashSet(),
read_set: Default::default(),
}));

let r = {
Expand Down Expand Up @@ -937,7 +937,7 @@ impl CurrentDepGraph {
CurrentDepGraph {
nodes: IndexVec::new(),
edges: IndexVec::new(),
node_to_node_index: FxHashMap(),
node_to_node_index: Default::default(),
anon_id_seed: stable_hasher.finish(),
forbidden_edge,
total_read_count: 0,
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/dep_graph/prev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use rustc_data_structures::fx::FxHashMap;
use super::dep_node::DepNode;
use super::serialized::{SerializedDepGraph, SerializedDepNodeIndex};

#[derive(Debug, RustcEncodable, RustcDecodable)]
#[derive(Debug, RustcEncodable, RustcDecodable, Default)]
pub struct PreviousDepGraph {
data: SerializedDepGraph,
index: FxHashMap<DepNode, SerializedDepNodeIndex>,
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/dep_graph/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl DepGraphQuery {
edges: &[(DepNode, DepNode)])
-> DepGraphQuery {
let mut graph = Graph::with_capacity(nodes.len(), edges.len());
let mut indices = FxHashMap();
let mut indices = FxHashMap::default();
for node in nodes {
indices.insert(node.clone(), graph.add_node(node.clone()));
}
Expand Down
12 changes: 1 addition & 11 deletions src/librustc/dep_graph/serialized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ newtype_index! {
}

/// Data for use when recompiling the **current crate**.
#[derive(Debug, RustcEncodable, RustcDecodable)]
#[derive(Debug, RustcEncodable, RustcDecodable, Default)]
pub struct SerializedDepGraph {
/// The set of all DepNodes in the graph
pub nodes: IndexVec<SerializedDepNodeIndex, DepNode>,
Expand All @@ -36,16 +36,6 @@ pub struct SerializedDepGraph {
}

impl SerializedDepGraph {

pub fn new() -> SerializedDepGraph {
SerializedDepGraph {
nodes: IndexVec::new(),
fingerprints: IndexVec::new(),
edge_list_indices: IndexVec::new(),
edge_list_data: Vec::new(),
}
}

#[inline]
pub fn edge_targets_from(&self,
source: SerializedDepNodeIndex)
Expand Down
8 changes: 4 additions & 4 deletions src/librustc/hir/map/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,10 +421,10 @@ impl Definitions {
node_to_def_index: NodeMap(),
def_index_to_node: [vec![], vec![]],
node_to_hir_id: IndexVec::new(),
parent_modules_of_macro_defs: FxHashMap(),
expansions_that_defined: FxHashMap(),
next_disambiguator: FxHashMap(),
def_index_to_span: FxHashMap(),
parent_modules_of_macro_defs: Default::default(),
expansions_that_defined: Default::default(),
next_disambiguator: Default::default(),
def_index_to_span: Default::default(),
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustc/hir/map/hir_id_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl<'a, 'hir: 'a> OuterVisitor<'a, 'hir> {
HirIdValidator {
hir_map,
owner_def_index: None,
hir_ids_seen: FxHashMap(),
hir_ids_seen: Default::default(),
errors: Vec::new(),
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ich/hcx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ impl<'a> HashStable<StableHashingContext<'a>> for Span {
// recursing every time.
thread_local! {
static CACHE: RefCell<FxHashMap<hygiene::Mark, u64>> =
RefCell::new(FxHashMap());
RefCell::new(Default::default());
}

let sub_hash: u64 = CACHE.with(|cache| {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ich/impls_ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ for &'gcx ty::List<T>
hasher: &mut StableHasher<W>) {
thread_local! {
static CACHE: RefCell<FxHashMap<(usize, usize), Fingerprint>> =
RefCell::new(FxHashMap());
RefCell::new(Default::default());
}

let hash = CACHE.with(|cache| {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/infer/freshen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl<'a, 'gcx, 'tcx> TypeFreshener<'a, 'gcx, 'tcx> {
TypeFreshener {
infcx,
freshen_count: 0,
freshen_map: FxHashMap(),
freshen_map: Default::default(),
}
}

Expand Down
Loading