You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by GitBox <gi...@apache.org> on 2022/10/18 03:24:09 UTC

[GitHub] [apisix] spacewander commented on a diff in pull request #8102: perf: optimizing performance of router match with lrucache

spacewander commented on code in PR #8102:
URL: https://github.com/apache/apisix/pull/8102#discussion_r997679932


##########
apisix/core/ai_plane.lua:
##########
@@ -0,0 +1,79 @@
+--
+-- 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.
+--
+local require         = require
+local core            = require("apisix.core")
+local ipairs          = ipairs
+
+local route_lrucache = core.lrucache.new({
+    -- TODO: we need to set the cache size by count of routes
+    -- if we have done this feature, we need to release the origin lrucache
+    count = 512
+})
+
+local _M = {}
+
+local orig_router_match
+local router
+
+local function match_route(ctx)
+    orig_router_match(ctx)
+    return ctx.matched_route or false
+end
+
+
+local function ai_match(ctx)
+    -- TODO: we need to generate cache key dynamically
+    local key = ctx.var.uri .. "-" .. ctx.var.method .. "-" ..
+                ctx.var.host .. "-" .. ctx.var.remote_addr

Review Comment:
   Is it suitable to cache remote_addr? The range can be very hugh.



##########
apisix/core/ai_plane.lua:
##########
@@ -0,0 +1,79 @@
+--
+-- 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.
+--
+local require         = require
+local core            = require("apisix.core")
+local ipairs          = ipairs
+
+local route_lrucache = core.lrucache.new({
+    -- TODO: we need to set the cache size by count of routes
+    -- if we have done this feature, we need to release the origin lrucache
+    count = 512
+})
+
+local _M = {}
+
+local orig_router_match
+local router
+
+local function match_route(ctx)
+    orig_router_match(ctx)
+    return ctx.matched_route or false
+end
+
+
+local function ai_match(ctx)
+    -- TODO: we need to generate cache key dynamically
+    local key = ctx.var.uri .. "-" .. ctx.var.method .. "-" ..
+                ctx.var.host .. "-" .. ctx.var.remote_addr
+    local ver = router.user_routes.conf_version
+    local route_cache = route_lrucache(key, ver,
+                                       match_route, ctx)
+    -- if the version has not changed, use the cached route
+    if route_cache then
+        ctx.matched_route = route_cache
+    end
+end
+
+
+function  _M.routes_analyze(routes)
+    -- TODO: we need to add a option in config.yaml to enable this feature(default is true)
+    local route_flags = core.table.new(0, 2)
+    for _, route in ipairs(routes) do
+        if route.vars then
+            route_flags.vars = true

Review Comment:
   We could simplify it with an `enable_ai_plane` flag?
   ```suggestion
               enable_ai_plane = false
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org