This is an automated email from the ASF dual-hosted git repository.

hulk pushed a commit to branch unstable
in repository https://gitbox.apache.org/repos/asf/incubator-kvrocks.git


The following commit(s) were added to refs/heads/unstable by this push:
     new bacb45a  Allow using LuaJIT to improve performance of the Lua script   
(#697)
bacb45a is described below

commit bacb45a5e056c243d87d6d5b92cc1cd55948a82c
Author: xiaobiaozhao <[email protected]>
AuthorDate: Wed Jul 20 19:43:12 2022 +0800

    Allow using LuaJIT to improve performance of the Lua script   (#697)
    
    LuaJIT can improve the performance of Lua script in most scenarios,
    so we enable it by default to achieve better performance and users
    can disabled it by passing `-DUSE_LUAJIT=OFF` when building from
    source.
    
    Co-authored-by: hulk <[email protected]>
    Co-authored-by: Twice <[email protected]>
    Co-authored-by: tison <[email protected]>
---
 .github/workflows/kvrocks.yaml     |  6 ++++-
 CMakeLists.txt                     | 16 +++++++++--
 cmake/lua.cmake                    |  8 +++---
 cmake/luajit.cmake                 | 55 ++++++++++++++++++++++++++++++++++++++
 src/scripting.h                    |  6 +----
 src/server.h                       |  3 +--
 tests/tcl/tests/unit/scripting.tcl |  5 ++--
 7 files changed, 83 insertions(+), 16 deletions(-)

diff --git a/.github/workflows/kvrocks.yaml b/.github/workflows/kvrocks.yaml
index 35fe1eb..1f30f4e 100644
--- a/.github/workflows/kvrocks.yaml
+++ b/.github/workflows/kvrocks.yaml
@@ -110,6 +110,10 @@ jobs:
             with_ninja: --ninja
             without_jemalloc: -DDISABLE_JEMALLOC=ON
             compiler: gcc
+          - name: Ubuntu GCC without luajit
+            os: ubuntu-18.04
+            without_luajit: -DUSE_LUAJIT=OFF
+            compiler: gcc
 
     runs-on: ${{ matrix.os }}
     steps:
@@ -151,7 +155,7 @@ jobs:
           python-version: 3.x
 
       - name: Build Kvrocks
-        run: ./x.py build -j$NPROC --unittest --compiler ${{ matrix.compiler 
}} ${{ matrix.with_ninja }} ${{ matrix.with_sanitizer }} ${{ 
matrix.without_jemalloc }}
+        run: ./x.py build -j$NPROC --unittest --compiler ${{ matrix.compiler 
}} ${{ matrix.with_ninja }} ${{ matrix.with_sanitizer }} ${{ 
matrix.without_jemalloc }} ${{ matrix.without_luajit }}
 
       - name: Run Unit Test
         run: ./build/unittest
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 4e99702..9b7a65b 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -25,8 +25,9 @@ option(ENABLE_ASAN "enable address santinizer" OFF)
 option(ENABLE_TSAN "enable thread santinizer" OFF)
 option(ASAN_WITH_LSAN "enable leak santinizer while address santinizer is 
enabled" ON)
 option(ENABLE_STATIC_LIBSTDCXX "link kvrocks with static library of libstd++ 
instead of shared library" ON)
+option(USE_LUAJIT "use luaJIT instead of lua" ON)
 
-set(DEPS_FETCH_PROXY "" CACHE STRING 
+set(DEPS_FETCH_PROXY "" CACHE STRING
     "a template URL to proxy the traffic for fetching dependencies, e.g. with 
DEPS_FETCH_PROXY = https://some-proxy/,
      https://example/some-dep.zip -> 
https://some-proxy/https://example/some-dep.zip";)
 
@@ -59,7 +60,12 @@ include(cmake/snappy.cmake)
 include(cmake/lz4.cmake)
 include(cmake/rocksdb.cmake)
 include(cmake/libevent.cmake)
+
+if (USE_LUAJIT)
+include(cmake/luajit.cmake)
+else()
 include(cmake/lua.cmake)
+endif()
 
 find_package(Threads REQUIRED)
 
@@ -67,8 +73,14 @@ list(APPEND EXTERNAL_LIBS PRIVATE glog)
 list(APPEND EXTERNAL_LIBS PRIVATE snappy)
 list(APPEND EXTERNAL_LIBS PRIVATE rocksdb_with_headers)
 list(APPEND EXTERNAL_LIBS PRIVATE event_with_headers)
-list(APPEND EXTERNAL_LIBS PRIVATE lua)
 list(APPEND EXTERNAL_LIBS PRIVATE lz4)
+if (USE_LUAJIT)
+list(APPEND EXTERNAL_LIBS PRIVATE luajit)
+else()
+list(APPEND EXTERNAL_LIBS PRIVATE lua)
+endif()
+
+
 list(APPEND EXTERNAL_LIBS PRIVATE Threads::Threads)
 
 # Add git sha to version.h
diff --git a/cmake/lua.cmake b/cmake/lua.cmake
index 031a0d4..8e55d4e 100644
--- a/cmake/lua.cmake
+++ b/cmake/lua.cmake
@@ -20,8 +20,8 @@ include_guard()
 include(cmake/utils.cmake)
 
 FetchContent_DeclareGitHubWithMirror(lua
-  KvrocksLabs/lua c8e4bbfa25f7202f3b778ccb88e54ab84a1861fb
-  MD5=79950ff054ae76e5c9f9c57b38484229
+  KvrocksLabs/lua f458c3d797db31155fa0c156d5301716df48cb8c
+  MD5=c7c4deb9f750d8f2bef0044a701df85c
 )
 
 FetchContent_GetProperties(lua)
@@ -38,8 +38,8 @@ if(NOT lua_POPULATED)
     WORKING_DIRECTORY ${lua_SOURCE_DIR}/src
     BYPRODUCTS ${lua_SOURCE_DIR}/src/liblua.a
   )
-  
-  file(GLOB LUA_PUBLIC_HEADERS "${lua_SOURCE_DIR}/src/*.h")
+
+  file(GLOB LUA_PUBLIC_HEADERS "${lua_SOURCE_DIR}/src/*.h" 
"${lua_SOURCE_DIR}/src/*.hpp")
   file(COPY ${LUA_PUBLIC_HEADERS} DESTINATION ${lua_BINARY_DIR}/include)
 endif()
 
diff --git a/cmake/luajit.cmake b/cmake/luajit.cmake
new file mode 100644
index 0000000..fd60105
--- /dev/null
+++ b/cmake/luajit.cmake
@@ -0,0 +1,55 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+include_guard()
+
+include(cmake/utils.cmake)
+
+FetchContent_DeclareGitHubWithMirror(luajit
+  KvrocksLabs/LuaJIT 803487f8b01c672495a2fcd29dcbed09e4fd6319
+  MD5=cd08841342cd933fb7e3d6d4253fbeec
+)
+
+FetchContent_GetProperties(luajit)
+if(NOT lua_POPULATED)
+  FetchContent_Populate(luajit)
+
+  set(LUA_CFLAGS "-DLUA_ANSI -DENABLE_CJSON_GLOBAL -DREDIS_STATIC= 
-DLUA_USE_MKSTEMP")
+  if(CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
+    set(LUA_CFLAGS "${LUA_CFLAGS} -isysroot ${CMAKE_OSX_SYSROOT}")
+  endif()
+
+  set(MACOSX_TARGET "")
+  if (CMAKE_HOST_APPLE)
+    set(MACOSX_TARGET "MACOSX_DEPLOYMENT_TARGET=11.0")
+  endif()
+
+  add_custom_target(make_luajit COMMAND make libluajit.a
+    "CFLAGS=${LUA_CFLAGS}"
+    ${MACOSX_TARGET}
+    WORKING_DIRECTORY ${luajit_SOURCE_DIR}/src
+    BYPRODUCTS ${luajit_SOURCE_DIR}/src/libluajit.a
+  )
+
+  file(GLOB LUA_PUBLIC_HEADERS "${luajit_SOURCE_DIR}/src/*.hpp" 
"${luajit_SOURCE_DIR}/src/*.h")
+  file(COPY ${LUA_PUBLIC_HEADERS} DESTINATION ${luajit_BINARY_DIR}/include)
+endif()
+
+add_library(luajit INTERFACE)
+target_include_directories(luajit INTERFACE ${luajit_BINARY_DIR}/include)
+target_link_libraries(luajit INTERFACE ${luajit_SOURCE_DIR}/src/libluajit.a dl)
+add_dependencies(luajit make_luajit)
diff --git a/src/scripting.h b/src/scripting.h
index d021184..eee1f02 100644
--- a/src/scripting.h
+++ b/src/scripting.h
@@ -23,14 +23,10 @@
 #include <string>
 #include <vector>
 
-#include <lua.h>
-#include <lauxlib.h>
-#include <lualib.h>
-
+#include "lua.hpp"
 #include "status.h"
 #include "redis_connection.h"
 
-
 namespace Lua {
 
 lua_State* CreateState();
diff --git a/src/server.h b/src/server.h
index 911bb39..3452c58 100644
--- a/src/server.h
+++ b/src/server.h
@@ -30,8 +30,7 @@
 #include <memory>
 #include <unordered_map>
 
-#include <lua.h>
-
+#include "lua.hpp"
 #include "stats.h"
 #include "storage.h"
 #include "task_runner.h"
diff --git a/tests/tcl/tests/unit/scripting.tcl 
b/tests/tcl/tests/unit/scripting.tcl
index d3fc47b..4138faf 100644
--- a/tests/tcl/tests/unit/scripting.tcl
+++ b/tests/tcl/tests/unit/scripting.tcl
@@ -263,9 +263,10 @@ start_server {tags {"scripting"}} {
                 assert(re.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x)
                 -- so, the final x.x is at the depth limit and was assigned nil
                 assert(re.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x == nil)
-                return {h, re.x.x.x.x.x.x.x.x.y == re.y, re.y == 5}
+                assert(h == 
"82a17881a17882a17881a17882a17881a17882a17881a17882a17881a17882a17881a17882a17881a17882a17881a178c0a17905a17905a17905a17905a17905a17905a17905a17905"
 or h == 
"82a17905a17881a17882a17905a17881a17882a17905a17881a17882a17905a17881a17882a17905a17881a17882a17905a17881a17882a17905a17881a17882a17905a17881a178c0")
+                return {re.x.x.x.x.x.x.x.x.y == re.y, re.y == 5}
         } 0
-    } 
{82a17905a17881a17882a17905a17881a17882a17905a17881a17882a17905a17881a17882a17905a17881a17882a17905a17881a17882a17905a17881a17882a17905a17881a178c0
 1 1}
+    } {1 1}
     test {EVAL - Numerical sanity check from bitop} {
         r eval {assert(0x7fffffff == 2147483647, "broken hex literals");
                 assert(0xffffffff == -1 or 0xffffffff == 2^32-1,

Reply via email to