forked from jruby/jruby
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce FString to cache common conversions
An FString is a frozen string that has additionally been de- duplicated and cached. Since such strings are often later converted to symbols, integers, or floats, the FString class adds fields to lazily cache those converted values. This helps, for example, when using APIs that can take either strings or symbols but need a symbol internally; if the incoming string is an FString the symbol created from it will be cached and more readily accessible. Performance of FString conversions is many times faster with this change at the cost of all FStrings having three additional reference fields. Warming up -------------------------------------- intern normal 6.203M i/100ms intern fstring 25.811M i/100ms to_i normal 9.876M i/100ms to_i fstring 27.629M i/100ms to_f normal 14.780M i/100ms to_f fstring 27.612M i/100ms Calculating ------------------------------------- intern normal 62.499M (± 0.5%) i/s - 316.353M in 5.061824s intern fstring 274.520M (± 1.0%) i/s - 1.394B in 5.077736s to_i normal 97.868M (± 1.3%) i/s - 493.824M in 5.046716s to_i fstring 273.394M (± 1.3%) i/s - 1.381B in 5.053858s to_f normal 146.627M (± 1.2%) i/s - 739.006M in 5.040805s to_f fstring 271.105M (± 1.5%) i/s - 1.381B in 5.093658s
- Loading branch information
Showing
3 changed files
with
124 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# frozen-string-literal: true | ||
|
||
require 'benchmark/ips' | ||
|
||
def intern(a) | ||
a.intern | ||
end | ||
|
||
def to_i(a) | ||
a.to_i | ||
end | ||
|
||
def to_f(a) | ||
a.to_f | ||
end | ||
|
||
FSTRING = "string" | ||
NORMAL = +FSTRING | ||
|
||
Benchmark.ips do |bm| | ||
bm.report("intern normal") do |i| | ||
while i > 0 | ||
intern(NORMAL) | ||
i-=1 | ||
end | ||
end | ||
|
||
bm.report("intern fstring") do |i| | ||
while i > 0 | ||
intern(FSTRING) | ||
i-=1 | ||
end | ||
end | ||
|
||
bm.report("to_i normal") do |i| | ||
while i > 0 | ||
to_i(NORMAL) | ||
i-=1 | ||
end | ||
end | ||
|
||
bm.report("to_i fstring") do |i| | ||
while i > 0 | ||
to_i(FSTRING) | ||
i-=1 | ||
end | ||
end | ||
|
||
bm.report("to_f normal") do |i| | ||
while i > 0 | ||
to_f(NORMAL) | ||
i-=1 | ||
end | ||
end | ||
|
||
bm.report("to_f fstring") do |i| | ||
while i > 0 | ||
to_f(FSTRING) | ||
i-=1 | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters