Skip to content

Commit 8e20e23

Browse files
authored
Merge pull request RustPython#3750 from youknowone/as-mapping-const
use static reference for AsMapping / AsSequence
2 parents 8ab4e77 + 4546661 commit 8e20e23

File tree

25 files changed

+574
-640
lines changed

25 files changed

+574
-640
lines changed

Cargo.lock

Lines changed: 11 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

benches/benchmarks/fannkuch.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"""
2+
The Computer Language Benchmarks Game
3+
http://benchmarksgame.alioth.debian.org/
4+
5+
Contributed by Sokolov Yura, modified by Tupteq.
6+
"""
7+
8+
# import pyperf
9+
10+
11+
DEFAULT_ARG = 9
12+
13+
14+
def fannkuch(n):
15+
count = list(range(1, n + 1))
16+
max_flips = 0
17+
m = n - 1
18+
r = n
19+
perm1 = list(range(n))
20+
perm = list(range(n))
21+
perm1_ins = perm1.insert
22+
perm1_pop = perm1.pop
23+
24+
while 1:
25+
while r != 1:
26+
count[r - 1] = r
27+
r -= 1
28+
29+
if perm1[0] != 0 and perm1[m] != m:
30+
perm = perm1[:]
31+
flips_count = 0
32+
k = perm[0]
33+
while k:
34+
perm[:k + 1] = perm[k::-1]
35+
flips_count += 1
36+
k = perm[0]
37+
38+
if flips_count > max_flips:
39+
max_flips = flips_count
40+
41+
while r != n:
42+
perm1_ins(r, perm1_pop(0))
43+
count[r] -= 1
44+
if count[r] > 0:
45+
break
46+
r += 1
47+
else:
48+
return max_flips
49+
50+
51+
if __name__ == "__main__":
52+
#runner = pyperf.Runner()
53+
arg = DEFAULT_ARG
54+
#runner.bench_func('fannkuch', fannkuch, arg)
55+
fannkuch(arg)

derive/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ proc-macro = true
1212

1313
[dependencies]
1414
syn = { version = "1.0.91", features = ["full", "extra-traits"] }
15-
syn-ext = { version = "0.3.1", features = ["full"] }
15+
syn-ext = { version = "0.4.0", features = ["full"] }
1616
quote = "1.0.18"
1717
proc-macro2 = "1.0.37"
1818
rustpython-compiler = { path = "../compiler/porcelain", version = "0.1.1" }

derive/src/pyclass.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -548,25 +548,28 @@ where
548548
Item: ItemLike + ToTokens + GetIdent,
549549
{
550550
fn gen_impl_item(&self, args: ImplItemArgs<'_, Item>) -> Result<()> {
551-
let func = args
552-
.item
553-
.function_or_method()
554-
.map_err(|_| self.new_syn_error(args.item.span(), "can only be on a method"))?;
555-
let ident = &func.sig().ident;
551+
let (ident, span) = if let Ok(c) = args.item.constant() {
552+
(c.ident(), c.span())
553+
} else if let Ok(f) = args.item.function_or_method() {
554+
(&f.sig().ident, f.span())
555+
} else {
556+
return Err(self.new_syn_error(args.item.span(), "can only be on a method"));
557+
};
556558

557559
let item_attr = args.attrs.remove(self.index());
558560
let item_meta = SlotItemMeta::from_attr(ident.clone(), &item_attr)?;
559561

560562
let slot_ident = item_meta.slot_name()?;
563+
let slot_ident = Ident::new(&slot_ident.to_string().to_lowercase(), slot_ident.span());
561564
let slot_name = slot_ident.to_string();
562565
let tokens = {
563566
const NON_ATOMIC_SLOTS: &[&str] = &["as_buffer"];
564567
if NON_ATOMIC_SLOTS.contains(&slot_name.as_str()) {
565-
quote_spanned! { func.span() =>
568+
quote_spanned! { span =>
566569
slots.#slot_ident = Some(Self::#ident as _);
567570
}
568571
} else {
569-
quote_spanned! { func.span() =>
572+
quote_spanned! { span =>
570573
slots.#slot_ident.store(Some(Self::#ident as _));
571574
}
572575
}
@@ -599,11 +602,11 @@ where
599602
Ok(py_name)
600603
};
601604
let (ident, py_name, tokens) =
602-
if args.item.function_or_method().is_ok() || args.item.is_const() {
605+
if args.item.function_or_method().is_ok() || args.item.constant().is_ok() {
603606
let ident = args.item.get_ident().unwrap();
604607
let py_name = get_py_name(&attr, ident)?;
605608

606-
let value = if args.item.is_const() {
609+
let value = if args.item.constant().is_ok() {
607610
// TODO: ctx.new_value
608611
quote_spanned!(ident.span() => ctx.new_int(Self::#ident).into())
609612
} else {

stdlib/src/array.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1238,8 +1238,8 @@ mod array {
12381238
},
12391239
};
12401240

1241-
impl PyArray {
1242-
const MAPPING_METHODS: PyMappingMethods = PyMappingMethods {
1241+
impl AsMapping for PyArray {
1242+
const AS_MAPPING: PyMappingMethods = PyMappingMethods {
12431243
length: Some(|mapping, _vm| Ok(Self::mapping_downcast(mapping).len())),
12441244
subscript: Some(|mapping, needle, vm| {
12451245
Self::mapping_downcast(mapping)._getitem(needle, vm)
@@ -1255,12 +1255,6 @@ mod array {
12551255
};
12561256
}
12571257

1258-
impl AsMapping for PyArray {
1259-
fn as_mapping(_zelf: &Py<Self>, _vm: &VirtualMachine) -> PyMappingMethods {
1260-
Self::MAPPING_METHODS
1261-
}
1262-
}
1263-
12641258
impl Iterable for PyArray {
12651259
fn iter(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult {
12661260
Ok(PyArrayIter {

vm/src/builtins/bytearray.rs

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use crate::{
3636
TryFromBorrowedObject, TryFromObject, VirtualMachine,
3737
};
3838
use bstr::ByteSlice;
39-
use std::{borrow::Cow, mem::size_of};
39+
use std::mem::size_of;
4040

4141
#[pyclass(module = false, name = "bytearray")]
4242
#[derive(Debug, Default)]
@@ -690,23 +690,6 @@ impl PyByteArray {
690690
}
691691
}
692692

693-
impl PyByteArray {
694-
const MAPPING_METHODS: PyMappingMethods = PyMappingMethods {
695-
length: Some(|mapping, _vm| Ok(Self::mapping_downcast(mapping).len())),
696-
subscript: Some(|mapping, needle, vm| {
697-
Self::mapping_downcast(mapping).getitem(needle.to_owned(), vm)
698-
}),
699-
ass_subscript: Some(|mapping, needle, value, vm| {
700-
let zelf = Self::mapping_downcast(mapping);
701-
if let Some(value) = value {
702-
Self::setitem(zelf.to_owned(), needle.to_owned(), value, vm)
703-
} else {
704-
zelf.delitem(needle.to_owned(), vm)
705-
}
706-
}),
707-
};
708-
}
709-
710693
impl Constructor for PyByteArray {
711694
type Args = FuncArgs;
712695

@@ -784,19 +767,24 @@ impl<'a> BufferResizeGuard<'a> for PyByteArray {
784767
}
785768

786769
impl AsMapping for PyByteArray {
787-
fn as_mapping(_zelf: &crate::Py<Self>, _vm: &VirtualMachine) -> PyMappingMethods {
788-
Self::MAPPING_METHODS
789-
}
770+
const AS_MAPPING: PyMappingMethods = PyMappingMethods {
771+
length: Some(|mapping, _vm| Ok(Self::mapping_downcast(mapping).len())),
772+
subscript: Some(|mapping, needle, vm| {
773+
Self::mapping_downcast(mapping).getitem(needle.to_owned(), vm)
774+
}),
775+
ass_subscript: Some(|mapping, needle, value, vm| {
776+
let zelf = Self::mapping_downcast(mapping);
777+
if let Some(value) = value {
778+
Self::setitem(zelf.to_owned(), needle.to_owned(), value, vm)
779+
} else {
780+
zelf.delitem(needle.to_owned(), vm)
781+
}
782+
}),
783+
};
790784
}
791785

792786
impl AsSequence for PyByteArray {
793-
fn as_sequence(_zelf: &Py<Self>, _vm: &VirtualMachine) -> Cow<'static, PySequenceMethods> {
794-
Cow::Borrowed(&Self::SEQUENCE_METHODS)
795-
}
796-
}
797-
798-
impl PyByteArray {
799-
const SEQUENCE_METHODS: PySequenceMethods = PySequenceMethods {
787+
const AS_SEQUENCE: PySequenceMethods = PySequenceMethods {
800788
length: Some(|seq, _vm| Ok(Self::sequence_downcast(seq).len())),
801789
concat: Some(|seq, other, vm| {
802790
Self::sequence_downcast(seq)

vm/src/builtins/bytes.rs

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use crate::{
2525
TryFromBorrowedObject, TryFromObject, VirtualMachine,
2626
};
2727
use bstr::ByteSlice;
28-
use std::{borrow::Cow, mem::size_of, ops::Deref};
28+
use std::{mem::size_of, ops::Deref};
2929

3030
#[pyclass(module = false, name = "bytes")]
3131
#[derive(Clone, Debug)]
@@ -550,14 +550,6 @@ impl PyBytes {
550550
}
551551
}
552552

553-
impl PyBytes {
554-
const MAPPING_METHODS: PyMappingMethods = PyMappingMethods {
555-
length: Some(|mapping, _vm| Ok(Self::mapping_downcast(mapping).len())),
556-
subscript: Some(|mapping, needle, vm| Self::mapping_downcast(mapping)._getitem(needle, vm)),
557-
ass_subscript: None,
558-
};
559-
}
560-
561553
static BUFFER_METHODS: BufferMethods = BufferMethods {
562554
obj_bytes: |buffer| buffer.obj_as::<PyBytes>().as_bytes().into(),
563555
obj_bytes_mut: |_| panic!(),
@@ -577,19 +569,15 @@ impl AsBuffer for PyBytes {
577569
}
578570

579571
impl AsMapping for PyBytes {
580-
fn as_mapping(_zelf: &Py<Self>, _vm: &VirtualMachine) -> PyMappingMethods {
581-
Self::MAPPING_METHODS
582-
}
572+
const AS_MAPPING: PyMappingMethods = PyMappingMethods {
573+
length: Some(|mapping, _vm| Ok(Self::mapping_downcast(mapping).len())),
574+
subscript: Some(|mapping, needle, vm| Self::mapping_downcast(mapping)._getitem(needle, vm)),
575+
ass_subscript: None,
576+
};
583577
}
584578

585579
impl AsSequence for PyBytes {
586-
fn as_sequence(_zelf: &Py<Self>, _vm: &VirtualMachine) -> Cow<'static, PySequenceMethods> {
587-
Cow::Borrowed(&Self::SEQUENCE_METHODS)
588-
}
589-
}
590-
591-
impl PyBytes {
592-
const SEQUENCE_METHODS: PySequenceMethods = PySequenceMethods {
580+
const AS_SEQUENCE: PySequenceMethods = PySequenceMethods {
593581
length: Some(|seq, _vm| Ok(Self::sequence_downcast(seq).len())),
594582
concat: Some(|seq, other, vm| {
595583
Self::sequence_downcast(seq)
@@ -614,7 +602,7 @@ impl PyBytes {
614602
let other = <Either<PyBytesInner, PyIntRef>>::try_from_object(vm, other.to_owned())?;
615603
Self::sequence_downcast(seq).contains(other, vm)
616604
}),
617-
..*PySequenceMethods::not_implemented()
605+
..PySequenceMethods::NOT_IMPLEMENTED
618606
};
619607
}
620608

0 commit comments

Comments
 (0)