Skip to content

Commit 8d93879

Browse files
committed
refactor: Optimize the record_variables() function, by removing useless operations
This function is called on every line, while the source code is being traced, and is, therefore, performance critical. The function did perform several useless operations: 1) returning an unused result: Vec<FullValueRecord> 2) useless cloning of each ValueRecord 3) an extra call to ensure_variable_id() after the call to register_variable_with_full_value, which has already called ensure_variable_id() internally. This results in an extra useless hashtable lookup. Sorry, if I overused the word 'useless'.
1 parent 2893f52 commit 8d93879

File tree

1 file changed

+3
-10
lines changed
  • gems/codetracer-ruby-recorder/ext/native_tracer/src

1 file changed

+3
-10
lines changed

gems/codetracer-ruby-recorder/ext/native_tracer/src/lib.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -523,13 +523,12 @@ unsafe fn to_value(recorder: &mut Recorder, val: VALUE, depth: usize) -> ValueRe
523523
ValueRecord::Raw { r: text, type_id }
524524
}
525525

526-
unsafe fn record_variables(recorder: &mut Recorder, binding: VALUE) -> Vec<FullValueRecord> {
526+
unsafe fn record_variables(recorder: &mut Recorder, binding: VALUE) {
527527
let vars = rb_funcall(binding, recorder.id.local_variables, 0);
528528
if !RB_TYPE_P(vars, rb_sys::ruby_value_type::RUBY_T_ARRAY) {
529-
return Vec::new();
529+
return;
530530
}
531531
let len = RARRAY_LEN(vars) as usize;
532-
let mut result = Vec::with_capacity(len);
533532
let ptr = RARRAY_CONST_PTR(vars);
534533
for i in 0..len {
535534
let sym = *ptr.add(i);
@@ -540,15 +539,9 @@ unsafe fn record_variables(recorder: &mut Recorder, binding: VALUE) -> Vec<FullV
540539
TraceWriter::register_variable_with_full_value(
541540
&mut *recorder.tracer,
542541
name,
543-
val_rec.clone(),
542+
val_rec,
544543
);
545-
let var_id = TraceWriter::ensure_variable_id(&mut *recorder.tracer, name);
546-
result.push(FullValueRecord {
547-
variable_id: var_id,
548-
value: val_rec,
549-
});
550544
}
551-
result
552545
}
553546

554547
unsafe fn collect_parameter_values(

0 commit comments

Comments
 (0)