Skip to content

PyStrInterned for PyAttributes/Constants #3700

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 9 commits into from
May 26, 2022
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
4 changes: 2 additions & 2 deletions ast/asdl_rs.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,9 +389,9 @@ def gen_classdef(self, name, fields, attrs, depth, base="AstNode"):
self.emit(f"#[extend_class]", depth + 1)
self.emit("fn extend_class_with_fields(ctx: &Context, class: &PyTypeRef) {", depth + 1)
fields = ",".join(f"ctx.new_str(ascii!({json.dumps(f.name)})).into()" for f in fields)
self.emit(f'class.set_str_attr("_fields", ctx.new_list(vec![{fields}]));', depth + 2)
self.emit(f'class.set_attr(interned!(ctx, _fields), ctx.new_list(vec![{fields}]).into());', depth + 2)
attrs = ",".join(f"ctx.new_str(ascii!({json.dumps(attr.name)})).into()" for attr in attrs)
self.emit(f'class.set_str_attr("_attributes", ctx.new_list(vec![{attrs}]));', depth + 2)
self.emit(f'class.set_attr(interned!(ctx, _attributes), ctx.new_list(vec![{attrs}]).into());', depth + 2)
self.emit("}", depth + 1)
self.emit("}", depth)

Expand Down
2 changes: 1 addition & 1 deletion benches/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ pub fn criterion_benchmark(c: &mut Criterion) {
.map(|entry| {
let path = entry.unwrap().path();
(
path.file_name().unwrap().to_str().unwrap().to_owned(),
path.file_name().unwrap().to_owned().unwrap().to_owned(),
std::fs::read_to_string(path).unwrap(),
)
})
Expand Down
5 changes: 3 additions & 2 deletions common/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ impl HashSecret {
pub fn new(seed: u32) -> Self {
let mut buf = [0u8; 16];
lcg_urandom(seed, &mut buf);
let k0 = u64::from_le_bytes(buf[..8].try_into().unwrap());
let k1 = u64::from_le_bytes(buf[8..].try_into().unwrap());
let (left, right) = buf.split_at(8);
let k0 = u64::from_le_bytes(left.try_into().unwrap());
let k1 = u64::from_le_bytes(right.try_into().unwrap());
Self { k0, k1 }
}
}
Expand Down
22 changes: 20 additions & 2 deletions common/src/refcount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::atomic::{Ordering::*, PyAtomic, Radium};
///
/// Going above this limit will abort your program (although not
/// necessarily) at _exactly_ `MAX_REFCOUNT + 1` references.
const MAX_REFCOUNT: usize = (isize::MAX) as usize;
const MAX_REFCOUNT: usize = isize::MAX as usize;

pub struct RefCount {
strong: PyAtomic<usize>,
Expand All @@ -18,6 +18,8 @@ impl Default for RefCount {
}

impl RefCount {
const MASK: usize = MAX_REFCOUNT;

pub fn new() -> Self {
RefCount {
strong: Radium::new(1),
Expand All @@ -33,7 +35,7 @@ impl RefCount {
pub fn inc(&self) {
let old_size = self.strong.fetch_add(1, Relaxed);

if old_size > MAX_REFCOUNT {
if old_size & Self::MASK == Self::MASK {
std::process::abort();
}
}
Expand All @@ -58,3 +60,19 @@ impl RefCount {
true
}
}

impl RefCount {
// move these functions out and give separated type once type range is stabilized

pub fn leak(&self) {
debug_assert!(!self.is_leaked());
const BIT_MARKER: usize = (std::isize::MAX as usize) + 1;
debug_assert_eq!(BIT_MARKER.count_ones(), 1);
debug_assert_eq!(BIT_MARKER.leading_zeros(), 0);
self.strong.fetch_add(BIT_MARKER, Relaxed);
}

pub fn is_leaked(&self) -> bool {
(self.strong.load(Acquire) as isize) < 0
}
}
8 changes: 5 additions & 3 deletions derive/src/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,8 @@ where
#py_name,
ctx.make_funcdef(#py_name, Self::#ident)
#doc
#build_func
#build_func,
ctx,
);
}
};
Expand Down Expand Up @@ -611,7 +612,7 @@ where
ident,
py_name.clone(),
quote! {
class.set_str_attr(#py_name, #value);
class.set_str_attr(#py_name, #value, ctx);
},
)
} else {
Expand Down Expand Up @@ -736,7 +737,8 @@ impl ToTokens for GetSetNursery {
::rustpython_vm::builtins::PyGetSet::new(#name.into(), class.clone())
.with_get(&Self::#getter)
#setter #deleter,
ctx.types.getset_type.clone(), None)
ctx.types.getset_type.clone(), None),
ctx
);
}
});
Expand Down
2 changes: 1 addition & 1 deletion derive/src/pymodule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ impl ModuleItem for ClassItem {
};
let class_new = quote_spanned!(ident.span() =>
let new_class = <#ident as ::rustpython_vm::class::PyClassImpl>::make_class(&vm.ctx);
new_class.set_str_attr("__module__", vm.new_pyobj(#module_name));
new_class.set_attr(rustpython_vm::identifier!(vm, __module__), vm.new_pyobj(#module_name));
);
(class_name, class_new)
};
Expand Down
13 changes: 9 additions & 4 deletions src/shell/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use rustpython_vm::{
builtins::{PyDictRef, PyStrRef},
function::ArgIterable,
PyResult, TryFromObject, VirtualMachine,
identifier, PyResult, TryFromObject, VirtualMachine,
};

pub struct ShellHelper<'vm> {
Expand Down Expand Up @@ -81,14 +81,19 @@ impl<'vm> ShellHelper<'vm> {
current = current.get_attr(attr.as_str(), self.vm).ok()?;
}

let current_iter = str_iter_method(current, "__dir__").ok()?;
let current_iter = str_iter_method(current, identifier!(self.vm, __dir__)).ok()?;

(last, current_iter, None)
} else {
// we need to get a variable based off of globals/builtins

let globals = str_iter_method(self.globals.clone().into(), "keys").ok()?;
let builtins = str_iter_method(self.vm.builtins.clone().into(), "__dir__").ok()?;
let globals =
str_iter_method(self.globals.clone().into(), identifier!(self.vm, keys)).ok()?;
let builtins = str_iter_method(
self.vm.builtins.clone().into(),
identifier!(self.vm, __dir__),
)
.ok()?;
(first, globals, Some(builtins))
};
Some((word_start, iter1.chain(iter2.into_iter().flatten())))
Expand Down
2 changes: 1 addition & 1 deletion stdlib/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ mod array {
fn typecode(&self, vm: &VirtualMachine) -> PyStrRef {
vm.ctx
.intern_str(self.read().typecode().to_string())
.to_str()
.to_owned()
}

#[pyproperty]
Expand Down
18 changes: 11 additions & 7 deletions stdlib/src/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ pub(crate) use math::make_module;
#[pymodule]
mod math {
use crate::vm::{
builtins::{try_bigint_to_f64, try_f64_to_bigint, PyFloat, PyInt, PyIntRef},
builtins::{try_bigint_to_f64, try_f64_to_bigint, PyFloat, PyInt, PyIntRef, PyStrInterned},
function::{ArgIntoFloat, ArgIterable, Either, OptionalArg, PosArgs},
AsObject, PyObject, PyObjectRef, PyRef, PyResult, VirtualMachine,
identifier, AsObject, PyObject, PyObjectRef, PyRef, PyResult, VirtualMachine,
};
use num_bigint::BigInt;
use num_traits::{One, Signed, Zero};
Expand Down Expand Up @@ -430,25 +430,29 @@ mod math {
}
}

fn try_magic_method(func_name: &str, vm: &VirtualMachine, value: &PyObject) -> PyResult {
fn try_magic_method(
func_name: &'static PyStrInterned,
vm: &VirtualMachine,
value: &PyObject,
) -> PyResult {
let method = vm.get_method_or_type_error(value.to_owned(), func_name, || {
format!(
"type '{}' doesn't define '{}' method",
value.class().name(),
func_name,
func_name.as_str(),
)
})?;
vm.invoke(&method, ())
}

#[pyfunction]
fn trunc(x: PyObjectRef, vm: &VirtualMachine) -> PyResult {
try_magic_method("__trunc__", vm, &x)
try_magic_method(identifier!(vm, __trunc__), vm, &x)
}

#[pyfunction]
fn ceil(x: PyObjectRef, vm: &VirtualMachine) -> PyResult {
let result_or_err = try_magic_method("__ceil__", vm, &x);
let result_or_err = try_magic_method(identifier!(vm, __ceil__), vm, &x);
if result_or_err.is_err() {
if let Ok(Some(v)) = x.try_to_f64(vm) {
let v = try_f64_to_bigint(v.ceil(), vm)?;
Expand All @@ -460,7 +464,7 @@ mod math {

#[pyfunction]
fn floor(x: PyObjectRef, vm: &VirtualMachine) -> PyResult {
let result_or_err = try_magic_method("__floor__", vm, &x);
let result_or_err = try_magic_method(identifier!(vm, __floor__), vm, &x);
if result_or_err.is_err() {
if let Ok(Some(v)) = x.try_to_f64(vm) {
let v = try_f64_to_bigint(v.floor(), vm)?;
Expand Down
2 changes: 1 addition & 1 deletion stdlib/src/pyexpat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ macro_rules! create_property {
move |this: &PyExpatLikeXmlParser, func: PyObjectRef| *this.$element.write() = func,
);

$attributes.insert($name.to_owned(), attr.into());
$attributes.insert($ctx.intern_str($name), attr.into());
};
}

Expand Down
8 changes: 5 additions & 3 deletions stdlib/src/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,11 @@ struct Selectable {
impl TryFromObject for Selectable {
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
let fno = obj.try_to_value(vm).or_else(|_| {
let meth = vm.get_method_or_type_error(obj.clone(), "fileno", || {
"select arg must be an int or object with a fileno() method".to_owned()
})?;
let meth = vm.get_method_or_type_error(
obj.clone(),
vm.ctx.interned_str("fileno").unwrap(),
|| "select arg must be an int or object with a fileno() method".to_owned(),
)?;
vm.invoke(&meth, ())?.try_into_value(vm)
})?;
Ok(Selectable { obj, fno })
Expand Down
14 changes: 7 additions & 7 deletions vm/src/builtins/bool.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use super::{PyInt, PyStrRef, PyTypeRef};
use crate::{
class::PyClassImpl, convert::ToPyObject, function::OptionalArg, types::Constructor, AsObject,
Context, PyObject, PyObjectRef, PyPayload, PyResult, TryFromBorrowedObject, VirtualMachine,
class::PyClassImpl, convert::ToPyObject, function::OptionalArg, identifier, types::Constructor,
AsObject, Context, PyObject, PyObjectRef, PyPayload, PyResult, TryFromBorrowedObject,
VirtualMachine,
};
use num_bigint::Sign;
use num_traits::Zero;
Expand Down Expand Up @@ -32,7 +33,7 @@ impl PyObjectRef {
if self.is(&vm.ctx.false_value) {
return Ok(false);
}
let rs_bool = match vm.get_method(self.clone(), "__bool__") {
let rs_bool = match vm.get_method(self.clone(), identifier!(vm, __bool__)) {
Some(method_or_err) => {
// If descriptor returns Error, propagate it further
let method = method_or_err?;
Expand All @@ -46,7 +47,7 @@ impl PyObjectRef {

get_value(&bool_obj)
}
None => match vm.get_method(self, "__len__") {
None => match vm.get_method(self, identifier!(vm, __len__)) {
Some(method_or_err) => {
let method = method_or_err?;
let bool_obj = vm.invoke(&method, ())?;
Expand Down Expand Up @@ -112,12 +113,11 @@ impl PyBool {
#[pymethod(magic)]
fn repr(zelf: bool, vm: &VirtualMachine) -> PyStrRef {
if zelf {
vm.ctx.true_str
vm.ctx.names.True
} else {
vm.ctx.false_str
vm.ctx.names.False
}
.to_owned()
.into_pyref()
}

#[pymethod(magic)]
Expand Down
18 changes: 7 additions & 11 deletions vm/src/builtins/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use super::{PyStrRef, PyTupleRef, PyTypeRef};
use crate::{
builtins::PyStrInterned,
bytecode::{self, BorrowedConstant, Constant, ConstantBag},
class::{PyClassImpl, StaticType},
convert::ToPyObject,
Expand Down Expand Up @@ -63,7 +64,7 @@ fn borrow_obj_constant(obj: &PyObject) -> BorrowedConstant<Literal> {
}

impl Constant for Literal {
type Name = PyStrRef;
type Name = &'static PyStrInterned;
fn borrow_constant(&self) -> BorrowedConstant<Self> {
borrow_obj_constant(&self.0)
}
Expand Down Expand Up @@ -103,8 +104,8 @@ impl ConstantBag for PyObjBag<'_> {
Literal(obj)
}

fn make_name(&self, name: &str) -> PyStrRef {
self.0.intern_str(name).to_str()
fn make_name(&self, name: &str) -> &'static PyStrInterned {
self.0.intern_str(name)
}
}

Expand Down Expand Up @@ -190,7 +191,7 @@ impl PyRef<PyCode> {

#[pyproperty]
fn co_filename(self) -> PyStrRef {
self.code.source_path.clone()
self.code.source_path.to_owned()
}

#[pyproperty]
Expand All @@ -211,7 +212,7 @@ impl PyRef<PyCode> {

#[pyproperty]
fn co_name(self) -> PyStrRef {
self.code.obj_name.clone()
self.code.obj_name.to_owned()
}

#[pyproperty]
Expand All @@ -221,12 +222,7 @@ impl PyRef<PyCode> {

#[pyproperty]
pub fn co_varnames(self, vm: &VirtualMachine) -> PyTupleRef {
let varnames = self
.code
.varnames
.iter()
.map(|s| s.clone().into())
.collect();
let varnames = self.code.varnames.iter().map(|s| s.to_object()).collect();
vm.ctx.new_tuple(varnames)
}
}
Expand Down
3 changes: 2 additions & 1 deletion vm/src/builtins/complex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{
PyArithmeticValue::{self, *},
PyComparisonValue,
},
identifier,
types::{Comparable, Constructor, Hashable, PyComparisonOp},
AsObject, Context, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
};
Expand Down Expand Up @@ -48,7 +49,7 @@ impl PyObjectRef {
if let Some(complex) = self.payload_if_exact::<PyComplex>(vm) {
return Ok(Some((complex.value, true)));
}
if let Some(method) = vm.get_method(self.clone(), "__complex__") {
if let Some(method) = vm.get_method(self.clone(), identifier!(vm, __complex__)) {
let result = vm.invoke(&method?, ())?;
// TODO: returning strict subclasses of complex in __complex__ is deprecated
return match result.payload::<PyComplex>() {
Expand Down
Loading