From 256c033957225a57b78374109efaeff8c9232443 Mon Sep 17 00:00:00 2001 From: oppiliappan Date: Wed, 16 Jul 2025 17:07:24 +0100 Subject: [PATCH] appview: reduce allocations in router regexes were recompiled on each router visit. Signed-off-by: oppiliappan --- appview/state/userutil/userutil.go | 14 ++++++++------ flake.nix | 3 ++- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/appview/state/userutil/userutil.go b/appview/state/userutil/userutil.go index d7cb40d..f29fce7 100644 --- a/appview/state/userutil/userutil.go +++ b/appview/state/userutil/userutil.go @@ -5,10 +5,14 @@ import ( "strings" ) +var ( + handleRegex = regexp.MustCompile(`^([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?$`) + didRegex = regexp.MustCompile(`^did:[a-z]+:[a-zA-Z0-9._:%-]*[a-zA-Z0-9._-]$`) +) + func IsHandleNoAt(s string) bool { // ref: https://atproto.com/specs/handle - re := regexp.MustCompile(`^([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?$`) - return re.MatchString(s) + return handleRegex.MatchString(s) } func UnflattenDid(s string) string { @@ -29,9 +33,8 @@ func IsFlattenedDid(s string) bool { // Reconstruct as a standard DID format using Replace // Example: "did-plc-xyz-abc" becomes "did:plc:xyz-abc" reconstructed := strings.Replace(s, "-", ":", 2) - re := regexp.MustCompile(`^did:[a-z]+:[a-zA-Z0-9._:%-]*[a-zA-Z0-9._-]$`) - return re.MatchString(reconstructed) + return didRegex.MatchString(reconstructed) } // FlattenDid converts a DID to a flattened format. @@ -46,6 +49,5 @@ func FlattenDid(s string) string { // IsDid checks if the given string is a standard DID. func IsDid(s string) bool { - re := regexp.MustCompile(`^did:[a-z]+:[a-zA-Z0-9._:%-]*[a-zA-Z0-9._-]$`) - return re.MatchString(s) + return didRegex.MatchString(s) } diff --git a/flake.nix b/flake.nix index 1f4a87a..34ab1ed 100644 --- a/flake.nix +++ b/flake.nix @@ -150,7 +150,8 @@ '' ${pkgs.air}/bin/air -c /dev/null \ -build.cmd "${pkgs.go}/bin/go build -o ./out/${name}.out ./cmd/${name}/main.go" \ - -build.bin "./out/${name}.out ${arg}" \ + -build.bin "./out/${name}.out" \ + -build.args_bin "${arg}" -build.stop_on_error "true" \ -build.include_ext "go" ''; -- 2.51.2