Skip to content

Commit 6b7a6ef

Browse files
committed
refactor: moved all the interned symbols to a separate structure, called InternedSymbols
1 parent 30f0adb commit 6b7a6ef

File tree

1 file changed

+60
-48
lines changed
  • gems/codetracer-ruby-recorder/ext/native_tracer/src

1 file changed

+60
-48
lines changed

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

Lines changed: 60 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ use rb_sys::{
4242
rb_tracearg_return_value, rb_tracearg_self,
4343
};
4444

45-
struct Recorder {
46-
tracer: Box<dyn TraceWriter>,
47-
active: bool,
45+
struct InternedSymbols {
4846
to_s_id: ID,
4947
local_variables_id: ID,
5048
local_variable_get_id: ID,
@@ -65,6 +63,39 @@ struct Recorder {
6563
instance_variable_get_id: ID,
6664
set_const_id: ID,
6765
open_struct_const_id: ID,
66+
}
67+
68+
impl InternedSymbols {
69+
unsafe fn new() -> InternedSymbols {
70+
InternedSymbols {
71+
to_s_id: rb_intern!("to_s"),
72+
local_variables_id: rb_intern!("local_variables"),
73+
local_variable_get_id: rb_intern!("local_variable_get"),
74+
instance_method_id: rb_intern!("instance_method"),
75+
parameters_id: rb_intern!("parameters"),
76+
class_id: rb_intern!("class"),
77+
to_a_id: rb_intern!("to_a"),
78+
begin_id: rb_intern!("begin"),
79+
end_id: rb_intern!("end"),
80+
to_i_id: rb_intern!("to_i"),
81+
nsec_id: rb_intern!("nsec"),
82+
source_id: rb_intern!("source"),
83+
options_id: rb_intern!("options"),
84+
members_id: rb_intern!("members"),
85+
values_id: rb_intern!("values"),
86+
to_h_id: rb_intern!("to_h"),
87+
instance_variables_id: rb_intern!("instance_variables"),
88+
instance_variable_get_id: rb_intern!("instance_variable_get"),
89+
set_const_id: rb_intern!("Set"),
90+
open_struct_const_id: rb_intern!("OpenStruct"),
91+
}
92+
}
93+
}
94+
95+
struct Recorder {
96+
tracer: Box<dyn TraceWriter>,
97+
active: bool,
98+
id: InternedSymbols,
6899
set_class: VALUE,
69100
open_struct_class: VALUE,
70101
struct_type_versions: HashMap<String, usize>,
@@ -186,26 +217,7 @@ unsafe extern "C" fn ruby_recorder_alloc(klass: VALUE) -> VALUE {
186217
let recorder = Box::new(Recorder {
187218
tracer: create_trace_writer("ruby", &vec![], TraceEventsFileFormat::Binary),
188219
active: false,
189-
to_s_id: rb_intern!("to_s"),
190-
local_variables_id: rb_intern!("local_variables"),
191-
local_variable_get_id: rb_intern!("local_variable_get"),
192-
instance_method_id: rb_intern!("instance_method"),
193-
parameters_id: rb_intern!("parameters"),
194-
class_id: rb_intern!("class"),
195-
to_a_id: rb_intern!("to_a"),
196-
begin_id: rb_intern!("begin"),
197-
end_id: rb_intern!("end"),
198-
to_i_id: rb_intern!("to_i"),
199-
nsec_id: rb_intern!("nsec"),
200-
source_id: rb_intern!("source"),
201-
options_id: rb_intern!("options"),
202-
members_id: rb_intern!("members"),
203-
values_id: rb_intern!("values"),
204-
to_h_id: rb_intern!("to_h"),
205-
instance_variables_id: rb_intern!("instance_variables"),
206-
instance_variable_get_id: rb_intern!("instance_variable_get"),
207-
set_const_id: rb_intern!("Set"),
208-
open_struct_const_id: rb_intern!("OpenStruct"),
220+
id: InternedSymbols::new(),
209221
set_class: Qnil.into(),
210222
open_struct_class: Qnil.into(),
211223
struct_type_versions: HashMap::new(),
@@ -289,7 +301,7 @@ unsafe fn value_to_string(recorder: &Recorder, val: VALUE) -> Option<String> {
289301
let slice = std::slice::from_raw_parts(ptr as *const u8, len);
290302
return Some(String::from_utf8_lossy(slice).to_string());
291303
}
292-
let str_val = rb_funcall(val, recorder.to_s_id, 0);
304+
let str_val = rb_funcall(val, recorder.id.to_s_id, 0);
293305
let ptr = RSTRING_PTR(str_val);
294306
let len = RSTRING_LEN(str_val) as usize;
295307
let slice = std::slice::from_raw_parts(ptr as *const u8, len);
@@ -309,7 +321,7 @@ unsafe fn value_to_string_safe(recorder: &Recorder, val: VALUE) -> Option<String
309321
return Some(String::from_utf8_lossy(slice).to_string());
310322
}
311323
let mut state: c_int = 0;
312-
let data = (val, recorder.to_s_id);
324+
let data = (val, recorder.id.to_s_id);
313325
let str_val = rb_protect(Some(call_to_s), &data as *const _ as VALUE, &mut state);
314326
if state != 0 {
315327
return None;
@@ -388,7 +400,7 @@ unsafe fn to_value(recorder: &mut Recorder, val: VALUE, depth: usize) -> ValueRe
388400
};
389401
}
390402
if RB_TYPE_P(val, rb_sys::ruby_value_type::RUBY_T_HASH) {
391-
let pairs = rb_funcall(val, recorder.to_a_id, 0);
403+
let pairs = rb_funcall(val, recorder.id.to_a_id, 0);
392404
let len = RARRAY_LEN(pairs) as usize;
393405
let ptr = RARRAY_CONST_PTR(pairs);
394406
let mut elements = Vec::with_capacity(len);
@@ -416,8 +428,8 @@ unsafe fn to_value(recorder: &mut Recorder, val: VALUE, depth: usize) -> ValueRe
416428
};
417429
}
418430
if rb_obj_is_kind_of(val, rb_cRange) != 0 {
419-
let begin_val = rb_funcall(val, recorder.begin_id, 0);
420-
let end_val = rb_funcall(val, recorder.end_id, 0);
431+
let begin_val = rb_funcall(val, recorder.id.begin_id, 0);
432+
let end_val = rb_funcall(val, recorder.id.end_id, 0);
421433
return struct_value(
422434
recorder,
423435
"Range",
@@ -427,12 +439,12 @@ unsafe fn to_value(recorder: &mut Recorder, val: VALUE, depth: usize) -> ValueRe
427439
);
428440
}
429441
if NIL_P(recorder.set_class) {
430-
if rb_const_defined(rb_cObject, recorder.set_const_id) != 0 {
431-
recorder.set_class = rb_const_get(rb_cObject, recorder.set_const_id);
442+
if rb_const_defined(rb_cObject, recorder.id.set_const_id) != 0 {
443+
recorder.set_class = rb_const_get(rb_cObject, recorder.id.set_const_id);
432444
}
433445
}
434446
if !NIL_P(recorder.set_class) && rb_obj_is_kind_of(val, recorder.set_class) != 0 {
435-
let arr = rb_funcall(val, recorder.to_a_id, 0);
447+
let arr = rb_funcall(val, recorder.id.to_a_id, 0);
436448
if RB_TYPE_P(arr, rb_sys::ruby_value_type::RUBY_T_ARRAY) {
437449
let len = RARRAY_LEN(arr) as usize;
438450
let ptr = RARRAY_CONST_PTR(arr);
@@ -450,13 +462,13 @@ unsafe fn to_value(recorder: &mut Recorder, val: VALUE, depth: usize) -> ValueRe
450462
}
451463
}
452464
if rb_obj_is_kind_of(val, rb_cTime) != 0 {
453-
let sec = rb_funcall(val, recorder.to_i_id, 0);
454-
let nsec = rb_funcall(val, recorder.nsec_id, 0);
465+
let sec = rb_funcall(val, recorder.id.to_i_id, 0);
466+
let nsec = rb_funcall(val, recorder.id.nsec_id, 0);
455467
return struct_value(recorder, "Time", &["sec", "nsec"], &[sec, nsec], depth);
456468
}
457469
if rb_obj_is_kind_of(val, rb_cRegexp) != 0 {
458-
let src = rb_funcall(val, recorder.source_id, 0);
459-
let opts = rb_funcall(val, recorder.options_id, 0);
470+
let src = rb_funcall(val, recorder.id.source_id, 0);
471+
let opts = rb_funcall(val, recorder.id.options_id, 0);
460472
return struct_value(
461473
recorder,
462474
"Regexp",
@@ -468,8 +480,8 @@ unsafe fn to_value(recorder: &mut Recorder, val: VALUE, depth: usize) -> ValueRe
468480
if rb_obj_is_kind_of(val, rb_cStruct) != 0 {
469481
let class_name =
470482
cstr_to_string(rb_obj_classname(val)).unwrap_or_else(|| "Struct".to_string());
471-
let members = rb_funcall(val, recorder.members_id, 0);
472-
let values = rb_funcall(val, recorder.values_id, 0);
483+
let members = rb_funcall(val, recorder.id.members_id, 0);
484+
let values = rb_funcall(val, recorder.id.values_id, 0);
473485
if !RB_TYPE_P(members, rb_sys::ruby_value_type::RUBY_T_ARRAY)
474486
|| !RB_TYPE_P(values, rb_sys::ruby_value_type::RUBY_T_ARRAY)
475487
{
@@ -494,18 +506,18 @@ unsafe fn to_value(recorder: &mut Recorder, val: VALUE, depth: usize) -> ValueRe
494506
return struct_value(recorder, &class_name, &names, &vals, depth);
495507
}
496508
if NIL_P(recorder.open_struct_class) {
497-
if rb_const_defined(rb_cObject, recorder.open_struct_const_id) != 0 {
498-
recorder.open_struct_class = rb_const_get(rb_cObject, recorder.open_struct_const_id);
509+
if rb_const_defined(rb_cObject, recorder.id.open_struct_const_id) != 0 {
510+
recorder.open_struct_class = rb_const_get(rb_cObject, recorder.id.open_struct_const_id);
499511
}
500512
}
501513
if !NIL_P(recorder.open_struct_class) && rb_obj_is_kind_of(val, recorder.open_struct_class) != 0
502514
{
503-
let h = rb_funcall(val, recorder.to_h_id, 0);
515+
let h = rb_funcall(val, recorder.id.to_h_id, 0);
504516
return to_value(recorder, h, depth - 1);
505517
}
506518
let class_name = cstr_to_string(rb_obj_classname(val)).unwrap_or_else(|| "Object".to_string());
507519
// generic object
508-
let ivars = rb_funcall(val, recorder.instance_variables_id, 0);
520+
let ivars = rb_funcall(val, recorder.id.instance_variables_id, 0);
509521
if !RB_TYPE_P(ivars, rb_sys::ruby_value_type::RUBY_T_ARRAY) {
510522
let text = value_to_string(recorder, val).unwrap_or_default();
511523
let type_id =
@@ -522,7 +534,7 @@ unsafe fn to_value(recorder: &mut Recorder, val: VALUE, depth: usize) -> ValueRe
522534
let cstr = rb_id2name(id);
523535
let name = CStr::from_ptr(cstr).to_str().unwrap_or("?");
524536
names.push(name);
525-
let value = rb_funcall(val, recorder.instance_variable_get_id, 1, sym);
537+
let value = rb_funcall(val, recorder.id.instance_variable_get_id, 1, sym);
526538
vals.push(value);
527539
}
528540
if !names.is_empty() {
@@ -534,7 +546,7 @@ unsafe fn to_value(recorder: &mut Recorder, val: VALUE, depth: usize) -> ValueRe
534546
}
535547

536548
unsafe fn record_variables(recorder: &mut Recorder, binding: VALUE) -> Vec<FullValueRecord> {
537-
let vars = rb_funcall(binding, recorder.local_variables_id, 0);
549+
let vars = rb_funcall(binding, recorder.id.local_variables_id, 0);
538550
if !RB_TYPE_P(vars, rb_sys::ruby_value_type::RUBY_T_ARRAY) {
539551
return Vec::new();
540552
}
@@ -545,7 +557,7 @@ unsafe fn record_variables(recorder: &mut Recorder, binding: VALUE) -> Vec<FullV
545557
let sym = *ptr.add(i);
546558
let id = rb_sym2id(sym);
547559
let name = CStr::from_ptr(rb_id2name(id)).to_str().unwrap_or("");
548-
let value = rb_funcall(binding, recorder.local_variable_get_id, 1, sym);
560+
let value = rb_funcall(binding, recorder.id.local_variable_get_id, 1, sym);
549561
let val_rec = to_value(recorder, value, 10);
550562
TraceWriter::register_variable_with_full_value(
551563
&mut *recorder.tracer,
@@ -571,8 +583,8 @@ unsafe fn collect_parameter_values(
571583
if rb_method_boundp(defined_class, mid, 0) == 0 {
572584
return Vec::new();
573585
}
574-
let method_obj = rb_funcall(defined_class, recorder.instance_method_id, 1, method_sym);
575-
let params_ary = rb_funcall(method_obj, recorder.parameters_id, 0);
586+
let method_obj = rb_funcall(defined_class, recorder.id.instance_method_id, 1, method_sym);
587+
let params_ary = rb_funcall(method_obj, recorder.id.parameters_id, 0);
576588
if !RB_TYPE_P(params_ary, rb_sys::ruby_value_type::RUBY_T_ARRAY) {
577589
return Vec::new();
578590
}
@@ -595,7 +607,7 @@ unsafe fn collect_parameter_values(
595607
continue;
596608
}
597609
let name = CStr::from_ptr(name_c).to_str().unwrap_or("").to_string();
598-
let value = rb_funcall(binding, recorder.local_variable_get_id, 1, name_sym);
610+
let value = rb_funcall(binding, recorder.id.local_variable_get_id, 1, name_sym);
599611
let val_rec = to_value(recorder, value, 10);
600612
result.push((name, val_rec));
601613
}
@@ -798,7 +810,7 @@ unsafe extern "C" fn event_hook_raw(data: VALUE, arg: *mut rb_trace_arg_t) {
798810
let self_val = rb_tracearg_self(arg);
799811
let mid_sym = rb_tracearg_callee_id(arg);
800812
let mid = rb_sym2id(mid_sym);
801-
let defined_class = rb_funcall(self_val, recorder.class_id, 0);
813+
let defined_class = rb_funcall(self_val, recorder.id.class_id, 0);
802814

803815
let param_vals = if NIL_P(binding) {
804816
Vec::new()

0 commit comments

Comments
 (0)