Skip to content

Commit 623b445

Browse files
stroxlerfacebook-github-bot
authored andcommitted
Improve the attrs map datatype choice
Summary: Based on review comments in D72496974; it was eaiser to stack this change on top of the diffs adding an API for creating narrowing branches than to fix at the bottom of the stack. Reviewed By: ndmitchell Differential Revision: D72509789 fbshipit-source-id: 67d196674f1624d24828f9a5387ac1897e0be46a
1 parent 314295c commit 623b445

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

pyrefly/lib/types/type_info.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use pyrefly_derive::TypeEq;
1414
use pyrefly_derive::Visit;
1515
use pyrefly_derive::VisitMut;
1616
use ruff_python_ast::name::Name;
17-
use starlark_map::ordered_map::OrderedMap;
17+
use starlark_map::small_map::SmallMap;
1818
use vec1::Vec1;
1919

2020
use crate::types::types::Type;
@@ -36,9 +36,7 @@ use crate::util::visit::VisitMut;
3636
/// # here, `x` is still `Foo` but we also can narrow
3737
/// # `x.foo`, `x.foo.bar`, and `x.baz`.
3838
/// ```
39-
#[derive(
40-
Debug, Clone, PartialEq, Eq, Visit, VisitMut, TypeEq, PartialOrd, Ord, Hash
41-
)]
39+
#[derive(Debug, Clone, PartialEq, Eq, Visit, VisitMut, TypeEq)]
4240
pub struct TypeInfo {
4341
pub ty: Type,
4442
pub attrs: NarrowedAttrs,
@@ -94,8 +92,8 @@ impl Display for TypeInfo {
9492
}
9593
}
9694

97-
#[derive(Debug, Clone, PartialEq, Eq, TypeEq, PartialOrd, Ord, Hash)]
98-
pub struct NarrowedAttrs(Option<OrderedMap<Name, NarrowedAttr>>);
95+
#[derive(Debug, Clone, PartialEq, Eq, TypeEq)]
96+
pub struct NarrowedAttrs(Option<Box<SmallMap<Name, NarrowedAttr>>>);
9997

10098
impl NarrowedAttrs {
10199
fn new() -> Self {
@@ -104,12 +102,12 @@ impl NarrowedAttrs {
104102

105103
fn add_narrow_mut(&mut self, name: Name, more_names: &[&Name], ty: Type) {
106104
if self.0.is_none() {
107-
self.0 = Some(OrderedMap::with_capacity(1))
105+
self.0 = Some(Box::new(SmallMap::with_capacity(1)))
108106
}
109107
match &mut self.0 {
110108
None => unreachable!("We just ensured that we have a map of attrs"),
111-
Some(attrs) => {
112-
let attr = match attrs.remove(&name) {
109+
Some(box attrs) => {
110+
let attr = match attrs.shift_remove(&name) {
113111
Some(attr) => attr.add_narrow(more_names, ty),
114112
None => NarrowedAttr::new(more_names, ty),
115113
};
@@ -119,9 +117,9 @@ impl NarrowedAttrs {
119117
}
120118

121119
fn of_narrow(name: Name, more_names: &[&Name], ty: Type) -> Self {
122-
let mut attrs = OrderedMap::with_capacity(1);
120+
let mut attrs = SmallMap::with_capacity(1);
123121
attrs.insert(name.clone(), NarrowedAttr::new(more_names, ty));
124-
Self(Some(attrs))
122+
Self(Some(Box::new(attrs)))
125123
}
126124

127125
fn fmt_with_prefix(&self, prefix: &mut Vec<String>, f: &mut fmt::Formatter<'_>) -> fmt::Result {
@@ -204,9 +202,7 @@ impl Display for NarrowedAttrs {
204202
}
205203
}
206204

207-
#[derive(
208-
Debug, Clone, Visit, VisitMut, PartialEq, Eq, TypeEq, PartialOrd, Ord, Hash
209-
)]
205+
#[derive(Debug, Clone, Visit, VisitMut, PartialEq, Eq, TypeEq)]
210206
pub enum NarrowedAttr {
211207
Leaf(Type),
212208
WithRoot(Type, NarrowedAttrs),

0 commit comments

Comments
 (0)