Skip to content

contrib/google.golang.org/api: lazy init regexp#2197

Merged
rarguelloF merged 6 commits into
mainfrom
felix.geisendoerfer/lazy-init-regexp
Sep 7, 2023
Merged

contrib/google.golang.org/api: lazy init regexp#2197
rarguelloF merged 6 commits into
mainfrom
felix.geisendoerfer/lazy-init-regexp

Conversation

@felixge

@felixge felixge commented Sep 1, 2023

Copy link
Copy Markdown
Member

What does this PR do?

Make regexp initialization lazy. Significantly reduce memory usage caused by importing this package.

Note: There is probably more room for improvement here:

  • I suspect the json.Unmarshal allocs can be significantly reduced.
  • The gen_endpoints.json could be gzipped which would reduce binary sizes by ~8MB (in exchange for a small startup cost of decompression) (this is probably offset by saving the regexp compilation startup cost eliminated in this PR)

Motivation

Problem reported by user in public slack.

image

Reviewer's Checklist

  • Changed code has unit tests for its functionality at or near 100% coverage.
  • There is a benchmark for any new code, or changes to existing code.
  • If this interacts with the agent in a new way, a system test has been added.

For Datadog employees:

  • If this PR touches code that handles credentials of any kind, such as Datadog API keys, I've requested a review from @DataDog/security-design-and-guidance.
  • This PR doesn't touch any of that.

Unsure? Have a question? Request a review!

@pr-commenter

pr-commenter Bot commented Sep 1, 2023

Copy link
Copy Markdown

Benchmarks

Benchmark execution time: 2023-09-07 10:35:48

Comparing candidate commit 4101e36 in PR branch felix.geisendoerfer/lazy-init-regexp with baseline commit 3a403a7 in branch main.

Found 0 performance improvements and 0 performance regressions! Performance is the same for 40 metrics, 1 unstable metrics.

pathMatcher, err := regexp.Compile(e.PathRegex)
if err != nil {
log.Warn("contrib/google.golang.org/api: failed to create regex: %s: %v", e.PathRegex, err)
return Endpoint{}, false

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think in this case it's preferable to skip this endpoint and keep trying with the rest of the endpoints instead of directly returning false.

Also, we can add a very simple unit test that checks the existing regexes compile properly, so we make sure this situation never happens at runtime.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I was just hacking this together quickly 😅. Testing all the regex with a unit test and then assuming they won't fail (or trying the next one) sounds good to me.

@rarguelloF rarguelloF changed the title WIP: lazy init regexp contrib/google.golang.org/api: lazy init regexp Sep 5, 2023
@rarguelloF
rarguelloF marked this pull request as ready for review September 6, 2023 11:30
@rarguelloF
rarguelloF requested a review from a team September 6, 2023 11:30

@ajgajg1134 ajgajg1134 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good! before merging can you include a benchstat comparison for the new benchmarks to show the improved memory?

@felixge felixge left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

LGTM. benchstat output for future reference:

goos: darwin
goarch: arm64
pkg: gopkg.in/DataDog/dd-trace-go.v1/contrib/google.golang.org/api
                        │     a.txt     │                b.txt                │
                        │    sec/op     │    sec/op     vs base               │
InitApiEndpointsTree-10   239.89m ± ∞ ¹   56.65m ± ∞ ¹  -76.38% (p=0.008 n=5)
¹ need >= 6 samples for confidence interval at level 0.95

                        │     a.txt      │                b.txt                 │
                        │      B/op      │     B/op       vs base               │
InitApiEndpointsTree-10   378.25Mi ± ∞ ¹   27.31Mi ± ∞ ¹  -92.78% (p=0.008 n=5)
¹ need >= 6 samples for confidence interval at level 0.95

                        │     a.txt     │                b.txt                │
                        │   allocs/op   │  allocs/op    vs base               │
InitApiEndpointsTree-10   2918.0k ± ∞ ¹   177.6k ± ∞ ¹  -93.91% (p=0.008 n=5)
¹ need >= 6 samples for confidence interval at level 0.95

@rarguelloF
rarguelloF self-requested a review September 7, 2023 10:22
@rarguelloF
rarguelloF merged commit 8868d37 into main Sep 7, 2023
@rarguelloF
rarguelloF deleted the felix.geisendoerfer/lazy-init-regexp branch September 7, 2023 10:36
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.

3 participants