diff options
author | James Tucker <jftucker@gmail.com> | 2022-11-09 20:49:30 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-10 05:49:30 +0100 |
commit | 78a6b871fe3dae308380ea88132ddadc86a1431e (patch) | |
tree | 355c20f14f76d034c9e2b9e90d2e9c86745d4b87 /app/models | |
parent | 0cd0786aef140ea41aa229cd52ac67867259a3f5 (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/models')
-rw-r--r-- | app/models/account.rb | 3 | ||||
-rw-r--r-- | app/models/custom_emoji.rb | 3 | ||||
-rw-r--r-- | app/models/featured_tag.rb | 2 | ||||
-rw-r--r-- | app/models/tag.rb | 13 |
4 files changed, 13 insertions, 8 deletions
diff --git a/app/models/account.rb b/app/models/account.rb index be1968fa6..cc3a8f3df 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -64,6 +64,7 @@ class Account < ApplicationRecord USERNAME_RE = /[a-z0-9_]+([a-z0-9_\.-]+[a-z0-9_]+)?/i MENTION_RE = /(?<=^|[^\/[:word:]])@((#{USERNAME_RE})(?:@[[:word:]\.\-]+[[:word:]]+)?)/i URL_PREFIX_RE = /\Ahttp(s?):\/\/[^\/]+/ + USERNAME_ONLY_RE = /\A#{USERNAME_RE}\z/i include Attachmentable include AccountAssociations @@ -84,7 +85,7 @@ class Account < ApplicationRecord validates_with UniqueUsernameValidator, if: -> { will_save_change_to_username? } # Remote user validations - validates :username, format: { with: /\A#{USERNAME_RE}\z/i }, if: -> { !local? && will_save_change_to_username? } + validates :username, format: { with: USERNAME_ONLY_RE }, if: -> { !local? && will_save_change_to_username? } # Local user validations validates :username, format: { with: /\A[a-z0-9_]+\z/i }, length: { maximum: 30 }, if: -> { local? && will_save_change_to_username? && actor_type != 'Application' } diff --git a/app/models/custom_emoji.rb b/app/models/custom_emoji.rb index 077ce559a..7b19cd2ac 100644 --- a/app/models/custom_emoji.rb +++ b/app/models/custom_emoji.rb @@ -30,6 +30,7 @@ class CustomEmoji < ApplicationRecord SCAN_RE = /(?<=[^[:alnum:]:]|\n|^) :(#{SHORTCODE_RE_FRAGMENT}): (?=[^[:alnum:]:]|$)/x + SHORTCODE_ONLY_RE = /\A#{SHORTCODE_RE_FRAGMENT}\z/ IMAGE_MIME_TYPES = %w(image/png image/gif image/webp).freeze @@ -41,7 +42,7 @@ class CustomEmoji < ApplicationRecord before_validation :downcase_domain validates_attachment :image, content_type: { content_type: IMAGE_MIME_TYPES }, presence: true, size: { less_than: LIMIT } - validates :shortcode, uniqueness: { scope: :domain }, format: { with: /\A#{SHORTCODE_RE_FRAGMENT}\z/ }, length: { minimum: 2 } + validates :shortcode, uniqueness: { scope: :domain }, format: { with: SHORTCODE_ONLY_RE }, length: { minimum: 2 } scope :local, -> { where(domain: nil) } scope :remote, -> { where.not(domain: nil) } diff --git a/app/models/featured_tag.rb b/app/models/featured_tag.rb index debae2212..70f949b6a 100644 --- a/app/models/featured_tag.rb +++ b/app/models/featured_tag.rb @@ -17,7 +17,7 @@ class FeaturedTag < ApplicationRecord belongs_to :account, inverse_of: :featured_tags belongs_to :tag, inverse_of: :featured_tags, optional: true # Set after validation - validates :name, presence: true, format: { with: /\A(#{Tag::HASHTAG_NAME_RE})\z/i }, on: :create + validates :name, presence: true, format: { with: Tag::HASHTAG_NAME_RE }, on: :create validate :validate_tag_uniqueness, on: :create validate :validate_featured_tags_limit, on: :create diff --git a/app/models/tag.rb b/app/models/tag.rb index 8929baf66..b66f85423 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -27,11 +27,14 @@ class Tag < ApplicationRecord has_many :followers, through: :passive_relationships, source: :account HASHTAG_SEPARATORS = "_\u00B7\u200c" - HASHTAG_NAME_RE = "([[:word:]_][[:word:]#{HASHTAG_SEPARATORS}]*[[:alpha:]#{HASHTAG_SEPARATORS}][[:word:]#{HASHTAG_SEPARATORS}]*[[:word:]_])|([[:word:]_]*[[:alpha:]][[:word:]_]*)" - HASHTAG_RE = /(?:^|[^\/\)\w])#(#{HASHTAG_NAME_RE})/i + HASHTAG_NAME_PAT = "([[:word:]_][[:word:]#{HASHTAG_SEPARATORS}]*[[:alpha:]#{HASHTAG_SEPARATORS}][[:word:]#{HASHTAG_SEPARATORS}]*[[:word:]_])|([[:word:]_]*[[:alpha:]][[:word:]_]*)" - validates :name, presence: true, format: { with: /\A(#{HASHTAG_NAME_RE})\z/i } - validates :display_name, format: { with: /\A(#{HASHTAG_NAME_RE})\z/i } + HASHTAG_RE = /(?:^|[^\/\)\w])#(#{HASHTAG_NAME_PAT})/i + HASHTAG_NAME_RE = /\A(#{HASHTAG_NAME_PAT})\z/i + HASHTAG_INVALID_CHARS_RE = /[^[:alnum:]#{HASHTAG_SEPARATORS}]/ + + validates :name, presence: true, format: { with: HASHTAG_NAME_RE } + validates :display_name, format: { with: HASHTAG_NAME_RE } validate :validate_name_change, if: -> { !new_record? && name_changed? } validate :validate_display_name_change, if: -> { !new_record? && display_name_changed? } @@ -102,7 +105,7 @@ class Tag < ApplicationRecord names = Array(name_or_names).map { |str| [normalize(str), str] }.uniq(&:first) names.map do |(normalized_name, display_name)| - tag = matching_name(normalized_name).first || create(name: normalized_name, display_name: display_name.gsub(/[^[:alnum:]#{HASHTAG_SEPARATORS}]/, '')) + tag = matching_name(normalized_name).first || create(name: normalized_name, display_name: display_name.gsub(HASHTAG_INVALID_CHARS_RE, '')) yield tag if block_given? |