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(detection): use strings.Builder #1366

Merged
merged 1 commit into from
Jul 4, 2023
Merged

Conversation

nikpivkin
Copy link
Collaborator

@nikpivkin nikpivkin commented Jul 3, 2023

Currently, in some places, when parsing ARM the resulting string is created by concatenation. Since string is a primitive type and a new string will be created each time, this is not effective. In go 1.10, was added strings.Builder, which effectively creates a string - O(n).

See https://github.com/aquasecurity/defsec/issues/1108

benchstat concat=old.txt strings.Builder=new.txt
goos: darwin
goarch: arm64
pkg: github.com/aquasecurity/defsec/pkg/scanners/azure/arm/parser/armjson
                        │    concat    │            strings.Builder            │
                        │    sec/op    │    sec/op     vs base                 │
Unmarshal_JFather-8       25.45µ ± ∞ ¹   17.54µ ± ∞ ¹        ~ (p=1.000 n=1) ²
Unmarshal_Traditional-8   4.705µ ± ∞ ¹   4.934µ ± ∞ ¹        ~ (p=1.000 n=1) ²
geomean                   10.94µ         9.303µ        -14.99%
¹ need >= 6 samples for confidence interval at level 0.95
² need >= 4 samples to detect a difference at alpha level 0.05

                        │    concat     │            strings.Builder             │
                        │     B/op      │     B/op       vs base                 │
Unmarshal_JFather-8       27.38Ki ± ∞ ¹   22.13Ki ± ∞ ¹        ~ (p=1.000 n=1) ²
Unmarshal_Traditional-8   2.516Ki ± ∞ ¹   2.516Ki ± ∞ ¹        ~ (p=1.000 n=1) ³
geomean                   8.300Ki         7.462Ki        -10.10%
¹ need >= 6 samples for confidence interval at level 0.95
² need >= 4 samples to detect a difference at alpha level 0.05
³ all samples are equal

                        │   concat    │           strings.Builder            │
                        │  allocs/op  │  allocs/op   vs base                 │
Unmarshal_JFather-8       724.0 ± ∞ ¹   251.0 ± ∞ ¹        ~ (p=1.000 n=1) ²
Unmarshal_Traditional-8   57.00 ± ∞ ¹   57.00 ± ∞ ¹        ~ (p=1.000 n=1) ³
geomean                   203.1         119.6        -41.12%
¹ need >= 6 samples for confidence interval at level 0.95
² need >= 4 samples to detect a difference at alpha level 0.05
³ all samples are equal

Testing the detection. I added the IsType_BigJson test with the file from this comment.

benchstat concat=old.txt strings.Builder=new.txt
goos: darwin
goarch: arm64
pkg: github.com/aquasecurity/defsec/pkg/detection
                   │     concat      │            strings.Builder            │
                   │     sec/op      │    sec/op     vs base                 │
IsType_SmallFile-8      2.120µ ± ∞ ¹   1.710µ ± ∞ ¹        ~ (p=1.000 n=1) ²
IsType_BigFile-8        641.5n ± ∞ ¹   658.4n ± ∞ ¹        ~ (p=1.000 n=1) ²
IsType_BigJson-8     464078.7µ ± ∞ ¹   793.0µ ± ∞ ¹        ~ (p=1.000 n=1) ²
geomean                 85.78µ         9.629µ        -88.77%
¹ need >= 6 samples for confidence interval at level 0.95
² need >= 4 samples to detect a difference at alpha level 0.05

                   │      concat       │            strings.Builder             │
                   │       B/op        │     B/op       vs base                 │
IsType_SmallFile-8       5.760Ki ± ∞ ¹   5.595Ki ± ∞ ¹        ~ (p=1.000 n=1) ²
IsType_BigFile-8         4.298Ki ± ∞ ¹   4.298Ki ± ∞ ¹        ~ (p=1.000 n=1) ³
IsType_BigJson-8     6955356.7Ki ± ∞ ¹   508.5Ki ± ∞ ¹        ~ (p=1.000 n=1) ²
geomean                  556.3Ki         23.04Ki        -95.86%
¹ need >= 6 samples for confidence interval at level 0.95
² need >= 4 samples to detect a difference at alpha level 0.05
³ all samples are equal

                   │     concat      │           strings.Builder            │
                   │    allocs/op    │  allocs/op   vs base                 │
IsType_SmallFile-8       57.00 ± ∞ ¹   26.00 ± ∞ ¹        ~ (p=1.000 n=1) ²
IsType_BigFile-8         7.000 ± ∞ ¹   7.000 ± ∞ ¹        ~ (p=1.000 n=1) ³
IsType_BigJson-8     232617.00 ± ∞ ¹   48.00 ± ∞ ¹        ~ (p=1.000 n=1) ²
geomean                  452.8         20.60        -95.45%
¹ need >= 6 samples for confidence interval at level 0.95
² need >= 4 samples to detect a difference at alpha level 0.05
³ all samples are equal

@nikpivkin nikpivkin marked this pull request as ready for review July 3, 2023 10:55
@nikpivkin nikpivkin requested review from giorod3 and simar7 as code owners July 3, 2023 10:55
Copy link
Member

@simar7 simar7 left a comment

Choose a reason for hiding this comment

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

thanks lgtm!

@simar7 simar7 merged commit 48d661f into aquasecurity:master Jul 4, 2023
@nikpivkin nikpivkin deleted the use-sb branch July 20, 2023 16:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants