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

Improve router performance with dedicated child types #1689

Merged

Conversation

pafuent
Copy link
Contributor

@pafuent pafuent commented Nov 22, 2020

Before this commit, all the node types were added to the same list of children nodes. Taking in consideration that only one Param and Any type of node could exist per node, two new node struct field were added to hold the references to those kind of nodes.
This avoid the need to iterate through all the Static type nodes just to find one Param or Any type node. Those iterations could be performed multiple times in the same iteration of Router#Find.
Removing the route comments of the Router benchmark tests.
Updating the Router benchmarks tests to find the routes defined to each particular benchmark. Before, all the benchmarks tried to find only the GitHub API.
Adding new router benchmarks to measure when the Router try to find routes that are not registered.

To be fair in the below benchmarks, I did the same modifications to the Router benchmarks on master. Also I performed 10 iterations per each benchmark, and compared the results with benchstat

benchstat bench_master.txt bench_new.txt

name                         old time/op    new time/op    delta
RouterStaticRoutes-2           20.0µs ± 2%    20.3µs ± 5%     ~     (p=0.065 n=10+9)
RouterStaticRoutesMisses-2     1.02µs ± 2%    0.66µs ± 4%  -35.43%  (p=0.000 n=8+9)
RouterGitHubAPI-2              38.9µs ± 3%    37.0µs ± 4%   -4.77%  (p=0.000 n=10+10)
RouterGitHubAPIMisses-2        1.25µs ± 3%    0.77µs ± 6%  -38.39%  (p=0.000 n=10+10)
RouterParseAPI-2               2.08µs ±10%    1.98µs ± 3%   -4.71%  (p=0.029 n=10+10)
RouterParseAPIMisses-2          442ns ± 4%     452ns ± 3%     ~     (p=0.066 n=9+9)
RouterGooglePlusAPI-2          1.35µs ± 2%    1.29µs ± 3%   -4.27%  (p=0.000 n=10+10)
RouterGooglePlusAPIMisses-2     989ns ± 4%     662ns ± 8%  -33.06%  (p=0.000 n=10+10)
RouterParamsAndAnyAPI-2        3.34µs ± 1%    2.96µs ± 3%  -11.41%  (p=0.000 n=8+10)

name                         old alloc/op   new alloc/op   delta
RouterStaticRoutes-2            0.00B          0.00B          ~     (all equal)
RouterStaticRoutesMisses-2      0.00B          0.00B          ~     (all equal)
RouterGitHubAPI-2               0.00B          0.00B          ~     (all equal)
RouterGitHubAPIMisses-2         0.00B          0.00B          ~     (all equal)
RouterParseAPI-2                0.00B          0.00B          ~     (all equal)
RouterParseAPIMisses-2          0.00B          0.00B          ~     (all equal)
RouterGooglePlusAPI-2           0.00B          0.00B          ~     (all equal)
RouterGooglePlusAPIMisses-2     0.00B          0.00B          ~     (all equal)
RouterParamsAndAnyAPI-2         0.00B          0.00B          ~     (all equal)

name                         old allocs/op  new allocs/op  delta
RouterStaticRoutes-2             0.00           0.00          ~     (all equal)
RouterStaticRoutesMisses-2       0.00           0.00          ~     (all equal)
RouterGitHubAPI-2                0.00           0.00          ~     (all equal)
RouterGitHubAPIMisses-2          0.00           0.00          ~     (all equal)
RouterParseAPI-2                 0.00           0.00          ~     (all equal)
RouterParseAPIMisses-2           0.00           0.00          ~     (all equal)
RouterGooglePlusAPI-2            0.00           0.00          ~     (all equal)
RouterGooglePlusAPIMisses-2      0.00           0.00          ~     (all equal)
RouterParamsAndAnyAPI-2          0.00           0.00          ~     (all equal)

@codecov
Copy link

codecov bot commented Nov 22, 2020

Codecov Report

Merging #1689 (045bec5) into master (4422e3b) will increase coverage by 0.11%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1689      +/-   ##
==========================================
+ Coverage   85.19%   85.30%   +0.11%     
==========================================
  Files          29       29              
  Lines        1986     2001      +15     
==========================================
+ Hits         1692     1707      +15     
  Misses        186      186              
  Partials      108      108              
Impacted Files Coverage Δ
router.go 97.05% <100.00%> (+0.17%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4422e3b...045bec5. Read the comment docs.

Before this commit, all the node types were added to the same list of
children nodes. Taking in consideration that only one Param and Any type
of node could exist per node, two new node struct field were added to hold
the references to those kind of nodes.
This avoid the need to iterate through all the Static type nodes just to
find one Param or Any type node. Those iterations could be performed
multiple times in the same iteration of Router#Find.
Removing the route comments of the Router benchmark tests.
Updating the Router benchmarks tests to find the routes defined to each
particular benchmark. Before, all the benchmarks tried to find only the
GitHub API.
Adding new router benchmarks to measure when the Router try to find
routes that are not registered.
@pafuent pafuent force-pushed the routing_misses_performance_improvements branch from e280e4e to 3a6100b Compare November 22, 2020 03:09
@pafuent pafuent self-assigned this Nov 26, 2020
@lammel
Copy link
Contributor

lammel commented Dec 6, 2020

@pafuent I do not see too much risk in changed behaviour as our router tests are pretty helpful here.
So I'm in favour of merging. Patch looks good based on the code, haven't tried the branch yet.

I'm missing our github action for benchmark comparison though.

@lammel lammel changed the title Improving routing benchmark suite and performance Improve router performance with dedicated child types Dec 7, 2020
@pafuent
Copy link
Contributor Author

pafuent commented Dec 10, 2020

Sadly the GitHub action won't be fair in this case, because I changed the benchmarks on this PR. In this case you will need to trust on my benchmarks 😉

@lammel
Copy link
Contributor

lammel commented Dec 15, 2020

Please rebase to master.

@pafuent
Copy link
Contributor Author

pafuent commented Dec 16, 2020

@lammel rebased

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants