wardite 1.2.0 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/benchmark/README.md +8 -0
  3. data/benchmark/compare_mir_generation_builds.rb +66 -0
  4. data/benchmark/generate_mir_generation_fixtures.rb +44 -0
  5. data/benchmark/measure_mir_generation.rb +52 -0
  6. data/benchmark/mir_copy_worker.rb +29 -0
  7. data/benchmark/mir_generation_worker.rb +50 -0
  8. data/benchmark/prepare_mir_generation_profile.rb +80 -0
  9. data/benchmark/profile_inline_snapshots.rb +63 -0
  10. data/benchmark/profile_mir_generation_allocations.rb +73 -0
  11. data/benchmark/results/mir-generation-adr0014.json +1779 -0
  12. data/benchmark/results/mir-generation-adr0015.json +3132 -0
  13. data/benchmark/results/mir-generation-allocations.json +236 -0
  14. data/benchmark/results/mir-generation-coarse.json +909 -0
  15. data/benchmark/results/mir-generation-control.json +463 -0
  16. data/benchmark/results/mir-generation-detail.json +387 -0
  17. data/benchmark/results/mir-generation-scaling.json +2945 -0
  18. data/docs/adr/0014-efflorescence-mir-build-validation-and-decoding.md +136 -0
  19. data/docs/adr/0015-efflorescence-selective-inline-snapshots.md +150 -0
  20. data/docs/adr/0016-efflorescence-generation-tagged-copy-propagation.md +163 -0
  21. data/docs/efflorescence-mir-build-validation-and-decoding-results.md +93 -0
  22. data/docs/efflorescence-mir-generation-implementation.md +77 -0
  23. data/docs/efflorescence-mir-generation-performance.md +184 -0
  24. data/docs/efflorescence-selective-inline-snapshots-results.md +92 -0
  25. data/lib/wardite/efflorescence/ir.rb +119 -37
  26. data/lib/wardite/efflorescence/optimizer.rb +65 -15
  27. data/lib/wardite/efflorescence/register_validation.rb +162 -32
  28. data/lib/wardite/instruction.rb +42 -1
  29. data/lib/wardite/leb128.rb +2 -5
  30. data/lib/wardite/load.rb +151 -73
  31. data/lib/wardite/version.rb +1 -1
  32. data/sig/generated/wardite/efflorescence/ir.rbs +29 -1
  33. data/sig/generated/wardite/efflorescence/optimizer.rbs +27 -3
  34. data/sig/generated/wardite/efflorescence/register_validation.rbs +45 -4
  35. data/sig/generated/wardite/instruction.rbs +23 -0
  36. data/sig/generated/wardite/load.rbs +61 -8
  37. metadata +23 -1
@@ -142,8 +142,11 @@ module Wardite
142
142
  @reads = Array.new(function.register_count, 0)
143
143
  @writes = Array.new(function.register_count, 0)
144
144
  function.instructions.each_with_index do |instruction, pc|
145
- function.reads(instruction).each do |register|
146
- @reads[register] += 1
145
+ read_count = function.read_count(instruction)
146
+ ordinal = 0
147
+ while ordinal < read_count
148
+ @reads[function.read_at(instruction, ordinal)] += 1
149
+ ordinal += 1
147
150
  end
148
151
  destination = Opcode.destination(instruction)
149
152
  @writes[destination] += 1 if destination >= 0
@@ -178,7 +181,7 @@ module Wardite
178
181
  end
179
182
 
180
183
  module Passes
181
- # Expand only small, straight-line leaves from a snapshot of the IR.
184
+ # Expand only small, straight-line leaves from pre-rewrite snapshots.
182
185
  # One round avoids transitive/exponential expansion and keeps recursion
183
186
  # on the ordinary CALL path. Existing local passes clean up the copies.
184
187
  class Inlining
@@ -190,12 +193,15 @@ module Wardite
190
193
  # @rbs ir: Wardite::Efflorescence::IR
191
194
  # @rbs return: Array[Integer]
192
195
  def run(ir)
193
- callees = ir.copy
196
+ callees = {} #: Hash[Integer, Wardite::Efflorescence::Function]
197
+ ir.functions.each do |function|
198
+ callees[function.index] = function.copy if eligible?(function)
199
+ end
194
200
  changed = [] #: Array[Integer]
195
201
  index = 0
196
202
  while index < ir.functions.size
197
203
  function = ir.defined_at(index)
198
- changed << function.index if !function.uses_i64 && !function.uses_float && !function.uses_eh && !function.uses_store && !function.uses_dynamic_calls && expand_calls(function, callees)
204
+ changed << function.index if !function.uses_i64 && !function.uses_float && !function.uses_eh && !function.uses_store && !function.uses_dynamic_calls && expand_calls(function, ir, callees)
199
205
  index += 1
200
206
  end
201
207
  changed
@@ -224,23 +230,22 @@ module Wardite
224
230
  end
225
231
 
226
232
  # @rbs function: Wardite::Efflorescence::Function
227
- # @rbs callees: Wardite::Efflorescence::IR
233
+ # @rbs ir: Wardite::Efflorescence::IR
234
+ # @rbs callees: Hash[Integer, Wardite::Efflorescence::Function]
228
235
  # @rbs return: bool
229
- def expand_calls(function, callees)
236
+ def expand_calls(function, ir, callees)
237
+ return false unless expandable_call?(function, ir, callees)
238
+
230
239
  rewrite = Rewrite.new(function)
231
240
  added_instructions = 0
232
241
  added_registers = 0
233
242
  changed = false
234
243
  function.instructions.each_with_index do |instruction, pc|
235
244
  rewrite.mark(pc)
236
- if instruction[0] == Opcode::CALL
237
- callee = callees.function_at(instruction[2])
238
- # Count the expansion before copy cleanup, conservatively.
239
- growth = callee.parameter_count + callee.local_count + callee.instructions.size - 2
240
- growth += 1 if instruction[1] >= 0
241
- growth = 0 if growth < 0
242
- registers = callee.register_count + (callee.local_count > 0 ? 1 : 0)
243
- if eligible?(callee) && added_instructions + growth <= MAX_ADDED_INSTRUCTIONS &&
245
+ if instruction[0] == Opcode::CALL && (callee = callee_for(ir, callees, instruction))
246
+ growth = instruction_growth(callee, instruction)
247
+ registers = register_growth(callee)
248
+ if added_instructions + growth <= MAX_ADDED_INSTRUCTIONS &&
244
249
  added_registers + registers <= MAX_ADDED_REGISTERS
245
250
  expand(function, callee, instruction, rewrite)
246
251
  added_instructions += growth
@@ -255,6 +260,51 @@ module Wardite
255
260
  changed
256
261
  end
257
262
 
263
+ # @rbs function: Wardite::Efflorescence::Function
264
+ # @rbs ir: Wardite::Efflorescence::IR
265
+ # @rbs callees: Hash[Integer, Wardite::Efflorescence::Function]
266
+ # @rbs return: bool
267
+ def expandable_call?(function, ir, callees)
268
+ instructions = function.instructions
269
+ pc = 0
270
+ while pc < instructions.size
271
+ instruction = instructions.fetch(pc)
272
+ if instruction[0] == Opcode::CALL && (callee = callee_for(ir, callees, instruction))
273
+ return true if instruction_growth(callee, instruction) <= MAX_ADDED_INSTRUCTIONS &&
274
+ register_growth(callee) <= MAX_ADDED_REGISTERS
275
+ end
276
+ pc += 1
277
+ end
278
+ false
279
+ end
280
+
281
+ # Count the expansion before copy cleanup, conservatively.
282
+ # @rbs callee: Wardite::Efflorescence::Function
283
+ # @rbs call: Wardite::Efflorescence::Instruction
284
+ # @rbs return: Integer
285
+ def instruction_growth(callee, call)
286
+ growth = callee.parameter_count + callee.local_count + callee.instructions.size - 2
287
+ growth += 1 if call[1] >= 0
288
+ growth < 0 ? 0 : growth
289
+ end
290
+
291
+ # @rbs callee: Wardite::Efflorescence::Function
292
+ # @rbs return: Integer
293
+ def register_growth(callee)
294
+ callee.register_count + (callee.local_count > 0 ? 1 : 0)
295
+ end
296
+
297
+ # Check the module index even when there are no inline candidates.
298
+ # @rbs ir: Wardite::Efflorescence::IR
299
+ # @rbs callees: Hash[Integer, Wardite::Efflorescence::Function]
300
+ # @rbs call: Wardite::Efflorescence::Instruction
301
+ # @rbs return: Wardite::Efflorescence::Function?
302
+ def callee_for(ir, callees, call)
303
+ index = call[2]
304
+ ir.function_at(index)
305
+ callees[index]
306
+ end
307
+
258
308
  # @rbs caller: Wardite::Efflorescence::Function
259
309
  # @rbs callee: Wardite::Efflorescence::Function
260
310
  # @rbs call: Wardite::Efflorescence::Instruction
@@ -67,22 +67,103 @@ module Wardite
67
67
  instructions = function.instructions
68
68
  raise TranslationError, "Efflorescence: empty MIR function" if instructions.empty?
69
69
  validate_types(function, ir, tags)
70
+ if !function.uses_eh && function.handlers.empty? && function.throw_sites.empty? &&
71
+ !function.register_types.include?(:exnref)
72
+ leaders = plain_leaders(instructions)
73
+ if leaders
74
+ validate_plain(function, leaders)
75
+ return
76
+ end
77
+ end
78
+
70
79
  analysis = Analysis.new(function)
71
80
  flow = analysis.flow
72
- register_count = function.register_count
81
+ blocks, block_for_pc = build_blocks(analysis.leaders)
82
+ order = reverse_postorder(blocks, block_for_pc, flow)
83
+ validate_blocks(function, blocks, order, block_for_pc, flow)
84
+ end
85
+
86
+ private
87
+
88
+ # @rbs instructions: Array[Wardite::Efflorescence::Instruction]
89
+ # @rbs return: Array[Integer]?
90
+ def plain_leaders(instructions)
91
+ count = instructions.size
92
+ leaders = Array.new(count, 0)
93
+ leaders[0] = 1
94
+ pc = 0
95
+ while pc < count
96
+ instruction = instructions.fetch(pc)
97
+ opcode = instruction[0]
98
+ case opcode
99
+ when Opcode::EH_ENTER, Opcode::EH_TRIM, Opcode::THROW, Opcode::THROW_REF, Opcode::COPY_REF
100
+ return nil
101
+ end
102
+ position = Opcode.target_position(instruction)
103
+ if position >= 0
104
+ target = instruction[position]
105
+ raise TranslationError, "invalid MIR branch target #{target}" unless target >= 0 && target < count
106
+ leaders[target] = 1
107
+ end
108
+ pc += 1
109
+ leaders[pc] = 1 if pc < count && (position >= 0 || Opcode.terminal?(opcode))
110
+ end
111
+ leaders
112
+ end
113
+
114
+ # @rbs leaders: Array[Integer]
115
+ # @rbs return: [Array[Wardite::Efflorescence::RegisterValidation::Block], Array[Integer]]
116
+ def build_blocks(leaders)
73
117
  blocks = [] #: Array[Wardite::Efflorescence::RegisterValidation::Block]
74
- block_for_pc = [] #: Array[Integer]
118
+ block_for_pc = Array.new(leaders.size, 0)
75
119
  first = 0
76
- instructions.size.times do |pc|
77
- if pc > 0 && analysis.leaders[pc] == 1
120
+ pc = 0
121
+ while pc < leaders.size
122
+ if pc > 0 && leaders.fetch(pc) == 1
78
123
  blocks << Block.new(first, pc)
79
124
  first = pc
80
125
  end
81
- block_for_pc << blocks.size
126
+ block_for_pc[pc] = blocks.size
127
+ pc += 1
82
128
  end
83
- blocks << Block.new(first, instructions.size)
84
- order = reverse_postorder(blocks, block_for_pc, flow)
129
+ blocks << Block.new(first, leaders.size)
130
+ [blocks, block_for_pc]
131
+ end
132
+
133
+ # @rbs function: Wardite::Efflorescence::Function
134
+ # @rbs leaders: Array[Integer]
135
+ # @rbs return: void
136
+ def validate_plain(function, leaders)
137
+ instructions = function.instructions
138
+ blocks, block_for_pc = build_blocks(leaders)
139
+ blocks.each do |block|
140
+ instruction = instructions.fetch(block.limit - 1)
141
+ position = Opcode.target_position(instruction)
142
+ block.successors << block_for_pc.fetch(instruction[position]) if position >= 0
143
+ if block.limit < instructions.size && !Opcode.terminal?(instruction[0])
144
+ block.successors << block_for_pc.fetch(block.limit)
145
+ end
146
+ end
147
+
148
+ order = block_reverse_postorder(blocks)
149
+ order.each do |index|
150
+ block = blocks.fetch(index)
151
+ if block.successors.empty? && !Opcode.terminal?(instructions.fetch(block.limit - 1)[0])
152
+ raise TranslationError, "MIR falls through without RETURN"
153
+ end
154
+ end
155
+ validate_blocks(function, blocks, order, block_for_pc, nil)
156
+ end
85
157
 
158
+ # @rbs function: Wardite::Efflorescence::Function
159
+ # @rbs blocks: Array[Wardite::Efflorescence::RegisterValidation::Block]
160
+ # @rbs order: Array[Integer]
161
+ # @rbs block_for_pc: Array[Integer]
162
+ # @rbs flow: Wardite::Efflorescence::FlowGraph?
163
+ # @rbs return: void
164
+ def validate_blocks(function, blocks, order, block_for_pc, flow)
165
+ instructions = function.instructions
166
+ register_count = function.register_count
86
167
  word_count = (register_count + WORD_MASK) >> WORD_SHIFT
87
168
  constants = Array.new(word_count, 0)
88
169
  defined = Array.new(word_count, 0)
@@ -105,17 +186,23 @@ module Wardite
105
186
  pc = block.first
106
187
  while pc < block.limit
107
188
  instruction = instructions.fetch(pc)
108
- function.reads(instruction).each do |register|
189
+ read_count = function.read_count(instruction)
190
+ ordinal = 0
191
+ while ordinal < read_count
192
+ register = function.read_at(instruction, ordinal)
109
193
  unless register >= 0 && register < register_count && marked?(defined, register)
110
194
  raise TranslationError, "Efflorescence: function #{function.index}, MIR PC #{pc}: r#{register} read before definition"
111
195
  end
196
+ ordinal += 1
112
197
  end
113
198
  # A CALL result exists only on its normal successor.
114
- flow.exceptional.fetch(pc).each do |clause|
115
- edge_defined = defined.dup
116
- clause.destinations.each { |register| mark(edge_defined, register) }
117
- target = block_for_pc.fetch(clause.target_pc)
118
- pending += 1 if blocks.fetch(target).merge(edge_defined)
199
+ if flow
200
+ flow.exceptional.fetch(pc).each do |clause|
201
+ edge_defined = defined.dup
202
+ clause.destinations.each { |register| mark(edge_defined, register) }
203
+ target = block_for_pc.fetch(clause.target_pc)
204
+ pending += 1 if blocks.fetch(target).merge(edge_defined)
205
+ end
119
206
  end
120
207
  destination = Opcode.destination(instruction)
121
208
  if destination >= 0
@@ -126,17 +213,20 @@ module Wardite
126
213
  end
127
214
  pc += 1
128
215
  end
129
- flow.normal.fetch(block.limit - 1).each do |target_pc|
130
- target = block_for_pc.fetch(target_pc)
131
- pending += 1 if blocks.fetch(target).merge(defined)
216
+ if flow
217
+ flow.normal.fetch(block.limit - 1).each do |target_pc|
218
+ target = block_for_pc.fetch(target_pc)
219
+ pending += 1 if blocks.fetch(target).merge(defined)
220
+ end
221
+ else
222
+ block.successors.each do |target|
223
+ pending += 1 if blocks.fetch(target).merge(defined)
224
+ end
132
225
  end
133
226
  end
134
227
  end
135
228
  end
136
- private
137
229
 
138
- # Visit predecessors before joins where possible, then revisit changed
139
- # states across backedges until the worklist is empty.
140
230
  # @rbs blocks: Array[Wardite::Efflorescence::RegisterValidation::Block]
141
231
  # @rbs block_for_pc: Array[Integer]
142
232
  # @rbs flow: Wardite::Efflorescence::FlowGraph
@@ -149,6 +239,14 @@ module Wardite
149
239
  end
150
240
  end
151
241
 
242
+ block_reverse_postorder(blocks)
243
+ end
244
+
245
+ # Visit predecessors before joins where possible, then revisit changed
246
+ # states across backedges until the worklist is empty.
247
+ # @rbs blocks: Array[Wardite::Efflorescence::RegisterValidation::Block]
248
+ # @rbs return: Array[Integer]
249
+ def block_reverse_postorder(blocks)
152
250
  seen = Array.new(blocks.size, false)
153
251
  next_edge = Array.new(blocks.size, 0)
154
252
  seen[0] = true
@@ -231,22 +329,26 @@ module Wardite
231
329
  raise TranslationError, "MIR host call signature mismatch"
232
330
  end
233
331
  end
234
- check_register(function, instruction[3], :i32) if opcode == Opcode::CALL_INDIRECT
235
- site.arguments.each_with_index { |register, i| check_register(function, register, site.parameter_types.fetch(i)) }
332
+ ordinal = 0
333
+ if opcode == Opcode::CALL_INDIRECT
334
+ check_register(function, function.read_at(instruction, ordinal), :i32)
335
+ ordinal += 1
336
+ end
337
+ check_reads(function, instruction, site.parameter_types, ordinal, function.read_count(instruction))
236
338
  check_result(function, destination, site.result_types)
237
339
  next
238
340
  end
239
- reads = function.reads(instruction)
341
+ read_count = function.read_count(instruction)
240
342
  destination = Opcode.destination(instruction)
241
343
  if opcode > Opcode::CALL_INDIRECT
242
- validate_extended_instruction(function, instruction, reads, destination)
344
+ validate_extended_instruction(function, instruction, read_count, destination)
243
345
  next
244
346
  end
245
347
  case opcode
246
348
  when Opcode::THROW
247
349
  site = function.throw_sites.fetch(instruction[1])
248
- types = payload_types(tags, site.tag_index, reads.size)
249
- reads.each_with_index { |register, i| check_register(function, register, types.fetch(i)) }
350
+ types = payload_types(tags, site.tag_index, read_count)
351
+ check_reads(function, instruction, types, 0, read_count)
250
352
  when Opcode::COPY
251
353
  type = function.register_types[destination] if destination >= 0
252
354
  unless type && FunctionEntry.numeric_type?(type)
@@ -258,18 +360,18 @@ module Wardite
258
360
  when Opcode::CALL
259
361
  if ir
260
362
  entry = ir.entry_at(instruction[2])
261
- unless entry.kind == :defined && reads.size == entry.parameter_types.size
363
+ unless entry.kind == :defined && read_count == entry.parameter_types.size
262
364
  raise TranslationError, "invalid MIR direct call signature"
263
365
  end
264
- reads.each_with_index { |register, i| check_register(function, register, entry.parameter_types.fetch(i)) }
366
+ check_reads(function, instruction, entry.parameter_types, 0, read_count)
265
367
  check_result(function, destination, entry.result_types)
266
368
  else
267
- reads.each { |register| check_register(function, register, :i32) }
369
+ check_reads_of_type(function, instruction, :i32, read_count)
268
370
  check_register(function, destination, :i32) if destination >= 0
269
371
  end
270
372
  else
271
373
  expected = opcode == Opcode::COPY_REF || opcode == Opcode::THROW_REF ? :exnref : :i32
272
- reads.each { |register| check_register(function, register, expected) }
374
+ check_reads_of_type(function, instruction, expected, read_count)
273
375
  check_register(function, destination, expected) if destination >= 0
274
376
  end
275
377
  end
@@ -286,15 +388,15 @@ module Wardite
286
388
 
287
389
  # @rbs function: Wardite::Efflorescence::Function
288
390
  # @rbs instruction: Wardite::Efflorescence::Instruction
289
- # @rbs reads: Array[Integer]
391
+ # @rbs read_count: Integer
290
392
  # @rbs destination: Integer
291
393
  # @rbs return: void
292
- def validate_extended_instruction(function, instruction, reads, destination)
394
+ def validate_extended_instruction(function, instruction, read_count, destination)
293
395
  opcode = instruction[0]
294
396
  types = Opcode.extended_alu_types(opcode)
295
397
  if types
296
398
  check_register(function, destination, types.fetch(0))
297
- reads.each_with_index { |register, i| check_register(function, register, types.fetch(i + 1)) }
399
+ check_reads(function, instruction, types, 0, read_count, 1)
298
400
  elsif (type = Opcode::EXTENDED_LOAD_TYPES[opcode])
299
401
  check_register(function, destination, type)
300
402
  check_register(function, instruction[2], :i32)
@@ -310,6 +412,34 @@ module Wardite
310
412
  end
311
413
  end
312
414
 
415
+ # @rbs function: Wardite::Efflorescence::Function
416
+ # @rbs instruction: Wardite::Efflorescence::Instruction
417
+ # @rbs types: Array[Symbol]
418
+ # @rbs first: Integer
419
+ # @rbs limit: Integer
420
+ # @rbs type_offset: Integer
421
+ # @rbs return: void
422
+ def check_reads(function, instruction, types, first, limit, type_offset = 0)
423
+ ordinal = first
424
+ while ordinal < limit
425
+ check_register(function, function.read_at(instruction, ordinal), types.fetch(ordinal - first + type_offset))
426
+ ordinal += 1
427
+ end
428
+ end
429
+
430
+ # @rbs function: Wardite::Efflorescence::Function
431
+ # @rbs instruction: Wardite::Efflorescence::Instruction
432
+ # @rbs type: Symbol
433
+ # @rbs limit: Integer
434
+ # @rbs return: void
435
+ def check_reads_of_type(function, instruction, type, limit)
436
+ ordinal = 0
437
+ while ordinal < limit
438
+ check_register(function, function.read_at(instruction, ordinal), type)
439
+ ordinal += 1
440
+ end
441
+ end
442
+
313
443
  # @rbs tags: Array[Wardite::Efflorescence::Tag]?
314
444
  # @rbs index: Integer
315
445
  # @rbs size: Integer
@@ -522,7 +522,7 @@ module Wardite
522
522
 
523
523
  # @rbs code: Symbol
524
524
  # @rbs return: Array[Symbol]
525
- def self.operand_of(code)
525
+ def self.build_operand_types(code)
526
526
  case code
527
527
  when :local_get, :local_set, :local_tee, :global_get, :global_set, :call, :br, :br_if, :throw, :data_drop
528
528
  [:u32]
@@ -555,6 +555,47 @@ module Wardite
555
555
  end
556
556
  end
557
557
 
558
+ private_class_method :build_operand_types
559
+
560
+ class DecodeSpec
561
+ attr_reader :namespace #: Symbol
562
+ attr_reader :code #: Symbol
563
+ attr_reader :operand_types #: Array[Symbol]
564
+
565
+ # @rbs namespace: Symbol
566
+ # @rbs code: Symbol
567
+ # @rbs operand_types: Array[Symbol]
568
+ def initialize(namespace, code, operand_types)
569
+ @namespace = namespace
570
+ @code = code
571
+ @operand_types = operand_types.freeze
572
+ freeze
573
+ end
574
+ end
575
+
576
+ OPERAND_TYPES_BY_CODE = {} #: Hash[Symbol, Array[Symbol]]
577
+ OPCODE_SPECS = NAMESPACES.each_with_index.map do |namespace, opcode|
578
+ code = SYMS.fetch(opcode)
579
+ types = build_operand_types(code).freeze
580
+ OPERAND_TYPES_BY_CODE[code] = types
581
+ DecodeSpec.new(namespace, code, types)
582
+ end.freeze #: Array[DecodeSpec]
583
+
584
+ FC_SPECS = FC_SYMS.each_with_index.map do |code, opcode|
585
+ types = build_operand_types(code).freeze
586
+ OPERAND_TYPES_BY_CODE[code] = types
587
+ DecodeSpec.new(FC_OPERANDS[opcode] || :default, code, types)
588
+ end.freeze #: Array[DecodeSpec]
589
+ OPERAND_TYPES_BY_CODE.freeze
590
+ private_constant :OPERAND_TYPES_BY_CODE
591
+
592
+ # @rbs code: Symbol
593
+ # @rbs return: Array[Symbol]
594
+ def self.operand_of(code)
595
+ types = OPERAND_TYPES_BY_CODE[code]
596
+ types ? types.dup : []
597
+ end
598
+
558
599
  # @see https://www.w3.org/TR/wasm-core-1/#value-types%E2%91%A2
559
600
  # We use this for reftype conversion. https://webassembly.github.io/spec/core/binary/types.html#binary-reftype
560
601
  # @rbs code: Integer
@@ -7,9 +7,7 @@ module Wardite
7
7
  def fetch_uleb128(buf, max_level: 4)
8
8
  dest = 0
9
9
  level = 0
10
- while b = buf.read(1)
11
- raise LoadError, "buffer too short" unless b
12
- c = b.ord
10
+ while c = buf.getbyte
13
11
  return c if c < 0x80 && level.zero?
14
12
 
15
13
  upper, lower = (c >> 7), (c & (1 << 7) - 1)
@@ -39,8 +37,7 @@ module Wardite
39
37
  negative = false
40
38
  level = 0
41
39
  terminated = false
42
- while b = buf.read(1)
43
- c = b.ord
40
+ while c = buf.getbyte
44
41
 
45
42
  upper, lower = (c >> 7), (c & (1 << 7) - 1)
46
43
  bytes << lower