mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 03:20:18 +00:00
refactor
This commit is contained in:
parent
6cff9ff9ec
commit
b0dc1e4a69
@ -8,8 +8,8 @@ with open("include/pocketpy/opcodes.h", "rt", encoding='utf-8') as f:
|
|||||||
pipeline = [
|
pipeline = [
|
||||||
["config.h", "export.h", "common.h", "memory.h", "vector.h", "str.h", "tuplelist.h", "namedict.h", "error.h"],
|
["config.h", "export.h", "common.h", "memory.h", "vector.h", "str.h", "tuplelist.h", "namedict.h", "error.h"],
|
||||||
["obj.h", "dict.h", "codeobject.h", "frame.h"],
|
["obj.h", "dict.h", "codeobject.h", "frame.h"],
|
||||||
["gc.h", "vm.h", "ceval.h", "lexer.h", "expr.h", "compiler.h", "repl.h"],
|
["gc.h", "vm.h", "ceval.h", "lexer.h", "expr.h", "compiler.h", "repl.h", "profiler.h"],
|
||||||
["_generated.h", "cffi.h", "bindings.h", "iter.h", "base64.h", "csv.h", "collections.h", "line_profiler.h", "array2d.h", "dataclasses.h", "random.h", "linalg.h", "easing.h", "io.h", "modules.h"],
|
["_generated.h", "cffi.h", "bindings.h", "iter.h", "base64.h", "csv.h", "collections.h", "array2d.h", "dataclasses.h", "random.h", "linalg.h", "easing.h", "io.h", "modules.h"],
|
||||||
["pocketpy.h", "pocketpy_c.h"]
|
["pocketpy.h", "pocketpy_c.h"]
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -10,5 +10,6 @@ void add_module_math(VM* vm);
|
|||||||
void add_module_traceback(VM* vm);
|
void add_module_traceback(VM* vm);
|
||||||
void add_module_dis(VM* vm);
|
void add_module_dis(VM* vm);
|
||||||
void add_module_gc(VM* vm);
|
void add_module_gc(VM* vm);
|
||||||
|
void add_module_line_profiler(VM* vm);
|
||||||
|
|
||||||
} // namespace pkpy
|
} // namespace pkpy
|
@ -16,5 +16,4 @@
|
|||||||
#include "csv.h"
|
#include "csv.h"
|
||||||
#include "dataclasses.h"
|
#include "dataclasses.h"
|
||||||
#include "array2d.h"
|
#include "array2d.h"
|
||||||
#include "line_profiler.h"
|
|
||||||
#include "modules.h"
|
#include "modules.h"
|
||||||
|
@ -150,10 +150,7 @@ public:
|
|||||||
// cached code objects for FSTRING_EVAL
|
// cached code objects for FSTRING_EVAL
|
||||||
std::map<std::string_view, CodeObject_> _cached_codes;
|
std::map<std::string_view, CodeObject_> _cached_codes;
|
||||||
|
|
||||||
// for user defined logic
|
|
||||||
void (*_ceval_on_step)(VM*, Frame*, Bytecode bc) = nullptr;
|
void (*_ceval_on_step)(VM*, Frame*, Bytecode bc) = nullptr;
|
||||||
// for line_profiler (users not to use this)
|
|
||||||
void (*_ceval_on_step_profile)(VM*, Frame*, Bytecode bc) = nullptr;
|
|
||||||
|
|
||||||
PrintFunc _stdout;
|
PrintFunc _stdout;
|
||||||
PrintFunc _stderr;
|
PrintFunc _stderr;
|
||||||
|
@ -67,8 +67,7 @@ PyObject* VM::_run_top_frame(){
|
|||||||
{
|
{
|
||||||
|
|
||||||
#define CEVAL_STEP_CALLBACK() \
|
#define CEVAL_STEP_CALLBACK() \
|
||||||
if(_ceval_on_step) _ceval_on_step(this, frame.get(), byte); \
|
if(_ceval_on_step) _ceval_on_step(this, frame.get(), byte);
|
||||||
if(_ceval_on_step_profile) _ceval_on_step_profile(this, frame.get(), byte);
|
|
||||||
|
|
||||||
#define DISPATCH_OP_CALL() { frame = top_frame(); goto __NEXT_FRAME; }
|
#define DISPATCH_OP_CALL() { frame = top_frame(); goto __NEXT_FRAME; }
|
||||||
__NEXT_FRAME:
|
__NEXT_FRAME:
|
||||||
|
@ -1,42 +0,0 @@
|
|||||||
#include "pocketpy/line_profiler.h"
|
|
||||||
|
|
||||||
namespace pkpy{
|
|
||||||
|
|
||||||
struct LineProfiler{
|
|
||||||
PY_CLASS(LineProfiler, line_profiler, LineProfiler)
|
|
||||||
|
|
||||||
std::set<void*> _functions;
|
|
||||||
|
|
||||||
static void _register(VM* vm, PyObject* mod, PyObject* type){
|
|
||||||
vm->bind_default_constructor<LineProfiler>(type);
|
|
||||||
|
|
||||||
vm->bind(type, "add_function(self, func)", [](VM* vm, ArgsView args){
|
|
||||||
// ...
|
|
||||||
return vm->None;
|
|
||||||
});
|
|
||||||
|
|
||||||
vm->bind(type, "runcall(self, func, *args)", [](VM* vm, ArgsView view){
|
|
||||||
LineProfiler& self = PK_OBJ_GET(LineProfiler, view[0]);
|
|
||||||
// enable_by_count
|
|
||||||
PyObject* func = view[1];
|
|
||||||
const Tuple& args = CAST(Tuple&, view[2]);
|
|
||||||
for(PyObject* arg : args) vm->s_data.push(arg);
|
|
||||||
vm->s_data.push(func);
|
|
||||||
PyObject* ret = vm->vectorcall(args.size());
|
|
||||||
// disable_by_count
|
|
||||||
return ret;
|
|
||||||
});
|
|
||||||
|
|
||||||
vm->bind(type, "print_stats(self)", [](VM* vm, ArgsView args){
|
|
||||||
// ...
|
|
||||||
return vm->None;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
void add_module_line_profiler(VM *vm){
|
|
||||||
PyObject* mod = vm->new_module("line_profiler");
|
|
||||||
LineProfiler::register_class(vm, mod);
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace pkpy
|
|
@ -223,18 +223,16 @@ void add_module_traceback(VM* vm){
|
|||||||
void add_module_dis(VM* vm){
|
void add_module_dis(VM* vm){
|
||||||
PyObject* mod = vm->new_module("dis");
|
PyObject* mod = vm->new_module("dis");
|
||||||
|
|
||||||
static const auto get_code = [](VM* vm, PyObject* obj)->CodeObject_{
|
vm->bind_func<1>(mod, "dis", [](VM* vm, ArgsView args) {
|
||||||
|
CodeObject_ code;
|
||||||
|
PyObject* obj = args[0];
|
||||||
if(is_type(obj, vm->tp_str)){
|
if(is_type(obj, vm->tp_str)){
|
||||||
const Str& source = CAST(Str, obj);
|
const Str& source = CAST(Str, obj);
|
||||||
return vm->compile(source, "<dis>", EXEC_MODE);
|
code = vm->compile(source, "<dis>", EXEC_MODE);
|
||||||
}
|
}
|
||||||
PyObject* f = obj;
|
PyObject* f = obj;
|
||||||
if(is_type(f, vm->tp_bound_method)) f = CAST(BoundMethod, obj).func;
|
if(is_type(f, vm->tp_bound_method)) f = CAST(BoundMethod, obj).func;
|
||||||
return CAST(Function&, f).decl->code;
|
code = CAST(Function&, f).decl->code;
|
||||||
};
|
|
||||||
|
|
||||||
vm->bind_func<1>(mod, "dis", [](VM* vm, ArgsView args) {
|
|
||||||
CodeObject_ code = get_code(vm, args[0]);
|
|
||||||
vm->stdout_write(vm->disassemble(code));
|
vm->stdout_write(vm->disassemble(code));
|
||||||
return vm->None;
|
return vm->None;
|
||||||
});
|
});
|
||||||
@ -245,5 +243,40 @@ void add_module_gc(VM* vm){
|
|||||||
vm->bind_func<0>(mod, "collect", PK_LAMBDA(VAR(vm->heap.collect())));
|
vm->bind_func<0>(mod, "collect", PK_LAMBDA(VAR(vm->heap.collect())));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// line_profiler wrapper
|
||||||
|
struct LineProfilerW{
|
||||||
|
PY_CLASS(LineProfilerW, line_profiler, LineProfiler)
|
||||||
|
|
||||||
|
static void _register(VM* vm, PyObject* mod, PyObject* type){
|
||||||
|
vm->bind_default_constructor<LineProfilerW>(type);
|
||||||
|
|
||||||
|
vm->bind(type, "add_function(self, func)", [](VM* vm, ArgsView args){
|
||||||
|
// ...
|
||||||
|
return vm->None;
|
||||||
|
});
|
||||||
|
|
||||||
|
vm->bind(type, "runcall(self, func, *args)", [](VM* vm, ArgsView view){
|
||||||
|
LineProfilerW& self = PK_OBJ_GET(LineProfilerW, view[0]);
|
||||||
|
// self.enable_by_count(vm);
|
||||||
|
PyObject* func = view[1];
|
||||||
|
const Tuple& args = CAST(Tuple&, view[2]);
|
||||||
|
for(PyObject* arg : args) vm->s_data.push(arg);
|
||||||
|
vm->s_data.push(func);
|
||||||
|
PyObject* ret = vm->vectorcall(args.size());
|
||||||
|
// self.disable_by_count(vm);
|
||||||
|
return ret;
|
||||||
|
});
|
||||||
|
|
||||||
|
vm->bind(type, "print_stats(self)", [](VM* vm, ArgsView args){
|
||||||
|
// ...
|
||||||
|
return vm->None;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
void add_module_line_profiler(VM *vm){
|
||||||
|
PyObject* mod = vm->new_module("line_profiler");
|
||||||
|
LineProfilerW::register_class(vm, mod);
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace pkpy
|
} // namespace pkpy
|
7
src/profiler.cpp
Normal file
7
src/profiler.cpp
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
#include "pocketpy/profiler.h"
|
||||||
|
|
||||||
|
namespace pkpy{
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
} // namespace pkpy
|
Loading…
x
Reference in New Issue
Block a user