1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
|
require 'optparse'
require 'set'
# Number of iterations to test
num_iters = 10_000
# Parse the command-line options
OptionParser.new do |opts|
opts.on("--num-iters=N") do |n|
num_iters = n.to_i
end
end.parse!
# Format large numbers with comma separators for readability
def format_number(pad, number)
s = number.to_s
i = s.index('.') || s.size
s.insert(i -= 3, ',') while i > 3
s.rjust(pad, ' ')
end
# Wrap an integer to pass as argument
# We use this so we can have some object arguments
class IntWrapper
def initialize(v)
# Force the object to have a random shape
if rand() < 50
@v0 = 1
end
if rand() < 50
@v1 = 1
end
if rand() < 50
@v2 = 1
end
if rand() < 50
@v3 = 1
end
if rand() < 50
@v4 = 1
end
if rand() < 50
@v5 = 1
end
if rand() < 50
@v6 = 1
end
@value = v
end
attr_reader :value
end
# Generate a random argument value, integer or string or object
def sample_arg()
c = ['int', 'string', 'object'].sample()
if c == 'int'
return rand(0...100)
end
if c == 'string'
return 'f' * rand(0...100)
end
if c == 'object'
return IntWrapper.new(rand(0...100))
end
raise "should not get here"
end
# Evaluate the value of an argument with respect to the checksum
def arg_val(arg)
if arg.kind_of? Integer
return arg
end
if arg.kind_of? String
return arg.length
end
if arg.kind_of? Object
return arg.value
end
raise "unknown arg type"
end
# List of parameters/arguments for a method
class ParamList
def initialize()
self.sample_params()
self.sample_args()
end
# Sample/generate a random set of parameters for a method
def sample_params()
# Choose how many positional arguments to use, and how many are optional
num_pargs = rand(10)
@opt_parg_idx = rand(num_pargs)
@num_opt_pargs = rand(num_pargs + 1 - @opt_parg_idx)
@num_pargs_req = num_pargs - @num_opt_pargs
@pargs = (0...num_pargs).map do |i|
{
:name => "p#{i}",
:optional => (i >= @opt_parg_idx && i < @opt_parg_idx + @num_opt_pargs)
}
end
# Choose how many kwargs to use, and how many are optional
num_kwargs = rand(10)
@kwargs = (0...num_kwargs).map do |i|
{
:name => "k#{i}",
:optional => rand() < 0.5
}
end
# Choose whether to have rest parameters or not
@has_rest = @num_opt_pargs == 0 && rand() < 0.5
@has_kwrest = rand() < 0.25
# Choose whether to have a named block parameter or not
@has_block_param = rand() < 0.25
end
# Sample/generate a random set of arguments corresponding to the parameters
def sample_args()
# Choose how many positional args to pass
num_pargs_passed = rand(@num_pargs_req..@pargs.size)
# How many optional arguments will be filled
opt_pargs_filled = num_pargs_passed - @num_pargs_req
@pargs.each_with_index do |parg, i|
if parg[:optional]
parg[:default] = rand(100)
end
if !parg[:optional] || i < @opt_parg_idx + opt_pargs_filled
parg[:argval] = rand(100)
end
end
@kwargs.each_with_index do |kwarg, i|
if kwarg[:optional]
kwarg[:default] = rand(100)
end
if !kwarg[:optional] || rand() < 0.5
kwarg[:argval] = rand(100)
end
end
# Randomly pass a block or not
@block_arg = nil
if rand() < 0.5
@block_arg = rand(100)
end
end
# Compute the expected checksum of arguments ahead of time
def compute_checksum()
checksum = 0
@pargs.each_with_index do |arg, i|
value = (arg.key? :argval)? arg[:argval]:arg[:default]
checksum += (i+1) * arg_val(value)
end
@kwargs.each_with_index do |arg, i|
value = (arg.key? :argval)? arg[:argval]:arg[:default]
checksum += (i+1) * arg_val(value)
end
if @block_arg
if @has_block_param
checksum += arg_val(@block_arg)
end
checksum += arg_val(@block_arg)
end
checksum
end
# Generate code for the method signature and method body
def gen_method_str()
m_str = "def m("
@pargs.each do |arg|
if !m_str.end_with?("(")
m_str += ", "
end
m_str += arg[:name]
# If this has a default value
if arg[:optional]
m_str += " = #{arg[:default]}"
end
end
if @has_rest
if !m_str.end_with?("(")
m_str += ", "
end
m_str += "*rest"
end
@kwargs.each do |arg|
if !m_str.end_with?("(")
m_str += ", "
end
m_str += "#{arg[:name]}:"
# If this has a default value
if arg[:optional]
m_str += " #{arg[:default]}"
end
end
if @has_kwrest
if !m_str.end_with?("(")
m_str += ", "
end
m_str += "**kwrest"
end
if @has_block_param
if !m_str.end_with?("(")
m_str += ", "
end
m_str += "&block"
end
m_str += ")\n"
# Add some useless locals
rand(0...16).times do |i|
m_str += "local#{i} = #{i}\n"
end
# Add some useless if statements
@pargs.each_with_index do |arg, i|
if rand() < 50
m_str += "if #{arg[:name]} > 4; end\n"
end
end
m_str += "checksum = 0\n"
@pargs.each_with_index do |arg, i|
m_str += "checksum += #{i+1} * arg_val(#{arg[:name]})\n"
end
@kwargs.each_with_index do |arg, i|
m_str += "checksum += #{i+1} * arg_val(#{arg[:name]})\n"
end
if @has_block_param
m_str += "if block; r = block.call; checksum += arg_val(r); end\n"
end
m_str += "if block_given?; r = yield; checksum += arg_val(r); end\n"
if @has_rest
m_str += "raise 'rest is not array' unless rest.kind_of?(Array)\n"
m_str += "raise 'rest size not integer' unless rest.size.kind_of?(Integer)\n"
end
if @has_kwrest
m_str += "raise 'kwrest is not a hash' unless kwrest.kind_of?(Hash)\n"
m_str += "raise 'kwrest size not integer' unless kwrest.size.kind_of?(Integer)\n"
end
m_str += "checksum\n"
m_str += "end"
m_str
end
# Generate code to call into the method and pass the arguments
def gen_call_str()
c_str = "m("
@pargs.each_with_index do |arg, i|
if !arg.key? :argval
next
end
if !c_str.end_with?("(")
c_str += ", "
end
c_str += "#{arg[:argval]}"
end
@kwargs.each_with_index do |arg, i|
if !arg.key? :argval
next
end
if !c_str.end_with?("(")
c_str += ", "
end
c_str += "#{arg[:name]}: #{arg[:argval]}"
end
c_str += ")"
# Randomly pass a block or not
if @block_arg
c_str += " { #{@block_arg} }"
end
c_str
end
end
iseqs_compiled_start = RubyVM::YJIT.runtime_stats[:compiled_iseq_entry]
start_time = Time.now.to_f
num_iters.times do |i|
puts "Iteration #{i}"
lst = ParamList.new()
m_str = lst.gen_method_str()
c_str = lst.gen_call_str()
checksum = lst.compute_checksum()
f = Object.new
# Define the method on f
puts "Defining"
p m_str
f.instance_eval(m_str)
#puts RubyVM::InstructionSequence.disasm(f.method(:m))
#exit 0
puts "Calling"
c_str = "f.#{c_str}"
p c_str
r = eval(c_str)
puts "checksum=#{r}"
if r != checksum
raise "return value #{r} doesn't match checksum #{checksum}"
end
puts ""
end
# Make sure that YJIT actually compiled the tests we ran
# Should be run with --yjit-call-threshold=1
iseqs_compiled_end = RubyVM::YJIT.runtime_stats[:compiled_iseq_entry]
if iseqs_compiled_end - iseqs_compiled_start < num_iters
raise "YJIT did not compile enough ISEQs"
end
puts "Code region size: #{ format_number(0, RubyVM::YJIT.runtime_stats[:code_region_size]) }"
end_time = Time.now.to_f
itrs_per_sec = num_iters / (end_time - start_time)
itrs_per_hour = 3600 * itrs_per_sec
puts "#{'%.1f' % itrs_per_sec} iterations/s"
puts "#{format_number(0, itrs_per_hour.round)} iterations/hour"
|