Skip to content

use interned str for builtin function names #4853

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
Apr 7, 2023
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 derive-impl/src/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ where
quote_spanned! { ident.span() =>
class.set_attr(
ctx.names.#name_ident,
ctx.make_func_def(#py_name, Self::#ident)
ctx.make_func_def(ctx.intern_str(#py_name), Self::#ident)
#doc
#build_func
.into(),
Expand All @@ -639,7 +639,7 @@ where
quote_spanned! { ident.span() =>
class.set_str_attr(
#py_name,
ctx.make_func_def(#py_name, Self::#ident)
ctx.make_func_def(ctx.intern_str(#py_name), Self::#ident)
#doc
#build_func,
ctx,
Expand Down
2 changes: 1 addition & 1 deletion derive-impl/src/pymodule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ impl ModuleItem for FunctionItem {
};
let doc = quote!(.with_doc(#doc.to_owned(), &vm.ctx));
let new_func = quote_spanned!(ident.span()=>
vm.ctx.make_func_def(#py_name, #ident)
vm.ctx.make_func_def(vm.ctx.intern_str(#py_name), #ident)
#doc
.into_function()
.with_module(vm.new_pyobj(#module.to_owned()))
Expand Down
14 changes: 7 additions & 7 deletions vm/src/builtins/builtin_func.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{type_, PyClassMethod, PyStaticMethod, PyStr, PyStrRef, PyType};
use super::{type_, PyClassMethod, PyStaticMethod, PyStr, PyStrInterned, PyStrRef, PyType};
use crate::{
builtins::PyBoundMethod,
class::PyClassImpl,
Expand All @@ -10,12 +10,12 @@ use std::fmt;

pub struct PyNativeFuncDef {
pub func: PyNativeFunc,
pub name: PyStrRef,
pub name: &'static PyStrInterned,
pub doc: Option<PyStrRef>,
}

impl PyNativeFuncDef {
pub fn new(func: PyNativeFunc, name: PyStrRef) -> Self {
pub fn new(func: PyNativeFunc, name: &'static PyStrInterned) -> Self {
Self {
func,
name,
Expand Down Expand Up @@ -122,7 +122,7 @@ impl PyBuiltinFunction {
}
#[pygetset(magic)]
fn name(&self) -> PyStrRef {
self.value.name.clone()
self.value.name.to_owned()
}
#[pygetset(magic)]
fn qualname(&self) -> PyStrRef {
Expand Down Expand Up @@ -217,7 +217,7 @@ impl Callable for PyBuiltinMethod {

impl PyBuiltinMethod {
pub fn new_ref<F, FKind>(
name: impl Into<PyStr>,
name: &'static PyStrInterned,
class: &'static Py<PyType>,
f: F,
ctx: &Context,
Expand All @@ -236,7 +236,7 @@ impl PyBuiltinMethod {
impl PyBuiltinMethod {
#[pygetset(magic)]
fn name(&self) -> PyStrRef {
self.value.name.clone()
self.value.name.to_owned()
}
#[pygetset(magic)]
fn qualname(&self) -> String {
Expand All @@ -260,7 +260,7 @@ impl PyBuiltinMethod {
) -> (Option<PyObjectRef>, (Option<PyObjectRef>, PyStrRef)) {
let builtins_getattr = vm.builtins.get_attr("getattr", vm).ok();
let classname = vm.builtins.get_attr(&self.class.__name__(vm), vm).ok();
(builtins_getattr, (classname, self.value.name.clone()))
(builtins_getattr, (classname, self.value.name.to_owned()))
}
}

Expand Down
4 changes: 2 additions & 2 deletions vm/src/builtins/staticmethod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{PyStr, PyType, PyTypeRef};
use super::{PyStr, PyStrInterned, PyType, PyTypeRef};
use crate::{
builtins::builtin_func::PyBuiltinMethod,
class::PyClassImpl,
Expand Down Expand Up @@ -75,7 +75,7 @@ impl PyStaticMethod {

impl PyStaticMethod {
pub fn new_builtin_ref<F, FKind>(
name: impl Into<PyStr>,
name: &'static PyStrInterned,
class: &'static Py<PyType>,
f: F,
ctx: &Context,
Expand Down
12 changes: 7 additions & 5 deletions vm/src/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::{
builtins::{PyBaseObject, PyBoundMethod, PyType, PyTypeRef},
identifier,
object::{Py, PyObjectRef},
object::Py,
types::{hash_not_implemented, PyTypeFlags, PyTypeSlots},
vm::Context,
};
Expand Down Expand Up @@ -99,10 +99,12 @@ pub trait PyClassImpl: PyClassDef {
);
}
if class.slots.new.load().is_some() {
let bound: PyObjectRef =
PyBoundMethod::new_ref(class.to_owned().into(), ctx.slot_new_wrapper.clone(), ctx)
.into();
class.set_attr(identifier!(ctx, __new__), bound);
let bound = PyBoundMethod::new_ref(
class.to_owned().into(),
ctx.slot_new_wrapper.clone().into(),
ctx,
);
class.set_attr(identifier!(ctx, __new__), bound.into());
}

if class.slots.hash.load().map_or(0, |h| h as usize) == hash_not_implemented as usize {
Expand Down
6 changes: 3 additions & 3 deletions vm/src/exceptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,7 @@ impl ExceptionZoo {
extend_exception!(PyLookupError, ctx, excs.lookup_error);
extend_exception!(PyIndexError, ctx, excs.index_error);
extend_exception!(PyKeyError, ctx, excs.key_error, {
"__str__" => ctx.new_method("__str__", excs.key_error, key_error_str),
"__str__" => ctx.new_method(identifier!(ctx, __str__), excs.key_error, key_error_str),
});

extend_exception!(PyMemoryError, ctx, excs.memory_error);
Expand Down Expand Up @@ -786,8 +786,8 @@ impl ExceptionZoo {
"filename" => ctx.none(),
// second exception filename
"filename2" => ctx.none(),
"__str__" => ctx.new_method("__str__", excs.os_error, os_error_str),
"__reduce__" => ctx.new_method("__reduce__", excs.os_error, os_error_reduce),
"__str__" => ctx.new_method(identifier!(ctx, __str__), excs.os_error, os_error_str),
"__reduce__" => ctx.new_method(identifier!(ctx, __reduce__), excs.os_error, os_error_reduce),
});
// TODO: this isn't really accurate
#[cfg(windows)]
Expand Down
2 changes: 1 addition & 1 deletion vm/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ macro_rules! named_function {
let ctx: &$crate::Context = &$ctx;
$crate::__exports::paste::expr! {
ctx.make_func_def(
stringify!($func),
ctx.intern_str(stringify!($func)),
[<$module _ $func>],
)
.into_function()
Expand Down
19 changes: 9 additions & 10 deletions vm/src/vm/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub struct Context {
pub int_cache_pool: Vec<PyIntRef>,
// there should only be exact objects of str in here, no non-str objects and no subclasses
pub(crate) string_pool: StringPool,
pub(crate) slot_new_wrapper: PyObjectRef,
pub(crate) slot_new_wrapper: PyRef<PyBuiltinFunction>,
pub names: ConstName,
}

Expand Down Expand Up @@ -288,11 +288,9 @@ impl Context {
let names = unsafe { ConstName::new(&string_pool, &types.str_type.to_owned()) };

let slot_new_wrapper = create_object(
PyNativeFuncDef::new(PyType::__new__.into_func(), names.__new__.to_owned())
.into_function(),
PyNativeFuncDef::new(PyType::__new__.into_func(), names.__new__).into_function(),
types.builtin_function_or_method_type,
)
.into();
);

let empty_str = unsafe { string_pool.intern("", types.str_type.to_owned()) }.to_owned();
let empty_bytes = create_object(PyBytes::from(Vec::new()), types.bytes_type);
Expand Down Expand Up @@ -491,11 +489,11 @@ impl Context {
}

#[inline]
pub fn make_func_def<F, FKind>(&self, name: impl Into<PyStr>, f: F) -> PyNativeFuncDef
pub fn make_func_def<F, FKind>(&self, name: &'static PyStrInterned, f: F) -> PyNativeFuncDef
where
F: IntoPyNativeFunc<FKind>,
{
PyNativeFuncDef::new(f.into_func(), PyStr::new_ref(name, self))
PyNativeFuncDef::new(f.into_func(), name)
}

#[inline]
Expand Down Expand Up @@ -531,16 +529,17 @@ impl Context {
}

// #[deprecated]
pub fn new_function<F, FKind>(&self, name: impl Into<PyStr>, f: F) -> PyRef<PyBuiltinFunction>
pub fn new_function<F, FKind>(&self, name: &str, f: F) -> PyRef<PyBuiltinFunction>
where
F: IntoPyNativeFunc<FKind>,
{
self.make_func_def(name, f).build_function(self)
self.make_func_def(self.intern_str(name), f)
.build_function(self)
}

pub fn new_method<F, FKind>(
&self,
name: impl Into<PyStr>,
name: &'static PyStrInterned,
class: &'static Py<PyType>,
f: F,
) -> PyRef<PyBuiltinMethod>
Expand Down
2 changes: 1 addition & 1 deletion wasm/lib/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ pub fn js_to_py(vm: &VirtualMachine, js_val: JsValue) -> PyObjectRef {
let func = js_sys::Function::from(js_val);
vm.ctx
.new_function(
String::from(func.name()),
String::from(func.name()).as_str(),
move |args: FuncArgs, vm: &VirtualMachine| -> PyResult {
let this = Object::new();
for (k, v) in args.kwargs {
Expand Down
4 changes: 2 additions & 2 deletions wasm/lib/src/wasm_builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ pub fn make_stdout_object(
{}
));
let write_method = ctx.new_method(
"write",
ctx.intern_str("write"),
cls,
move |_self: PyObjectRef, data: PyStrRef, vm: &VirtualMachine| -> PyResult<()> {
write_f(data.as_str(), vm)
},
);
let flush_method = ctx.new_method("flush", cls, |_self: PyObjectRef| {});
let flush_method = ctx.new_method(ctx.intern_str("flush"), cls, |_self: PyObjectRef| {});
extend_class!(ctx, cls, {
"write" => write_method,
"flush" => flush_method,
Expand Down