Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(router/atc): do not create new table in split_routes_and_services_by_path() #12875

Merged
merged 4 commits into from
Apr 24, 2024

Conversation

chronolaw
Copy link
Contributor

@chronolaw chronolaw commented Apr 18, 2024

Summary

The original split_routes_and_services_by_path() will create a new table to contains all routes even if no splitting.
To erase this overhead, now split_routes_and_services_by_path() will insert the cloned routes directly into the passed in table.

KAG-4138

Checklist

  • The Pull Request has tests
  • A changelog file has been created under changelog/unreleased/kong or skip-changelog label added on PR if changelog is unnecessary. README.md
  • There is a user-facing docs PR against https://github.com/Kong/docs.konghq.com - PUT DOCS PR HERE

Issue reference

Fix #[issue number]

@github-actions github-actions bot added core/router cherry-pick kong-ee schedule this PR for cherry-picking to kong/kong-ee labels Apr 18, 2024
@chronolaw chronolaw requested a review from hanshuebner April 18, 2024 06:07
@chronolaw chronolaw changed the title refactor(router/atc): optimize split_routes_and_services_by_path() refactor(router/atc): do not create new table in split_routes_and_services_by_path() Apr 18, 2024
@chronolaw chronolaw marked this pull request as ready for review April 18, 2024 06:22
@chronolaw
Copy link
Contributor Author

@hanshuebner , could you take a look? since you are the author of this feature.

@chronolaw chronolaw requested a review from bungle April 18, 2024 08:04
@chronolaw chronolaw force-pushed the refactor/clean_split_routes_and_services branch from 95eac62 to 0f5c08e Compare April 18, 2024 08:12
Copy link
Contributor

@hanshuebner hanshuebner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the benefit of this change. I find that the split_routes_and_services_by_path function was already pretty complicated, and the change further complicates it. I would endorse this change only if the performance benefit is measureable and significant.

@chronolaw
Copy link
Contributor Author

chronolaw commented Apr 19, 2024

I did a simple test:

  it("#only test", function()
    local transform = require("kong.router.transform")
    local split_routes_and_services_by_path = transform.split_routes_and_services_by_path

    local routes = {}

    for i = 1, 5000 do
      routes[i] = {
        route = {
          id = "e8fb37f1-102d-461e-9c51-6608a6bb8101",
          hosts = { "a.com" },
        }
      }
    end

    local COUNT = 1000

    ngx.update_time()
    local t = ngx.now()

    for i = 1, COUNT do
      split_routes_and_services_by_path(routes)
    end

    ngx.update_time()
    print(ngx.now() - t)
  end)

The master's output:

(kong-dev) [KONG kong]$bin/busted t.lua -t only
0.03000020980835
●
1 success / 0 failures / 0 errors / 0 pending : 0.063314 seconds
(kong-dev) [KONG kong]$bin/busted t.lua -t only
0.028000116348267
●
1 success / 0 failures / 0 errors / 0 pending : 0.059201 seconds

This PR's output:

(kong-dev) [KONG kong]$bin/busted t.lua -t only
0.017999887466431
●
1 success / 0 failures / 0 errors / 0 pending : 0.049651 seconds
(kong-dev) [KONG kong]$bin/busted t.lua -t only
0.016999959945679
●
1 success / 0 failures / 0 errors / 0 pending : 0.053319 seconds

Obviously, This PR is faster.
Of course, it is a corner case, there is no paths in the route, so This PR will not insert any data into the table.

@hanshuebner , Do you have more opinions?

@chronolaw chronolaw requested a review from chobits April 19, 2024 02:26
@hanshuebner
Copy link
Contributor

I think the performance optimization is too visible in the code, making it harder to understand and follow. It was already pretty difficult to understand. Given the tiny benefit, I would not go ahead and merge it.

Copy link
Member

@bungle bungle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chronolaw, I guess you will get the idea?

kong/router/transform.lua Outdated Show resolved Hide resolved
kong/router/transform.lua Show resolved Hide resolved
kong/router/transform.lua Show resolved Hide resolved
kong/router/transform.lua Outdated Show resolved Hide resolved
@chronolaw chronolaw force-pushed the refactor/clean_split_routes_and_services branch from df9bd56 to 6823dcb Compare April 20, 2024 00:28
@chronolaw chronolaw force-pushed the refactor/clean_split_routes_and_services branch 2 times, most recently from e9281f6 to d49ea3b Compare April 20, 2024 00:58
@bungle
Copy link
Member

bungle commented Apr 22, 2024

@chronolaw here are some of my proposals, feel free to merge them here or ignore:
#12901

@chronolaw
Copy link
Contributor Author

@chronolaw here are some of my proposals, feel free to merge them here or ignore: #12901

Thank you Aapo, it is a good improvement.
But I think it also increase the complexity of this PR, could you turn it into a new PR after this PR is merged?

@bungle
Copy link
Member

bungle commented Apr 23, 2024

@chronolaw here are some of my proposals, feel free to merge them here or ignore: #12901

Thank you Aapo, it is a good improvement. But I think it also increase the complexity of this PR, could you turn it into a new PR after this PR is merged?

Ok.

@hanshuebner hanshuebner merged commit 9db89af into master Apr 24, 2024
25 checks passed
@hanshuebner hanshuebner deleted the refactor/clean_split_routes_and_services branch April 24, 2024 09:51
@team-gateway-bot
Copy link
Collaborator

Successfully created cherry-pick PR for master:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cherry-pick kong-ee schedule this PR for cherry-picking to kong/kong-ee core/router size/M skip-changelog
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants