Skip to content

Add empty tuple optimization #1178

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

Merged
merged 2 commits into from
Jul 25, 2019
Merged
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
6 changes: 6 additions & 0 deletions tests/snippets/tuple.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,9 @@ def __eq__(self, x):
a = (1, 2, 3)
a += 1,
assert a == (1, 2, 3, 1)

assert () is ()

a = ()
b = ()
assert a is b
11 changes: 10 additions & 1 deletion vm/src/pyobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ pub struct PyContext {
pub none: PyNoneRef,
pub ellipsis: PyEllipsisRef,
pub not_implemented: PyNotImplementedRef,
pub empty_tuple: PyTupleRef,
pub tuple_type: PyClassRef,
pub tupleiterator_type: PyClassRef,
pub set_type: PyClassRef,
Expand Down Expand Up @@ -324,6 +325,9 @@ impl PyContext {

let true_value = create_object(PyInt::new(BigInt::one()), &bool_type);
let false_value = create_object(PyInt::new(BigInt::zero()), &bool_type);

let empty_tuple = create_object(PyTuple::from(vec![]), &tuple_type);

let context = PyContext {
bool_type,
memoryview_type,
Expand Down Expand Up @@ -382,6 +386,7 @@ impl PyContext {
weakproxy_type,
type_type,
exceptions,
empty_tuple,
};
objtype::init(&context);
objlist::init(&context);
Expand Down Expand Up @@ -645,7 +650,11 @@ impl PyContext {
}

pub fn new_tuple(&self, elements: Vec<PyObjectRef>) -> PyObjectRef {
PyObject::new(PyTuple::from(elements), self.tuple_type(), None)
if elements.is_empty() {
self.empty_tuple.clone().into_object()
} else {
PyObject::new(PyTuple::from(elements), self.tuple_type(), None)
}
}

pub fn new_list(&self, elements: Vec<PyObjectRef>) -> PyObjectRef {
Expand Down