Skip to content

Commit 7da6966

Browse files
authored
Reduce number of string clones when compiling python source code (#8103)
1 parent a135593 commit 7da6966

21 files changed

Lines changed: 63 additions & 69 deletions

File tree

‎Lib/test/test_thread.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def test_nt_and_posix_stack_size(self):
115115

116116
thread.stack_size(0)
117117

118-
@unittest.skipIf(__import__("sys").platform == "linux", "TODO: RUSTPYTHON; Flakey on CI")
118+
@unittest.skipIf(__import__("sys").platform in ("linux", "win32"), "TODO: RUSTPYTHON; Flakey on CI")
119119
def test__count(self):
120120
# Test the _count() function.
121121
orig = thread._count()

‎benches/execution.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ fn bench_rustpython_code(b: &mut Bencher, name: &str, source: &str) {
3030
// Note: bench_cpython is both compiling and executing the code.
3131
// As such we compile the code in the benchmark loop as well.
3232
b.iter(|| {
33-
let code = vm.compile(source, Mode::Exec, name.to_owned()).unwrap();
33+
let code = vm.compile(source, Mode::Exec, name).unwrap();
3434
let scope = vm.new_scope_with_builtins();
3535
let res: PyResult = vm.run_code_obj(code.clone(), scope);
3636
vm.unwrap_pyresult(res);

‎benches/microbenchmarks.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,10 @@ fn bench_rustpython_code(group: &mut BenchmarkGroup<WallTime>, bench: &MicroBenc
118118
let interp = builder.add_native_modules(&defs).build();
119119
interp.enter(|vm| {
120120
let setup_code = vm
121-
.compile(&bench.setup, Mode::Exec, bench.name.to_owned())
121+
.compile(&bench.setup, Mode::Exec, &bench.name)
122122
.expect("Error compiling setup code");
123123
let bench_code = vm
124-
.compile(&bench.code, Mode::Exec, bench.name.to_owned())
124+
.compile(&bench.code, Mode::Exec, &bench.name)
125125
.expect("Error compiling bench code");
126126

127127
let bench_func = |scope| {

‎crates/capi/src/ceval.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub unsafe extern "C" fn Py_CompileString(
3838
}
3939
};
4040

41-
vm.compile(code, mode, filename.to_owned())
41+
vm.compile(code, mode, filename)
4242
.map_err(|err| vm.new_syntax_error(&err, Some(code)))
4343
})
4444
}

‎crates/stdlib/src/_opcode.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ mod tests {
192192
///
193193
/// Memory addresses in the output are replaced with `0xdeadbeef` for consistency.
194194
fn dis(source: &str) -> String {
195-
let fname = String::from("<?>");
195+
const FNAME: &str = "<?>";
196196

197197
let builder = vm::Interpreter::builder(Default::default());
198198
let stdlib_defs = crate::stdlib_module_defs(&builder.ctx);
@@ -204,7 +204,7 @@ mod tests {
204204
interp.enter(|vm| {
205205
let scope = vm.new_scope_with_builtins();
206206
let code_obj = vm
207-
.compile(source.trim(), Mode::Exec, fname.clone())
207+
.compile(source.trim(), Mode::Exec, FNAME)
208208
.map_err(|err| vm.new_syntax_error(&err, Some(source)))
209209
.unwrap();
210210
scope.globals.set_item("code", code_obj.into(), vm).unwrap();
@@ -227,7 +227,7 @@ output = re.sub(r'(<code object \w+ at )0x[0-9a-fA-F]+', r'\g<1>0xdeadbeef', tmp
227227
"#;
228228

229229
let py_code_obj = vm
230-
.compile(py_source, Mode::Exec, fname)
230+
.compile(py_source, Mode::Exec, FNAME)
231231
.map_err(|err| vm.new_syntax_error(&err, Some(py_source)))
232232
.unwrap();
233233

‎crates/vm/src/builtins/getset.rs‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
/*! Python `attribute` descriptor class. (PyGetSet)
1+
//! Python `attribute` descriptor class. (PyGetSet)
22
3-
*/
43
use super::PyType;
54
use crate::{
65
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyResult, VirtualMachine,
@@ -71,9 +70,9 @@ impl GetDescriptor for PyGetSet {
7170

7271
impl PyGetSet {
7372
#[must_use]
74-
pub fn new(name: String, class: &'static Py<PyType>) -> Self {
73+
pub fn new(name: &str, class: &'static Py<PyType>) -> Self {
7574
Self {
76-
name,
75+
name: name.into(),
7776
class: PointerSlot::from(class),
7877
getter: None,
7978
setter: None,

‎crates/vm/src/eval.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::{PyResult, VirtualMachine, compiler, scope::Scope};
22

33
pub fn eval(vm: &VirtualMachine, source: &str, scope: Scope, source_path: &str) -> PyResult {
4-
match vm.compile(source, compiler::Mode::Eval, source_path.to_owned()) {
4+
match vm.compile(source, compiler::Mode::Eval, source_path) {
55
Ok(bytecode) => {
66
debug!("Code object: {bytecode:?}");
77
vm.run_code_obj(bytecode, scope)

‎crates/vm/src/import.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ pub fn import_builtin(vm: &VirtualMachine, module_name: &str) -> PyResult {
131131
pub fn import_file(
132132
vm: &VirtualMachine,
133133
module_name: &str,
134-
file_path: String,
134+
file_path: &str,
135135
content: &str,
136136
) -> PyResult {
137137
let code = vm
@@ -151,7 +151,7 @@ pub fn import_source(vm: &VirtualMachine, module_name: &str, content: &str) -> P
151151
.compile_with_opts(
152152
content,
153153
crate::compiler::Mode::Exec,
154-
"<source>".to_owned(),
154+
"<source>",
155155
vm.compile_opts(),
156156
)
157157
.map_err(|err| vm.new_syntax_error(&err, Some(content)))?;

‎crates/vm/src/stdlib/builtins.rs‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -424,12 +424,7 @@ mod builtins {
424424
opts.optimize = optimize;
425425

426426
let code = vm
427-
.compile_with_opts(
428-
source,
429-
mode,
430-
filename.to_string_lossy().into_owned(),
431-
opts,
432-
)
427+
.compile_with_opts(source, mode, &filename.to_string_lossy(), opts)
433428
.map_err(|err| {
434429
(err, Some(source), allow_incomplete).to_pyexception(vm)
435430
})?;
@@ -632,7 +627,7 @@ mod builtins {
632627
#[cfg(feature = "rustpython-compiler")]
633628
Either::A(string) => {
634629
let source = string.as_str();
635-
vm.compile(source, mode, "<string>".to_owned())
630+
vm.compile(source, mode, "<string>")
636631
.map_err(|err| vm.new_syntax_error(&err, Some(source)))?
637632
}
638633
#[cfg(not(feature = "rustpython-compiler"))]

‎crates/vm/src/stdlib/sys.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,7 @@ pub mod sys {
750750
handle
751751
.read_to_string(&mut source)
752752
.map_err(|e| vm.new_os_error(format!("Error reading from stdin: {e}")))?;
753-
vm.compile(&source, crate::compiler::Mode::Single, "<stdin>".to_owned())
753+
vm.compile(&source, crate::compiler::Mode::Single, "<stdin>")
754754
.map_err(|e| vm.new_os_error(format!("Error running stdin: {e}")))?;
755755
Ok(())
756756
}

0 commit comments

Comments
 (0)