From 2cd22d40c5b889d9cf4c7208fe24328d503cf1f3 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sun, 7 Jan 2024 01:49:33 +0800 Subject: [PATCH] some optimize --- include/pocketpy/expr.h | 3 ++- src/compiler.cpp | 4 +-- src/expr.cpp | 59 +++++++++++++++++++++-------------------- 3 files changed, 34 insertions(+), 32 deletions(-) diff --git a/include/pocketpy/expr.h b/include/pocketpy/expr.h index 4c3480a3..d3c2ff8f 100644 --- a/include/pocketpy/expr.h +++ b/include/pocketpy/expr.h @@ -67,7 +67,8 @@ struct CodeEmitContext{ void patch_jump(int index); bool add_label(StrName name); int add_varname(StrName name); - int add_const(PyObject* v); + int add_const(PyObject*); + int add_const_string(std::string_view); int add_func_decl(FuncDecl_ decl); void emit_store_name(NameScope scope, StrName name, int line); }; diff --git a/src/compiler.cpp b/src/compiler.cpp index 459af202..3538b732 100644 --- a/src/compiler.cpp +++ b/src/compiler.cpp @@ -515,7 +515,7 @@ __SUBSCR_END: do { consume(TK("@id")); Str name = prev().str(); - ctx()->emit_(OP_IMPORT_PATH, ctx()->add_const(VAR(name)), prev().line); + ctx()->emit_(OP_IMPORT_PATH, ctx()->add_const_string(name.sv()), prev().line); if (match(TK("as"))) { consume(TK("@id")); name = prev().str(); @@ -567,7 +567,7 @@ __EAT_DOTS_END: } } - ctx()->emit_(OP_IMPORT_PATH, ctx()->add_const(VAR(ss.str())), prev().line); + ctx()->emit_(OP_IMPORT_PATH, ctx()->add_const_string(ss.str().sv()), prev().line); consume(TK("import")); if (match(TK("*"))) { diff --git a/src/expr.cpp b/src/expr.cpp index f5ba2a9b..9bd4174a 100644 --- a/src/expr.cpp +++ b/src/expr.cpp @@ -86,19 +86,22 @@ namespace pkpy{ return index; } + int CodeEmitContext::add_const_string(std::string_view key){ + auto it = _co_consts_string_dedup_map.find(key); + if(it != _co_consts_string_dedup_map.end()){ + return it->second; + }else{ + co->consts.push_back(VAR(key)); + int index = co->consts.size() - 1; + _co_consts_string_dedup_map[std::string(key)] = index; + return index; + } + } + int CodeEmitContext::add_const(PyObject* v){ if(is_non_tagged_type(v, vm->tp_str)){ - // string deduplication - std::string_view key = PK_OBJ_GET(Str, v).sv(); - auto it = _co_consts_string_dedup_map.find(key); - if(it != _co_consts_string_dedup_map.end()){ - return it->second; - }else{ - co->consts.push_back(v); - int index = co->consts.size() - 1; - _co_consts_string_dedup_map[std::string(key)] = index; - return index; - } + // warning: should use add_const_string() instead + return add_const_string(PK_OBJ_GET(Str, v).sv()); }else{ // non-string deduplication auto it = _co_consts_nonstring_dedup_map.find(v); @@ -225,8 +228,7 @@ namespace pkpy{ } void LongExpr::emit_(CodeEmitContext* ctx) { - VM* vm = ctx->vm; - ctx->emit_(OP_LOAD_CONST, ctx->add_const(VAR(s)), line); + ctx->emit_(OP_LOAD_CONST, ctx->add_const_string(s.sv()), line); ctx->emit_(OP_BUILD_LONG, BC_NOARG, line); } @@ -237,31 +239,31 @@ namespace pkpy{ } void BytesExpr::emit_(CodeEmitContext* ctx) { - VM* vm = ctx->vm; - ctx->emit_(OP_LOAD_CONST, ctx->add_const(VAR(s)), line); + ctx->emit_(OP_LOAD_CONST, ctx->add_const_string(s.sv()), line); ctx->emit_(OP_BUILD_BYTES, BC_NOARG, line); } void LiteralExpr::emit_(CodeEmitContext* ctx) { VM* vm = ctx->vm; - PyObject* obj = nullptr; if(std::holds_alternative(value)){ i64 _val = std::get(value); if(is_imm_int(_val)){ ctx->emit_(OP_LOAD_INTEGER, (uint16_t)_val, line); return; } - obj = VAR(_val); + ctx->emit_(OP_LOAD_CONST, ctx->add_const(VAR(_val)), line); + return; } if(std::holds_alternative(value)){ f64 _val = std::get(value); - obj = VAR(_val); + ctx->emit_(OP_LOAD_CONST, ctx->add_const(VAR(_val)), line); + return; } if(std::holds_alternative(value)){ - obj = VAR(std::get(value)); + std::string_view key = std::get(value).sv(); + ctx->emit_(OP_LOAD_CONST, ctx->add_const_string(key), line); + return; } - PK_ASSERT(obj != nullptr) - ctx->emit_(OP_LOAD_CONST, ctx->add_const(obj), line); } void NegatedExpr::emit_(CodeEmitContext* ctx){ @@ -412,7 +414,7 @@ namespace pkpy{ ctx->emit_(OP_LOAD_ATTR, attr.index, line); } }else{ - int index = ctx->add_const(py_var(ctx->vm, expr)); + int index = ctx->add_const_string(expr.sv()); ctx->emit_(OP_FSTRING_EVAL, index, line); } if(repr){ @@ -421,7 +423,6 @@ namespace pkpy{ } void FStringExpr::emit_(CodeEmitContext* ctx){ - VM* vm = ctx->vm; int i = 0; // left index int j = 0; // right index int count = 0; // how many string parts @@ -444,7 +445,7 @@ namespace pkpy{ for(char c: spec) if(!fmt_valid_char_set.count(c)){ ok = false; break; } if(ok){ _load_simple_expr(ctx, expr.substr(0, conon)); - ctx->emit_(OP_FORMAT_STRING, ctx->add_const(VAR(spec)), line); + ctx->emit_(OP_FORMAT_STRING, ctx->add_const_string(spec.sv()), line); }else{ // ':' is not a spec indicator _load_simple_expr(ctx, expr); @@ -461,7 +462,7 @@ namespace pkpy{ if(j+1 < src.size && src[j+1] == '{'){ // {{ -> { j++; - ctx->emit_(OP_LOAD_CONST, ctx->add_const(VAR("{")), line); + ctx->emit_(OP_LOAD_CONST, ctx->add_const_string("{"), line); count++; }else{ // { -> } @@ -473,7 +474,7 @@ namespace pkpy{ if(j+1 < src.size && src[j+1] == '}'){ // }} -> } j++; - ctx->emit_(OP_LOAD_CONST, ctx->add_const(VAR("}")), line); + ctx->emit_(OP_LOAD_CONST, ctx->add_const_string("}"), line); count++; }else{ // } -> error @@ -485,7 +486,7 @@ namespace pkpy{ i = j; while(j < src.size && src[j] != '{' && src[j] != '}') j++; Str literal = src.substr(i, j-i); - ctx->emit_(OP_LOAD_CONST, ctx->add_const(VAR(literal)), line); + ctx->emit_(OP_LOAD_CONST, ctx->add_const_string(literal.sv()), line); count++; continue; // skip j++ } @@ -496,7 +497,7 @@ namespace pkpy{ if(flag){ // literal Str literal = src.substr(i, src.size-i); - ctx->emit_(OP_LOAD_CONST, ctx->add_const(VAR(literal)), line); + ctx->emit_(OP_LOAD_CONST, ctx->add_const_string(literal.sv()), line); count++; } @@ -576,7 +577,7 @@ namespace pkpy{ item.second->emit_(ctx); }else{ // k=v - int index = ctx->add_const(py_var(ctx->vm, item.first)); + int index = ctx->add_const_string(item.first.sv()); ctx->emit_(OP_LOAD_CONST, index, line); item.second->emit_(ctx); ctx->emit_(OP_BUILD_TUPLE, 2, line);