Skip to content

Commit 6632fe0

Browse files
committed
Memoize EmojiSupport.recommended
It's a simple function and I believe the result is not supposed to change at runtime. Strings were not frozen, so it made a bunch of useless allocations too. I found this method while benchmarking, which was unexpected. Some basic numbers: ``` # frozen_string_literal: true require 'unicode/display_width' require 'benchmark/ips' Benchmark.ips do |x| x.report do Unicode::DisplayWidth.of("foo") end x.compare! end ``` Old gives `207.779k` iterations per second, while new one is `427.344k`. So more than 2x faster for basic cases
1 parent 85692f4 commit 6632fe0

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

lib/unicode/display_width/emoji_support.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
# require "rbconfig"
2-
# RbConfig::CONFIG["host_os"] =~ /mswin|mingw/ # windows
1+
# frozen_string_literal: true
32

43
module Unicode
54
class DisplayWidth
@@ -13,6 +12,10 @@ module EmojiSupport
1312
# Please note: Many terminals do not set any ENV vars,
1413
# maybe CSI queries can help?
1514
def self.recommended
15+
@recommended ||= _recommended
16+
end
17+
18+
def self._recommended
1619
if ENV["CI"]
1720
return :rqi
1821
end

0 commit comments

Comments
 (0)