Skip to content
This repository has been archived by the owner on Sep 30, 2024. It is now read-only.

zoekt: 40% of CPU cycles spent on regexp.String #61462

Closed
keegancsmith opened this issue Mar 28, 2024 · 7 comments
Closed

zoekt: 40% of CPU cycles spent on regexp.String #61462

keegancsmith opened this issue Mar 28, 2024 · 7 comments
Assignees
Labels
team/search-platform Issues owned by the search platform team

Comments

@keegancsmith
Copy link
Member

keegancsmith commented Mar 28, 2024

@keegancsmith keegancsmith added the team/search-platform Issues owned by the search platform team label Mar 28, 2024
@keegancsmith
Copy link
Member Author

The above graph indicates on march 12th we went from this being 0% to 60% of CPU. Checking our deployment logs that was the first time we deployed with a version compiled with go1.22. This is likely a regression related to that. Here are the commits that changed in that deployment as well, I don't see anything that would change the behaviour of how we construct a regexp matchtree:

$ git log --oneline 19fa44cea9eb...371a303692de
371a303 sourcegraph: Remove HTTP APIs (#738)
74bbcc9 ranking: allow partial overlap with symbol (#742)
27d9399 remove deprecated ShardMaxImportantMatch TotalMaxImportantMatch (#744)
1c158f9 Don't truncate file before detecting language (#740)
b227501 all: gofumpt -l -w .
245e0ce gomod: update otel and circl for CVEs (#737)
7ec3d8e gomod: update grpc for GHSA-m425-mq94-257g (#736)
0ddb91f all: use stdlib slices package (#735)
6201776 gomod: update mountinfo to latest (#734)
746f388 matchtree: fix panic for missing files (#733)
fdc144f all: gofmt -s -w . (#730)

@keegancsmith
Copy link
Member Author

Alright it is almost certainly this change which is the root cause golang/go@98c9f27

It introduces the use of unicode.SimpleFold over all runes in your literal. As mentioned in the commit description this is supposed to be only used for debugging. However, we need it so we can convert a syntax.Regexp into a regexp.Regexp. It has always bothered me we went via .String, so will investigate if we can do something else.

@mmanela
Copy link
Contributor

mmanela commented Mar 28, 2024

@keegancsmith What impact on the end-user would this have? Does it impact user perf or just is using too much CPU overall?

@keegancsmith
Copy link
Member Author

This has a noticeable impact, but not quite as much as half as slow given that IO is often the bottleneck and CPU usage is bursty. But for example I just checked our continuous performance monitoring and you can notice a regression in the graphs when this was deployed to dotcom:

https://ui.honeycomb.io/sourcegraph/datasets/search/result/bLDGcUQRjgh

image

@keegancsmith
Copy link
Member Author

Here is a temporary fix, just use a version of regexp.String with the slowdown reverted out of it: sourcegraph/zoekt#753

Bigger change: We rely on grafana/regexp but it hasn't been updated in 2 years. So I…

Now that we control the fork I started adding things I wish we could do:

I want to finish up this change next week. But before I do that some other simplifications to zoekt:

  • Remove our support for gob and net/rpc. This introduces lots of uses of syntax.Regexp which make it hard to interact.
  • Possibly introduce an intermediate representation between query.Q and matchtree. Then it doesn't matter that Regexp.String is slower since we will only run it once per search instead of once per shard. This is bigger change, so might be better to just rely on our fork.

@keegancsmith keegancsmith added the release-blocker Prevents us from releasing: https://about.sourcegraph.com/handbook/engineering/releases label Apr 2, 2024
@keegancsmith
Copy link
Member Author

keegancsmith commented Apr 2, 2024

Fairly confident the workaround in sourcegraph/zoekt#753 will solve this problem in the shortterm. My goal today related to this work is to:

  • ship the change to sourcegraph.com
  • monitor effects on the profiler
  • file follow up issues (mostly related to future work in my last comment)
  • close this.

The goal is that we don't ship this regression in the next release which is being cut tomorrow. This is why I am also marking this as a release blocker.

@keegancsmith
Copy link
Member Author

keegancsmith commented Apr 2, 2024

  • I filed https://github.com/sourcegraph/sourcegraph/issues/61524 to track the tech debt introduced to solve this problem
  • profiler output on dotcom has syntax.Regexp.String nowhere to be seen.
  • Look at this beautiful graph of CPU usage on dotcom for the last week (we need to scale down as well!)

image

metrics-explorer link

@mmanela mmanela removed the release-blocker Prevents us from releasing: https://about.sourcegraph.com/handbook/engineering/releases label May 21, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
team/search-platform Issues owned by the search platform team
Projects
None yet
Development

No branches or pull requests

2 participants