diff --git a/include/pocketpy/codeobject.h b/include/pocketpy/codeobject.h index 9ad1df12..e02920e7 100644 --- a/include/pocketpy/codeobject.h +++ b/include/pocketpy/codeobject.h @@ -138,10 +138,13 @@ struct FuncDecl { Str docstring; // docstring of this function bool is_simple; - int keyword_to_index(StrName key) const{ - for(const KwArg& item: kwargs) if(item.key == key) return item.index; - return -1; + NameDictInt kw_to_index; + + void add_kwarg(int index, StrName key, PyObject* value){ + kw_to_index.set(key, index); + kwargs.push_back({index, key, value}); } + void _gc_mark() const; }; diff --git a/src/compiler.cpp b/src/compiler.cpp index 36d94219..7286b0ab 100644 --- a/src/compiler.cpp +++ b/src/compiler.cpp @@ -1037,7 +1037,7 @@ __EAT_DOTS_END: if(value == nullptr){ SyntaxError(Str("default argument must be a literal")); } - decl->kwargs.push_back(FuncDecl::KwArg{index, name, value}); + decl->add_kwarg(index, name, value); } break; case 3: decl->starred_kwarg = index; diff --git a/src/vm.cpp b/src/vm.cpp index 595c3a5c..0b586eff 100644 --- a/src/vm.cpp +++ b/src/vm.cpp @@ -844,9 +844,9 @@ void VM::_prepare_py_call(PyObject** buffer, ArgsView args, ArgsView kwargs, con for(int j=0; jkeyword_to_index(key); + int index = decl->kw_to_index.try_get_likely_found(key); // if key is an explicit key, set as local variable - if(index != -1){ + if(index >= 0){ buffer[index] = kwargs[j+1]; }else{ // otherwise, set as **kwargs if possible