Skip to content

Commit

Permalink
SsoHashSet reimplemented as a wrapper on top of SsoHashMap
Browse files Browse the repository at this point in the history
SsoHashSet::replace had to be removed because
it requires missing API from SsoHashMap.
It's not a widely used function, so I think it's ok
to omit it for now.

EitherIter moved into its own file.

Also sprinkled code with #[inline] attributes where appropriate.
  • Loading branch information
VFLashM committed Sep 26, 2020
1 parent 0600b17 commit 41942fa
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 228 deletions.
75 changes: 75 additions & 0 deletions compiler/rustc_data_structures/src/sso/either_iter.rs
@@ -0,0 +1,75 @@
use std::fmt;
use std::iter::ExactSizeIterator;
use std::iter::FusedIterator;
use std::iter::Iterator;

/// Iterator which may contain instance of
/// one of two specific implementations.
///
/// Note: For most methods providing custom
/// implementation may margianlly
/// improve performance by avoiding
/// doing Left/Right match on every step
/// and doing it only once instead.
#[derive(Clone)]
pub enum EitherIter<L, R> {
Left(L),
Right(R),
}

impl<L, R> Iterator for EitherIter<L, R>
where
L: Iterator,
R: Iterator<Item = L::Item>,
{
type Item = L::Item;

fn next(&mut self) -> Option<Self::Item> {
match self {
EitherIter::Left(l) => l.next(),
EitherIter::Right(r) => r.next(),
}
}

fn size_hint(&self) -> (usize, Option<usize>) {
match self {
EitherIter::Left(l) => l.size_hint(),
EitherIter::Right(r) => r.size_hint(),
}
}
}

impl<L, R> ExactSizeIterator for EitherIter<L, R>
where
L: ExactSizeIterator,
R: ExactSizeIterator,
EitherIter<L, R>: Iterator,
{
fn len(&self) -> usize {
match self {
EitherIter::Left(l) => l.len(),
EitherIter::Right(r) => r.len(),
}
}
}

impl<L, R> FusedIterator for EitherIter<L, R>
where
L: FusedIterator,
R: FusedIterator,
EitherIter<L, R>: Iterator,
{
}

impl<L, R> fmt::Debug for EitherIter<L, R>
where
L: fmt::Debug,
R: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
EitherIter::Left(l) => l.fmt(f),
EitherIter::Right(r) => r.fmt(f),
}
}
}
18 changes: 16 additions & 2 deletions compiler/rustc_data_structures/src/sso/map.rs
@@ -1,4 +1,4 @@
use super::EitherIter;
use super::either_iter::EitherIter;
use crate::fx::FxHashMap;
use arrayvec::ArrayVec;
use std::borrow::Borrow;
Expand Down Expand Up @@ -32,6 +32,7 @@ pub enum SsoHashMap<K, V> {

impl<K, V> SsoHashMap<K, V> {
/// Creates an empty `SsoHashMap`.
#[inline]
pub fn new() -> Self {
SsoHashMap::Array(ArrayVec::new())
}
Expand Down Expand Up @@ -81,13 +82,15 @@ impl<K, V> SsoHashMap<K, V> {

/// An iterator visiting all key-value pairs in arbitrary order.
/// The iterator element type is `(&'a K, &'a V)`.
pub fn iter(&self) -> impl Iterator<Item = (&'_ K, &'_ V)> {
#[inline]
pub fn iter(&self) -> <&Self as IntoIterator>::IntoIter {
self.into_iter()
}

/// An iterator visiting all key-value pairs in arbitrary order,
/// with mutable references to the values.
/// The iterator element type is `(&'a K, &'a mut V)`.
#[inline]
pub fn iter_mut(&mut self) -> impl Iterator<Item = (&'_ K, &'_ mut V)> {
self.into_iter()
}
Expand Down Expand Up @@ -319,12 +322,14 @@ impl<K: Eq + Hash, V> SsoHashMap<K, V> {
}

/// Gets the given key's corresponding entry in the map for in-place manipulation.
#[inline]
pub fn entry(&mut self, key: K) -> Entry<'_, K, V> {
Entry { ssomap: self, key }
}
}

impl<K, V> Default for SsoHashMap<K, V> {
#[inline]
fn default() -> Self {
Self::new()
}
Expand All @@ -348,6 +353,7 @@ impl<K: Eq + Hash, V> Extend<(K, V)> for SsoHashMap<K, V> {
}
}

#[inline]
fn extend_one(&mut self, (k, v): (K, V)) {
self.insert(k, v);
}
Expand Down Expand Up @@ -375,10 +381,12 @@ where
self.extend(iter.into_iter().map(|(k, v)| (k.clone(), v.clone())))
}

#[inline]
fn extend_one(&mut self, (&k, &v): (&'a K, &'a V)) {
self.insert(k, v);
}

#[inline]
fn extend_reserve(&mut self, additional: usize) {
Extend::<(K, V)>::extend_reserve(self, additional)
}
Expand All @@ -400,12 +408,14 @@ impl<K, V> IntoIterator for SsoHashMap<K, V> {
}

/// adapts Item of array reference iterator to Item of hashmap reference iterator.
#[inline(always)]
fn adapt_array_ref_it<K, V>(pair: &'a (K, V)) -> (&'a K, &'a V) {
let (a, b) = pair;
(a, b)
}

/// adapts Item of array mut reference iterator to Item of hashmap mut reference iterator.
#[inline(always)]
fn adapt_array_mut_it<K, V>(pair: &'a mut (K, V)) -> (&'a K, &'a mut V) {
let (a, b) = pair;
(a, b)
Expand Down Expand Up @@ -464,6 +474,7 @@ where
{
type Output = V;

#[inline]
fn index(&self, key: &Q) -> &V {
self.get(key).expect("no entry found for key")
}
Expand All @@ -490,6 +501,7 @@ impl<'a, K: Eq + Hash, V> Entry<'a, K, V> {

/// Ensures a value is in the entry by inserting the default if empty, and returns
/// a mutable reference to the value in the entry.
#[inline]
pub fn or_insert(self, value: V) -> &'a mut V {
self.or_insert_with(|| value)
}
Expand All @@ -515,6 +527,7 @@ impl<'a, K: Eq + Hash, V> Entry<'a, K, V> {
}

/// Returns a reference to this entry's key.
#[inline]
pub fn key(&self) -> &K {
&self.key
}
Expand All @@ -523,6 +536,7 @@ impl<'a, K: Eq + Hash, V> Entry<'a, K, V> {
impl<'a, K: Eq + Hash, V: Default> Entry<'a, K, V> {
/// Ensures a value is in the entry by inserting the default value if empty,
/// and returns a mutable reference to the value in the entry.
#[inline]
pub fn or_default(self) -> &'a mut V {
self.or_insert_with(Default::default)
}
Expand Down
73 changes: 1 addition & 72 deletions compiler/rustc_data_structures/src/sso/mod.rs
@@ -1,75 +1,4 @@
use std::fmt;
use std::iter::ExactSizeIterator;
use std::iter::FusedIterator;
use std::iter::Iterator;

/// Iterator which may contain instance of
/// one of two specific implementations.
///
/// Used by both SsoHashMap and SsoHashSet.
#[derive(Clone)]
pub enum EitherIter<L, R> {
Left(L),
Right(R),
}

impl<L, R> Iterator for EitherIter<L, R>
where
L: Iterator,
R: Iterator<Item = L::Item>,
{
type Item = L::Item;

fn next(&mut self) -> Option<Self::Item> {
match self {
EitherIter::Left(l) => l.next(),
EitherIter::Right(r) => r.next(),
}
}

fn size_hint(&self) -> (usize, Option<usize>) {
match self {
EitherIter::Left(l) => l.size_hint(),
EitherIter::Right(r) => r.size_hint(),
}
}
}

impl<L, R> ExactSizeIterator for EitherIter<L, R>
where
L: ExactSizeIterator,
R: ExactSizeIterator,
EitherIter<L, R>: Iterator,
{
fn len(&self) -> usize {
match self {
EitherIter::Left(l) => l.len(),
EitherIter::Right(r) => r.len(),
}
}
}

impl<L, R> FusedIterator for EitherIter<L, R>
where
L: FusedIterator,
R: FusedIterator,
EitherIter<L, R>: Iterator,
{
}

impl<L, R> fmt::Debug for EitherIter<L, R>
where
L: fmt::Debug,
R: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
EitherIter::Left(l) => l.fmt(f),
EitherIter::Right(r) => r.fmt(f),
}
}
}

mod either_iter;
mod map;
mod set;

Expand Down

0 comments on commit 41942fa

Please sign in to comment.