From 763926e3a475d4c3c7f1eeeb63948a649b9a8497 Mon Sep 17 00:00:00 2001 From: "Wu, Zhenyu" Date: Thu, 21 Nov 2024 23:14:15 +0800 Subject: [PATCH] :zap: Use init() once to speed up --- jieba.c | 10 ++++++++-- jieba.cpp | 10 ++++++++-- jieba.h | 3 ++- lua/jieba/nvim/init.lua | 2 ++ lua/jieba/nvim/utils.lua | 3 ++- 5 files changed, 22 insertions(+), 6 deletions(-) diff --git a/jieba.c b/jieba.c index 232f74a..5f92837 100644 --- a/jieba.c +++ b/jieba.c @@ -2,7 +2,7 @@ #include "jieba.h" -static int _cut(lua_State *L) { +static int _init(lua_State *L) { struct jieba_path jieba_path; lua_getfield(L, 3, "dict_path"); jieba_path.dict_path = lua_tostring(L, -1); @@ -14,8 +14,13 @@ static int _cut(lua_State *L) { jieba_path.idf_path = lua_tostring(L, -1); lua_getfield(L, 3, "stop_word_path"); jieba_path.stop_word_path = lua_tostring(L, -1); + init(jieba_path); + return 0; +} + +static int _cut(lua_State *L) { lua_newtable(L); - char **results = cut(lua_tostring(L, 1), lua_toboolean(L, 2), jieba_path); + char **results = cut(lua_tostring(L, 1), lua_toboolean(L, 2)); int i = 1; while (*results) { lua_pushstring(L, *results++); @@ -25,6 +30,7 @@ static int _cut(lua_State *L) { } static const luaL_Reg functions[] = { + {"init", _init}, {"cut", _cut}, {NULL, NULL}, }; diff --git a/jieba.cpp b/jieba.cpp index 947ac82..bb13392 100644 --- a/jieba.cpp +++ b/jieba.cpp @@ -5,18 +5,24 @@ using namespace std; -extern "C" char **cut(const char *str, bool hmm, struct jieba_path jieba_path) { +static cppjieba::Jieba jieba; + +extern "C" void init(struct jieba_path jieba_path) { cppjieba::Jieba jieba{jieba_path.dict_path, jieba_path.model_path, jieba_path.user_dict_path, jieba_path.idf_path, jieba_path.stop_word_path}; +} + +extern "C" char **cut(const char *str, bool hmm) { vector words; vector jiebawords; string s = str; jieba.Cut(s, words, hmm); - char **results = new char *[DEFAULT_BUFFER_SIZE] {}; + char **results = (char **)malloc(sizeof(char *) * DEFAULT_BUFFER_SIZE); char **p = results; for (auto word : words) *p++ = strdup(word.c_str()); + *p = NULL; return results; } diff --git a/jieba.h b/jieba.h index 8b8d70c..a44185e 100644 --- a/jieba.h +++ b/jieba.h @@ -11,7 +11,8 @@ struct jieba_path { const char *idf_path; const char *stop_word_path; }; -char **cut(const char *str, bool hmm, struct jieba_path); +void init(struct jieba_path); +char **cut(const char *str, bool hmm); __END_DECLS #endif /* jieba.h */ diff --git a/lua/jieba/nvim/init.lua b/lua/jieba/nvim/init.lua index 13c0594..2afec37 100644 --- a/lua/jieba/nvim/init.lua +++ b/lua/jieba/nvim/init.lua @@ -24,6 +24,8 @@ local M = {} local jieba = require("jieba.nvim.utils") local ut = require("jieba.utils") +require("jieba").init(jieba.jieba_path) + local str_match = string.match local sub = ut.sub local len = vim.api.nvim_strwidth diff --git a/lua/jieba/nvim/utils.lua b/lua/jieba/nvim/utils.lua index 37a4eaf..f3619b6 100644 --- a/lua/jieba/nvim/utils.lua +++ b/lua/jieba/nvim/utils.lua @@ -21,9 +21,10 @@ end ---@param hmm boolean ---@return string[] local function cut(str, hmm) - return require 'jieba'.cut(str, hmm, jieba_path) + return require 'jieba'.cut(str, hmm) end return { + jieba_path = jieba_path, cut = cut }