You can use "measure" command to check performance in IRB like below:
irb(main):001:0> 3
=> 3
irb(main):002:0> measure
TIME is added.
=> nil
irb(main):003:0> 3
processing time: 0.000058s
=> 3
irb(main):004:0> measure :off
=> nil
irb(main):005:0> 3
=> 3
You can set "measure :on" by "IRB.conf[:MEASURE] = true" in .irbrc, and, also,
set custom performance check method:
IRB.conf[:MEASURE_PROC][:CUSTOM] = proc { |context, code, line_no, &block|
time = Time.now
result = block.()
now = Time.now
puts 'custom processing time: %fs' % (Time.now - time) if IRB.conf[:MEASURE]
result
}
https://github.com/ruby/irb/commit/3899eaf2e2
This new mode uses a copy of the TOPLEVEL_BINDING. This is compatible with refinements (contrary to mode 3), while keeping nested IRB sessions separate
https://github.com/ruby/irb/commit/25c731cb2f
Miller Rabin algorithm can be used to test primality for integers smaller than a max value "MaxMR" (~3e24)
It can be much faster than previous implementation: ~100x faster for numbers with 13 digits, at least 5 orders of magnitude for even larger numbers (previous implementation is so slow that it requires more patience than I have for more precise estimate).
Miller Rabin test becomes faster than previous implementation at somewhere in the range 1e5-1e6. It seems that the range 62000..66000 is where Miller Rabin starts being always faster, so I picked 0xffff arbitrarily; before that, or above "MaxMR", the previous implementation remains.
I compared the `faster_prime` gem too. It is slower than previous implementation up to ~1e4. After that it becomes faster and faster compared to previous implementation, but is still slower than Miller Rabin starting at ~1e5 and up to MaxMR. Thus, after this commit, builtin `Integer#prime?` will be similar or faster than `faster_prime` up to "MaxMR".
Adapted from patch of Stephen Blackstone [Feature #16468]
Benchmark results and code: https://gist.github.com/marcandre/b263bdae488e76dabdda84daf73733b9
Co-authored-by: Stephen Blackstone <sblackstone@gmail.com>
Avoiding recursive call would imply iterating bits starting from
most significant, which is not easy to do efficiently.
Any saving would be dwarfed by the multiplications anyways.
[Feature #15233]
When running irb 1.2.1 (2019-12-24) with frozen-string-literal enabled, it crashes in reline with `can't modify frozen String (FrozenError)`
Steps to reproduce:
`RUBYOPT="--enable-frozen-string-literal" irb`
The rendering time in IRB has been reduced as follows:
start = Time.now
def each_top_level_statement
initialize_input
catch(:TERM_INPUT) do
loop do
begin
prompt
unless l = lex
throw :TERM_INPUT if @line == ''
else
@line_no += l.count("\n")
next if l == "\n"
@line.concat l
if @code_block_open or @ltype or @continue or @indent > 0
next
end
end
if @line != "\n"
@line.force_encoding(@io.encoding)
yield @line, @exp_line_no
end
break if @io.eof?
@line = ''
@exp_line_no = @line_no
@indent = 0
rescue TerminateLineInput
initialize_input
prompt
end
end
end
end
puts "Duration: #{Time.now - start} seconds"
0.33sec -> 0.22sec
https://github.com/ruby/reline/commit/496c6a1892
The rendering time in IRB has been reduced as follows:
start = Time.now
[{"_id"=>"5f9072a4589a06d2d74b6028",
"index"=>0,
"guid"=>"6b3051e2-dbc7-4537-bdb9-6cd7bb5358a7",
"isActive"=>true,
"balance"=>"$1,442.84",
"picture"=>"http://placehold.it/32x32",
"age"=>34,
"eyeColor"=>"blue",
"name"=>{"first"=>"Ward", "last"=>"Levy"},
"company"=>"HYPLEX",
"email"=>"ward.levy@hyplex.us",
"phone"=>"+1 (867) 568-3319",
"address"=>"867 Cobek Court, Clara, Maryland, 3254",
"about"=>
"Exercitation eu ex aliqua sit. Pariatur aliquip incididunt sint id non consectetur ullamco Lorem ea mollit duis amet sint labore. Commodo laborum labore commodo officia in cillum adipisicing esse excepteur cupidatat adipisicing ut. Non esse incididunt voluptate aliquip cillum eu aute duis laboris sit et. Amet enim quis tempor occaecat excepteur exercitation excepteur deserunt amet cillum adipisicing.",
"registered"=>"Monday, May 25, 2015 6:51 AM",
"latitude"=>"16.001127",
"longitude"=>"-72.377848",
"tags"=>["dolore", "nostrud", "occaecat", "cillum", "nisi"],
"range"=>[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
"friends"=>
[{"id"=>0, "name"=>"Alison Bryant"},
{"id"=>1, "name"=>"Ester Espinoza"},
{"id"=>2, "name"=>"Sullivan Kane"}],
"greeting"=>"Hello, Ward! You have 7 unread messages.",
"favoriteFruit"=>"apple"}]
puts "Duration: #{Time.now - start} seconds"
0.47sec -> 0.34sec
start = Time.now
"Exercitation eu ex aliqua sit. Pariatur aliquip incididunt sint id non consectetur ullamco Lorem ea mollit duis amet sint labore. Commodo laborum labore commodo officia in cillum adipisicing esse excepteur cupidatat adipisicing ut. Non esse incididunt voluptate aliquip cillum eu aute duis laboris sit et. Amet enim quis tempor occaecat excepteur exercitation excepteur deserunt amet cillum adipisicing."
puts "Duration: #{Time.now - start} seconds"
0.11sec -> 0.08sec
start = Time.now
def each_top_level_statement
initialize_input
catch(:TERM_INPUT) do
loop do
begin
prompt
unless l = lex
throw :TERM_INPUT if @line == ''
else
@line_no += l.count("\n")
next if l == "\n"
@line.concat l
if @code_block_open or @ltype or @continue or @indent > 0
next
end
end
if @line != "\n"
@line.force_encoding(@io.encoding)
yield @line, @exp_line_no
end
break if @io.eof?
@line = ''
@exp_line_no = @line_no
@indent = 0
rescue TerminateLineInput
initialize_input
prompt
end
end
end
end
puts "Duration: #{Time.now - start} seconds"
0.40sec -> 0.33sec
Co-authored-by: NARUSE, Yui <naruse@airemix.jp>
https://github.com/ruby/reline/commit/a9e39ddcc4
The rendering time in IRB has been reduced as follows:
start = Time.now
[{"_id"=>"5f9072a4589a06d2d74b6028",
"index"=>0,
"guid"=>"6b3051e2-dbc7-4537-bdb9-6cd7bb5358a7",
"isActive"=>true,
"balance"=>"$1,442.84",
"picture"=>"http://placehold.it/32x32",
"age"=>34,
"eyeColor"=>"blue",
"name"=>{"first"=>"Ward", "last"=>"Levy"},
"company"=>"HYPLEX",
"email"=>"ward.levy@hyplex.us",
"phone"=>"+1 (867) 568-3319",
"address"=>"867 Cobek Court, Clara, Maryland, 3254",
"about"=>
"Exercitation eu ex aliqua sit. Pariatur aliquip incididunt sint id non consectetur ullamco Lorem ea mollit duis amet sint labore. Commodo laborum labore commodo officia in cillum adipisicing esse excepteur cupidatat adipisicing ut. Non esse incididunt voluptate aliquip cillum eu aute duis laboris sit et. Amet enim quis tempor occaecat excepteur exercitation excepteur deserunt amet cillum adipisicing.",
"registered"=>"Monday, May 25, 2015 6:51 AM",
"latitude"=>"16.001127",
"longitude"=>"-72.377848",
"tags"=>["dolore", "nostrud", "occaecat", "cillum", "nisi"],
"range"=>[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
"friends"=>
[{"id"=>0, "name"=>"Alison Bryant"},
{"id"=>1, "name"=>"Ester Espinoza"},
{"id"=>2, "name"=>"Sullivan Kane"}],
"greeting"=>"Hello, Ward! You have 7 unread messages.",
"favoriteFruit"=>"apple"}]
puts "Duration: #{Time.now - start} seconds"
0.73sec -> 0.43sec
start = Time.now
"Exercitation eu ex aliqua sit. Pariatur aliquip incididunt sint id non consectetur ullamco Lorem ea mollit duis amet sint labore. Commodo laborum labore commodo officia in cillum adipisicing esse excepteur cupidatat adipisicing ut. Non esse incididunt voluptate aliquip cillum eu aute duis laboris sit et. Amet enim quis tempor occaecat excepteur exercitation excepteur deserunt amet cillum adipisicing."
puts "Duration: #{Time.now - start} seconds"
0.15sec -> 0.10sec
start = Time.now
def each_top_level_statement
initialize_input
catch(:TERM_INPUT) do
loop do
begin
prompt
unless l = lex
throw :TERM_INPUT if @line == ''
else
@line_no += l.count("\n")
next if l == "\n"
@line.concat l
if @code_block_open or @ltype or @continue or @indent > 0
next
end
end
if @line != "\n"
@line.force_encoding(@io.encoding)
yield @line, @exp_line_no
end
break if @io.eof?
@line = ''
@exp_line_no = @line_no
@indent = 0
rescue TerminateLineInput
initialize_input
prompt
end
end
end
end
puts "Duration: #{Time.now - start} seconds"
0.59sec -> 0.38sec
Co-authored-by: NARUSE, Yui <naruse@airemix.jp>
https://github.com/ruby/reline/commit/36bc9e3abb
The rendering time in IRB has been reduced as follows:
start = Time.now
[{"_id"=>"5f9072a4589a06d2d74b6028",
"index"=>0,
"guid"=>"6b3051e2-dbc7-4537-bdb9-6cd7bb5358a7",
"isActive"=>true,
"balance"=>"$1,442.84",
"picture"=>"http://placehold.it/32x32",
"age"=>34,
"eyeColor"=>"blue",
"name"=>{"first"=>"Ward", "last"=>"Levy"},
"company"=>"HYPLEX",
"email"=>"ward.levy@hyplex.us",
"phone"=>"+1 (867) 568-3319",
"address"=>"867 Cobek Court, Clara, Maryland, 3254",
"about"=>
"Exercitation eu ex aliqua sit. Pariatur aliquip incididunt sint id non consectetur ullamco Lorem ea mollit duis amet sint labore. Commodo laborum labore commodo officia in cillum adipisicing esse excepteur cupidatat adipisicing ut. Non esse incididunt voluptate aliquip cillum eu aute duis laboris sit et. Amet enim quis tempor occaecat excepteur exercitation excepteur deserunt amet cillum adipisicing.",
"registered"=>"Monday, May 25, 2015 6:51 AM",
"latitude"=>"16.001127",
"longitude"=>"-72.377848",
"tags"=>["dolore", "nostrud", "occaecat", "cillum", "nisi"],
"range"=>[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
"friends"=>
[{"id"=>0, "name"=>"Alison Bryant"},
{"id"=>1, "name"=>"Ester Espinoza"},
{"id"=>2, "name"=>"Sullivan Kane"}],
"greeting"=>"Hello, Ward! You have 7 unread messages.",
"favoriteFruit"=>"apple"}]
puts "Duration: #{Time.now - start} seconds"
2.17sec -> 0.92sec
start = Time.now
"Exercitation eu ex aliqua sit. Pariatur aliquip incididunt sint id non consectetur ullamco Lorem ea mollit duis amet sint labore. Commodo laborum labore commodo officia in cillum adipisicing esse excepteur cupidatat adipisicing ut. Non esse incididunt voluptate aliquip cillum eu aute duis laboris sit et. Amet enim quis tempor occaecat excepteur exercitation excepteur deserunt amet cillum adipisicing."
puts "Duration: #{Time.now - start} seconds"
1.57sec -> 0.22sec
start = Time.now
def each_top_level_statement
initialize_input
catch(:TERM_INPUT) do
loop do
begin
prompt
unless l = lex
throw :TERM_INPUT if @line == ''
else
@line_no += l.count("\n")
next if l == "\n"
@line.concat l
if @code_block_open or @ltype or @continue or @indent > 0
next
end
end
if @line != "\n"
@line.force_encoding(@io.encoding)
yield @line, @exp_line_no
end
break if @io.eof?
@line = ''
@exp_line_no = @line_no
@indent = 0
rescue TerminateLineInput
initialize_input
prompt
end
end
end
end
puts "Duration: #{Time.now - start} seconds"
0.88sec -> 0.77sec
https://github.com/ruby/reline/commit/7d87ac5a12
IRB uses Reline's 3 dynamic real-time callbacks with calling Ripper;
output_modifier_proc, prompt_proc, and auto_indent_proc. These processing
times make the paste time too long.
https://github.com/ruby/reline/commit/beec3399a8
Previously, if creating the directory directly didn't work
and the directory didn't exist, mkdir_p would create all
directories from the root. This modifies the approach to
check whether the directory exists when walking up the
directory tree from the argument, and once you have found an
intermediate directory that exists, you only need to create
directories under it.
This approach has a couple advantages:
1) It performs better when most directories in path already exist,
and that will be true for most usage of mkdir_p, as mkdir_p is
usually called with paths where the first few directories exist
and only the last directory or last few directories do not.
2) It works in file-system access limited environments such as
when unveil(2) is used on OpenBSD. In these environments, if
/foo/bar/baz exists and is unveiled, you can do
`mkdir /foo/bar/baz/xyz` but `mkdir /foo` and `mkdir /foo/bar` raise
Errno::ENOENT.
https://github.com/ruby/fileutils/commit/ec0c229e78
It required RBTree to perform decently and the external dependency was
not suitable for a standard library. The pure ruby fallback
implementation was originally meant to be an example of how to write a
subclass of Set, and its poor performance was not suitable for use in
production.
I decided it should be distributed as an external library instead of
bundling it with Set.
https://github.com/ruby/set/commit/dfcc8e568b