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

perf(logql): Micro-optimizations for IP filter #14072

Merged
merged 5 commits into from
Sep 10, 2024

Conversation

KasonBraley
Copy link
Contributor

@KasonBraley KasonBraley commented Sep 6, 2024

What this PR does / why we need it:

Small micro optimizations to save some allocations for the IP filter in logql.

goos: darwin
goarch: arm64
pkg: github.com/grafana/loki/v3/pkg/logql/log
cpu: Apple M2 Pro
                                       │   old.txt    │               new.txt               │
                                       │    sec/op    │    sec/op     vs base               │
_IPFilter/127.0.0.1-10                   5.098µ ±  1%   3.168µ ±  1%  -37.86% (p=0.002 n=6)
_IPFilter/192.168.0.1-192.189.10.12-10   5.128µ ±  1%   3.288µ ± 24%  -35.89% (p=0.002 n=6)
_IPFilter/192.168.4.5/16-10              4.844µ ± 11%   3.063µ ±  2%  -36.76% (p=0.002 n=6)
geomean                                  5.022µ         3.172µ        -36.84%

                                       │   old.txt   │              new.txt              │
                                       │    B/op     │    B/op     vs base               │
_IPFilter/127.0.0.1-10                   425.00 ± 0%   32.00 ± 0%  -92.47% (p=0.002 n=6)
_IPFilter/192.168.0.1-192.189.10.12-10   424.00 ± 0%   32.00 ± 0%  -92.45% (p=0.002 n=6)
_IPFilter/192.168.4.5/16-10              425.00 ± 0%   32.00 ± 0%  -92.47% (p=0.002 n=6)
geomean                                   424.7        32.00       -92.46%

                                       │   old.txt   │              new.txt              │
                                       │  allocs/op  │ allocs/op   vs base               │
_IPFilter/127.0.0.1-10                   10.000 ± 0%   3.000 ± 0%  -70.00% (p=0.002 n=6)
_IPFilter/192.168.0.1-192.189.10.12-10   10.000 ± 0%   3.000 ± 0%  -70.00% (p=0.002 n=6)
_IPFilter/192.168.4.5/16-10              10.000 ± 0%   3.000 ± 0%  -70.00% (p=0.002 n=6)
geomean                                   10.00        3.000       -70.00%

Which issue(s) this PR fixes:
Fixes #

Special notes for your reviewer:

Checklist

  • Reviewed the CONTRIBUTING.md guide (required)
  • Documentation added
  • Tests updated
  • Title matches the required conventional commits format, see here
    • Note that Promtail is considered to be feature complete, and future development for logs collection will be in Grafana Alloy. As such, feat PRs are unlikely to be accepted unless a case can be made for the feature actually being a bug fix to existing behavior.
  • Changes that require user attention or interaction to upgrade are documented in docs/sources/setup/upgrade/_index.md
  • For Helm chart changes bump the Helm chart version in production/helm/loki/Chart.yaml and update production/helm/loki/CHANGELOG.md and production/helm/loki/README.md. Example PR
  • If the change is deprecating or removing a configuration option, update the deprecated-config.yaml and deleted-config.yaml files respectively in the tools/deprecated-config-checker directory. Example PR

Preallocate map to save 2 allocations.

```
goos: darwin
goarch: arm64
pkg: github.com/grafana/loki/v3/pkg/logql/log
cpu: Apple M2 Pro
                                       │   old.txt   │              new.txt              │
                                       │   sec/op    │   sec/op     vs base              │
_IPFilter/127.0.0.1-10                   5.048µ ± 0%   4.591µ ± 1%  -9.06% (p=0.002 n=6)
_IPFilter/192.168.0.1-192.189.10.12-10   5.082µ ± 0%   4.634µ ± 0%  -8.83% (p=0.002 n=6)
_IPFilter/192.168.4.5/16-10              4.842µ ± 0%   4.390µ ± 0%  -9.35% (p=0.002 n=6)
geomean                                  4.990µ        4.537µ       -9.08%

                                       │  old.txt   │              new.txt              │
                                       │    B/op    │    B/op     vs base               │
_IPFilter/127.0.0.1-10                   424.0 ± 0%   341.0 ± 0%  -19.58% (p=0.002 n=6)
_IPFilter/192.168.0.1-192.189.10.12-10   424.5 ± 0%   341.0 ± 0%  -19.67% (p=0.002 n=6)
_IPFilter/192.168.4.5/16-10              425.0 ± 0%   341.0 ± 0%  -19.76% (p=0.002 n=6)
geomean                                  424.5        341.0       -19.67%

                                       │   old.txt   │              new.txt              │
                                       │  allocs/op  │ allocs/op   vs base               │
_IPFilter/127.0.0.1-10                   10.000 ± 0%   8.000 ± 0%  -20.00% (p=0.002 n=6)
_IPFilter/192.168.0.1-192.189.10.12-10   10.000 ± 0%   8.000 ± 0%  -20.00% (p=0.002 n=6)
_IPFilter/192.168.4.5/16-10              10.000 ± 0%   8.000 ± 0%  -20.00% (p=0.002 n=6)
geomean                                   10.00        8.000       -20.00%
```
Micro-optimization to use a struct instead of bool to save some bytes.

```
goos: darwin
goarch: arm64
pkg: github.com/grafana/loki/v3/pkg/logql/log
cpu: Apple M2 Pro
                                       │   old.txt   │              new.txt              │
                                       │   sec/op    │   sec/op     vs base              │
_IPFilter/127.0.0.1-10                   4.595µ ± 1%   4.753µ ± 2%  +3.44% (p=0.002 n=6)
_IPFilter/192.168.0.1-192.189.10.12-10   4.611µ ± 1%   4.608µ ± 1%       ~ (p=0.784 n=6)
_IPFilter/192.168.4.5/16-10              4.374µ ± 0%   4.339µ ± 0%  -0.81% (p=0.002 n=6)
geomean                                  4.525µ        4.563µ       +0.84%

                                       │  old.txt   │              new.txt              │
                                       │    B/op    │    B/op     vs base               │
_IPFilter/127.0.0.1-10                   341.0 ± 0%   272.0 ± 0%  -20.23% (p=0.002 n=6)
_IPFilter/192.168.0.1-192.189.10.12-10   341.0 ± 0%   272.0 ± 0%  -20.23% (p=0.002 n=6)
_IPFilter/192.168.4.5/16-10              341.0 ± 0%   272.0 ± 0%  -20.23% (p=0.002 n=6)
geomean                                  341.0        272.0       -20.23%

                                       │  old.txt   │              new.txt               │
                                       │ allocs/op  │ allocs/op   vs base                │
_IPFilter/127.0.0.1-10                   8.000 ± 0%   8.000 ± 0%       ~ (p=1.000 n=6) ¹
_IPFilter/192.168.0.1-192.189.10.12-10   8.000 ± 0%   8.000 ± 0%       ~ (p=1.000 n=6) ¹
_IPFilter/192.168.4.5/16-10              8.000 ± 0%   8.000 ± 0%       ~ (p=1.000 n=6) ¹
geomean                                  8.000        8.000       +0.00%
¹ all samples are equal
```
@KasonBraley KasonBraley requested a review from a team as a code owner September 6, 2024 17:15
@CLAassistant
Copy link

CLAassistant commented Sep 6, 2024

CLA assistant check
All committers have signed the CLA.

We can optimize even more here by eliminating the use of the map
and opting to use a 256 len array instead for the lookup.

```
goos: darwin
goarch: arm64
pkg: github.com/grafana/loki/v3/pkg/logql/log
cpu: Apple M2 Pro
                                       │   old.txt   │               new.txt               │
                                       │   sec/op    │    sec/op     vs base               │
_IPFilter/127.0.0.1-10                   4.609µ ± 1%   3.155µ ±  1%  -31.55% (p=0.002 n=6)
_IPFilter/192.168.0.1-192.189.10.12-10   4.621µ ± 2%   3.328µ ± 10%  -27.98% (p=0.002 n=6)
_IPFilter/192.168.4.5/16-10              4.388µ ± 2%   2.947µ ±  1%  -32.84% (p=0.002 n=6)
geomean                                  4.538µ        3.139µ        -30.82%

                                       │   old.txt   │              new.txt              │
                                       │    B/op     │    B/op     vs base               │
_IPFilter/127.0.0.1-10                   272.00 ± 0%   32.00 ± 0%  -88.24% (p=0.002 n=6)
_IPFilter/192.168.0.1-192.189.10.12-10   272.00 ± 0%   32.00 ± 0%  -88.24% (p=0.002 n=6)
_IPFilter/192.168.4.5/16-10              272.00 ± 0%   32.00 ± 0%  -88.24% (p=0.002 n=6)
geomean                                   272.0        32.00       -88.24%

                                       │  old.txt   │              new.txt              │
                                       │ allocs/op  │ allocs/op   vs base               │
_IPFilter/127.0.0.1-10                   8.000 ± 0%   3.000 ± 0%  -62.50% (p=0.002 n=6)
_IPFilter/192.168.0.1-192.189.10.12-10   8.000 ± 0%   3.000 ± 0%  -62.50% (p=0.002 n=6)
_IPFilter/192.168.4.5/16-10              8.000 ± 0%   3.000 ± 0%  -62.50% (p=0.002 n=6)
geomean                                  8.000        3.000       -62.50%
```
Copy link
Contributor

@cyriltovena cyriltovena left a comment

Choose a reason for hiding this comment

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

LGTM

@cyriltovena cyriltovena merged commit c5083c7 into grafana:main Sep 10, 2024
61 checks passed
@KasonBraley KasonBraley deleted the ip-perf branch September 10, 2024 14:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants