mirror of
https://github.com/discourse/discourse.git
synced 2025-01-18 21:02:48 +08:00
PERF: Optimize ActionView::Helpers::TextHelper#excerpt
.
This commit is contained in:
parent
801a078105
commit
ee5d8fba0c
|
@ -9,6 +9,27 @@ class Search
|
|||
|
||||
class TextHelper
|
||||
extend ActionView::Helpers::TextHelper
|
||||
|
||||
private
|
||||
|
||||
# TODO: Remove when https://github.com/rails/rails/pull/39979 is merged
|
||||
# For a 10_000 words string, speeds up excerpts by 85X.
|
||||
def self.cut_excerpt_part(part_position, part, separator, options)
|
||||
return "", "" unless part
|
||||
|
||||
radius = options.fetch(:radius, 100)
|
||||
omission = options.fetch(:omission, "...")
|
||||
|
||||
if separator != ""
|
||||
part = part.split(separator)
|
||||
part.delete("")
|
||||
end
|
||||
|
||||
affix = part.length > radius ? omission : ""
|
||||
part = part.public_send(part_position == :first ? :last : :first, radius)
|
||||
part = part.join(separator) if separator != ""
|
||||
return affix, part
|
||||
end
|
||||
end
|
||||
|
||||
attr_reader(
|
||||
|
|
Loading…
Reference in New Issue
Block a user