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: avoid regex re-compile #2700

Merged
merged 4 commits into from
Sep 23, 2024
Merged

Conversation

trim21
Copy link
Contributor

@trim21 trim21 commented Sep 23, 2024

Summary

found re.findall and re.sub usage in starlette module, we should always pre-compile regex pattern if it's possible.

Althrough re has a built-in cache, but it has size limit and for project using many regex it still may be invalidated and pattern get unnecessary re-compile.

There is still one case in

route_path = re.sub(r"^" + root_path + r"(?=/|$)", "", scope["path"])

It's not a simple case so I didn't touch it.

Checklist

  • I understand that this PR may be closed in case there was no previous discussion. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.

@trim21 trim21 marked this pull request as draft September 23, 2024 06:46
@trim21 trim21 marked this pull request as ready for review September 23, 2024 06:48
@Kludex
Copy link
Member

Kludex commented Sep 23, 2024

Thanks! 🙏

@Kludex Kludex merged commit 9d4d5a5 into encode:master Sep 23, 2024
6 checks passed
@trim21 trim21 deleted the perf-avoid-regex-compile branch September 23, 2024 06:55
nixroxursox pushed a commit to nixroxursox/starlette that referenced this pull request Sep 30, 2024
* perf

* format

* avoid

* fix
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