Skip to content

Commit 5320818

Browse files
authored
Changed PyAttributes type to IndexMap
1 parent 91b5a18 commit 5320818

File tree

1 file changed

+7
-12
lines changed

1 file changed

+7
-12
lines changed

vm/src/builtins/type.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,9 @@ use crate::{
1313
types::{Callable, GetAttr, PyTypeFlags, PyTypeSlots, SetAttr},
1414
AsObject, Context, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
1515
};
16+
use indexmap::{map::Entry, IndexMap};
1617
use itertools::Itertools;
17-
use std::{
18-
borrow::Borrow,
19-
collections::{HashMap, HashSet},
20-
fmt,
21-
ops::Deref,
22-
};
18+
use std::{borrow::Borrow, collections::HashSet, fmt, ops::Deref};
2319

2420
/// type(object_or_name, bases, dict)
2521
/// type(object) -> the object's type
@@ -36,10 +32,10 @@ pub struct PyType {
3632

3733
pub type PyTypeRef = PyRef<PyType>;
3834

39-
/// For attributes we do not use a dict, but a hashmap. This is probably
40-
/// faster, unordered, and only supports strings as keys.
41-
/// TODO: class attributes should maintain insertion order (use IndexMap here)
42-
pub type PyAttributes = HashMap<String, PyObjectRef, ahash::RandomState>;
35+
/// For attributes we do not use a dict, but an IndexMap, which is an Hash Table
36+
/// that maintains order and is compatible with the standard HashMap This is probably
37+
/// faster and only supports strings as keys.
38+
pub type PyAttributes = IndexMap<String, PyObjectRef, ahash::RandomState>;
4339

4440
impl fmt::Display for PyType {
4541
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
@@ -271,7 +267,7 @@ impl PyType {
271267
fn dir(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyList {
272268
let attributes: Vec<PyObjectRef> = zelf
273269
.get_attributes()
274-
.drain()
270+
.drain(..)
275271
.map(|(k, _)| vm.ctx.new_str(k).into())
276272
.collect();
277273
PyList::from(attributes)
@@ -495,7 +491,6 @@ impl PyType {
495491
}
496492

497493
if let Some(current_frame) = vm.current_frame() {
498-
use std::collections::hash_map::Entry;
499494
let entry = attributes.entry("__module__".to_owned());
500495
if matches!(entry, Entry::Vacant(_)) {
501496
let module_name =

0 commit comments

Comments
 (0)