Merge pull request #4939 from tgxworld/memory_emoji
Reduce allocations in `Emoji.unicode_replacements`
This commit is contained in:
commit
482365b943
|
@ -108,28 +108,30 @@ class Emoji
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.replacement_code(code)
|
def self.replacement_code(code)
|
||||||
hexes = code.split('-').map(&:hex)
|
hexes = code.split('-'.freeze).map!(&:hex)
|
||||||
|
|
||||||
# Don't replace digits, letters and some symbols
|
# Don't replace digits, letters and some symbols
|
||||||
return hexes.pack("U" * hexes.size) if hexes[0] > 255
|
hexes.pack("U*".freeze) if hexes[0] > 255
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.unicode_replacements
|
def self.unicode_replacements
|
||||||
return @unicode_replacements if @unicode_replacements
|
return @unicode_replacements if @unicode_replacements
|
||||||
|
|
||||||
@unicode_replacements = {}
|
@unicode_replacements = {}
|
||||||
|
is_tonable_emojis = Emoji.tonable_emojis
|
||||||
|
fitzpatrick_scales = FITZPATRICK_SCALE.map { |scale| scale.to_i(16) }
|
||||||
|
|
||||||
db['emojis'].each do |e|
|
db['emojis'].each do |e|
|
||||||
next if e['name'] == 'tm'
|
name = e['name']
|
||||||
|
next if name == 'tm'.freeze
|
||||||
|
|
||||||
code = replacement_code(e['code'])
|
code = replacement_code(e['code'])
|
||||||
next unless code
|
next unless code
|
||||||
|
|
||||||
@unicode_replacements[code] = e['name']
|
@unicode_replacements[code] = name
|
||||||
if Emoji.tonable_emojis.include?(e['name'])
|
if is_tonable_emojis.include?(name)
|
||||||
FITZPATRICK_SCALE.each_with_index do |scale, index|
|
fitzpatrick_scales.each_with_index do |scale, index|
|
||||||
toned_code = (code.codepoints.insert(1, scale.to_i(16))).pack("U*")
|
toned_code = code.codepoints.insert(1, scale).pack("U*".freeze)
|
||||||
@unicode_replacements[toned_code] = "#{e['name']}:t#{index+2}"
|
@unicode_replacements[toned_code] = "#{name}:t#{index+2}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -156,6 +158,7 @@ class Emoji
|
||||||
def self.lookup_unicode(name)
|
def self.lookup_unicode(name)
|
||||||
@reverse_map ||= begin
|
@reverse_map ||= begin
|
||||||
map = {}
|
map = {}
|
||||||
|
is_tonable_emojis = Emoji.tonable_emojis
|
||||||
|
|
||||||
db['emojis'].each do |e|
|
db['emojis'].each do |e|
|
||||||
next if e['name'] == 'tm'
|
next if e['name'] == 'tm'
|
||||||
|
@ -164,7 +167,7 @@ class Emoji
|
||||||
next unless code
|
next unless code
|
||||||
|
|
||||||
map[e['name']] = code
|
map[e['name']] = code
|
||||||
if Emoji.tonable_emojis.include?(e['name'])
|
if is_tonable_emojis.include?(e['name'])
|
||||||
FITZPATRICK_SCALE.each_with_index do |scale, index|
|
FITZPATRICK_SCALE.each_with_index do |scale, index|
|
||||||
toned_code = (code.codepoints.insert(1, scale.to_i(16))).pack("U*")
|
toned_code = (code.codepoints.insert(1, scale.to_i(16))).pack("U*")
|
||||||
map["#{e['name']}:t#{index+2}"] = toned_code
|
map["#{e['name']}:t#{index+2}"] = toned_code
|
||||||
|
|
Loading…
Reference in New Issue