diff --git a/run_profile.sh b/run_profile.sh index fd2e0594..c868aabc 100644 --- a/run_profile.sh +++ b/run_profile.sh @@ -1,5 +1,5 @@ clang++ -pg -O2 -std=c++17 -fno-rtti -stdlib=libc++ -Wall -o pocketpy src/main.cpp -time ./pocketpy benchmarks/sort.py +time ./pocketpy benchmarks/primes.py mv benchmarks/gmon.out . gprof pocketpy gmon.out > gprof.txt rm gmon.out diff --git a/src/pocketpy.h b/src/pocketpy.h index 32d32c52..e3325176 100644 --- a/src/pocketpy.h +++ b/src/pocketpy.h @@ -389,19 +389,9 @@ inline void init_builtins(VM* _vm) { }); _vm->bind_method<1>("str", "__getitem__", [](VM* vm, ArgsView args) { - const Str& self = _CAST(Str&, args[0]); - - if(is_type(args[1], vm->tp_slice)){ - const Slice& s = _CAST(Slice&, args[1]); - int start, stop, step; - vm->parse_int_slice(s, self.u8_length(), start, stop, step); - return VAR(self.u8_slice(start, stop, step)); - } - - int index = CAST(int, args[1]); - index = vm->normalized_index(index, self.u8_length()); - return VAR(self.u8_getitem(index)); + return PyStrGetItem(vm, args[0], args[1]); }); + _vm->_type_info("str")->m__getitem__ = PyStrGetItem; _vm->bind_method<1>("str", "__gt__", [](VM* vm, ArgsView args) { const Str& self = _CAST(Str&, args[0]); diff --git a/src/vm.h b/src/vm.h index b2e19f7c..8b173e86 100644 --- a/src/vm.h +++ b/src/vm.h @@ -1263,4 +1263,19 @@ inline PyObject* PyListSetItem(VM* vm, PyObject* obj, PyObject* index, PyObject* return vm->None; } +inline PyObject* PyStrGetItem(VM* vm, PyObject* obj, PyObject* index){ + const Str& self = _CAST(Str&, obj); + + if(is_type(index, vm->tp_slice)){ + const Slice& s = _CAST(Slice&, index); + int start, stop, step; + vm->parse_int_slice(s, self.u8_length(), start, stop, step); + return VAR(self.u8_slice(start, stop, step)); + } + + int i = CAST(int, index); + i = vm->normalized_index(i, self.u8_length()); + return VAR(self.u8_getitem(i)); +} + } // namespace pkpy \ No newline at end of file