From b912d65f42b13689576e7b6c73048854fd123dd7 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Mon, 5 Jan 2026 05:36:09 +0000 Subject: [PATCH] Optimize regex compilation in TLS tracer Moved regex compilations in `ebpftracer/tls.go` to package-level variables. Previously, `regexp.MustCompile` was called every time `getSslLibPathAndVersion` was executed (scanning process maps), which is expensive. This change compiles the regexes once at startup, reducing CPU overhead and allocations. Impact: - Eliminates regex compilation overhead (parsing + compilation) for each call. - Reduces memory allocations during process scanning. --- ebpftracer/tls.go | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/ebpftracer/tls.go b/ebpftracer/tls.go index 02ad5abd..06b7e489 100644 --- a/ebpftracer/tls.go +++ b/ebpftracer/tls.go @@ -27,6 +27,12 @@ const ( var ( opensslVersionRe = regexp.MustCompile(`OpenSSL\s(\d\.\d+\.\d+)`) + // Regex to find libraries that look like libssl or libcrypto, even with version numbers. + // e.g., libssl.so.3, libcrypto-74fbf0e0.so.3 + libSslRe = regexp.MustCompile(`libssl\.so(\.\d+)*`) + libCryptoRe = regexp.MustCompile(`libcrypto\.so(\.\d+)*`) + // A more specific regex for psycopg2's bundled libs + psycopg2LibRe = regexp.MustCompile(`lib(ssl|crypto)-[a-f0-9]+\.so\.\d+`) ) func (t *Tracer) AttachOpenSslUprobes(pid uint32) []link.Link { @@ -271,13 +277,6 @@ func getSslLibPathAndVersion(pid uint32) (string, string) { } defer f.Close() - // Regex to find libraries that look like libssl or libcrypto, even with version numbers. - // e.g., libssl.so.3, libcrypto-74fbf0e0.so.3 - libSslRe := regexp.MustCompile(`libssl\.so(\.\d+)*`) - libCryptoRe := regexp.MustCompile(`libcrypto\.so(\.\d+)*`) - // A more specific regex for psycopg2's bundled libs - psycopg2LibRe := regexp.MustCompile(`lib(ssl|crypto)-[a-f0-9]+\.so\.\d+`) - scanner := bufio.NewScanner(f) scanner.Split(bufio.ScanLines) var libsslPath, libcryptoPath string