Skip to content

Commit 763fea8

Browse files
Improved performance.
1 parent 21d12a0 commit 763fea8

File tree

4 files changed

+201
-103
lines changed

4 files changed

+201
-103
lines changed

ext/memory/profiler/capture.c

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ static VALUE Memory_Profiler_Capture = Qnil;
2020
// Event symbols:
2121
static VALUE sym_newobj, sym_freeobj;
2222

23-
2423
// Main capture state (per-instance).
2524
struct Memory_Profiler_Capture {
2625
// Master switch - is tracking active? (set by start/stop).
@@ -199,7 +198,6 @@ static void Memory_Profiler_Capture_process_newobj(VALUE self, VALUE klass, VALU
199198
RB_OBJ_WRITTEN(self, Qnil, object);
200199
RB_OBJ_WRITE(self, &entry->klass, klass);
201200
RB_OBJ_WRITE(self, &entry->data, data);
202-
RB_OBJ_WRITE(self, &entry->allocations, allocations);
203201

204202
if (DEBUG) fprintf(stderr, "[NEWOBJ] Object inserted into table: %p\n", (void*)object);
205203

@@ -227,7 +225,15 @@ static void Memory_Profiler_Capture_process_freeobj(VALUE capture_value, VALUE u
227225

228226
VALUE klass = entry->klass;
229227
VALUE data = entry->data;
230-
VALUE allocations = entry->allocations;
228+
229+
// Look up allocations from tracked table:
230+
st_data_t allocations_data;
231+
if (!st_lookup(capture->tracked, (st_data_t)klass, &allocations_data)) {
232+
// Class not tracked - shouldn't happen, but be defensive:
233+
if (DEBUG) fprintf(stderr, "[FREEOBJ] Class not found in tracked: %p\n", (void*)klass);
234+
goto done;
235+
}
236+
VALUE allocations = (VALUE)allocations_data;
231237

232238
// Delete by entry pointer (faster - no second lookup!)
233239
Memory_Profiler_Object_Table_delete_entry(capture->states, entry);
@@ -596,12 +602,19 @@ static VALUE Memory_Profiler_Capture_each_object_body(VALUE arg) {
596602
continue;
597603
}
598604

605+
// Look up allocations from klass
606+
st_data_t allocations_data;
607+
VALUE allocations = Qnil;
608+
if (st_lookup(capture->tracked, (st_data_t)entry->klass, &allocations_data)) {
609+
allocations = (VALUE)allocations_data;
610+
}
611+
599612
// Filter by allocations if specified
600613
if (!NIL_P(arguments->allocations)) {
601-
if (entry->allocations != arguments->allocations) continue;
614+
if (allocations != arguments->allocations) continue;
602615
}
603616

604-
rb_yield_values(2, entry->object, entry->allocations);
617+
rb_yield_values(2, entry->object, allocations);
605618
}
606619
}
607620

0 commit comments

Comments
 (0)