about summary refs log tree commit diff
path: root/app/services
diff options
context:
space:
mode:
authorJames Tucker <jftucker@gmail.com>2022-11-09 20:49:30 -0800
committerGitHub <noreply@github.com>2022-11-10 05:49:30 +0100
commit78a6b871fe3dae308380ea88132ddadc86a1431e (patch)
tree355c20f14f76d034c9e2b9e90d2e9c86745d4b87 /app/services
parent0cd0786aef140ea41aa229cd52ac67867259a3f5 (diff)
Improve performance by avoiding regex construction (#20215)
```ruby
10.times { p /#{FOO}/.object_id }
10.times { p FOO_RE.object_id }
```
Diffstat (limited to 'app/services')
-rw-r--r--app/services/account_search_service.rb4
-rw-r--r--app/services/resolve_url_service.rb4
2 files changed, 6 insertions, 2 deletions
diff --git a/app/services/account_search_service.rb b/app/services/account_search_service.rb
index 9f2330a94..85538870b 100644
--- a/app/services/account_search_service.rb
+++ b/app/services/account_search_service.rb
@@ -3,6 +3,8 @@
 class AccountSearchService < BaseService
   attr_reader :query, :limit, :offset, :options, :account
 
+  MENTION_ONLY_RE = /\A#{Account::MENTION_RE}\z/i
+
   # Min. number of characters to look for non-exact matches
   MIN_QUERY_LENGTH = 5
 
@@ -180,7 +182,7 @@ class AccountSearchService < BaseService
   end
 
   def username_complete?
-    query.include?('@') && "@#{query}".match?(/\A#{Account::MENTION_RE}\Z/)
+    query.include?('@') && "@#{query}".match?(MENTION_ONLY_RE)
   end
 
   def likely_acct?
diff --git a/app/services/resolve_url_service.rb b/app/services/resolve_url_service.rb
index 37c856cf8..52f35daf3 100644
--- a/app/services/resolve_url_service.rb
+++ b/app/services/resolve_url_service.rb
@@ -4,6 +4,8 @@ class ResolveURLService < BaseService
   include JsonLdHelper
   include Authorization
 
+  USERNAME_STATUS_RE = %r{/@(?<username>#{Account::USERNAME_RE})/(?<status_id>[0-9]+)\Z}
+
   def call(url, on_behalf_of: nil)
     @url          = url
     @on_behalf_of = on_behalf_of
@@ -43,7 +45,7 @@ class ResolveURLService < BaseService
 
     # We don't have an index on `url`, so try guessing the `uri` from `url`
     parsed_url = Addressable::URI.parse(@url)
-    parsed_url.path.match(%r{/@(?<username>#{Account::USERNAME_RE})/(?<status_id>[0-9]+)\Z}) do |matched|
+    parsed_url.path.match(USERNAME_STATUS_RE) do |matched|
       parsed_url.path = "/users/#{matched[:username]}/statuses/#{matched[:status_id]}"
       scope = scope.or(Status.where(uri: parsed_url.to_s, url: @url))
     end