ext/heap_profiler/heap_profiler.cpp in heap-profiler-0.3.0 vs ext/heap_profiler/heap_profiler.cpp in heap-profiler-0.4.0

- old
+ new

@@ -3,13 +3,13 @@ #include "simdjson.h" #include <fstream> using namespace simdjson; -static VALUE rb_eHeapProfilerError, sym_type, sym_class, sym_address, sym_value, - sym_memsize, sym_imemo_type, sym_struct, sym_file, sym_line, sym_shared, - sym_references, id_uminus; +static VALUE rb_eHeapProfilerError, rb_eHeapProfilerCapacityError, sym_type, sym_class, + sym_address, sym_value, sym_memsize, sym_imemo_type, sym_struct, sym_file, + sym_line, sym_shared, sym_references, id_uminus; typedef struct { dom::parser *parser; } parser_t; @@ -104,16 +104,17 @@ static VALUE rb_heap_build_index(VALUE self, VALUE path, VALUE batch_size) { Check_Type(path, T_STRING); Check_Type(batch_size, T_FIXNUM); dom::parser *parser = get_parser(self); + dom::document_stream objects; VALUE string_index = rb_hash_new(); VALUE class_index = rb_hash_new(); try { - auto [objects, error] = parser->load_many(RSTRING_PTR(path), FIX2INT(batch_size)); + auto error = parser->load_many(RSTRING_PTR(path), FIX2INT(batch_size)).get(objects); if (error != SUCCESS) { rb_raise(rb_eHeapProfilerError, "%s", error_message(error)); } for (dom::object object : objects) { @@ -154,11 +155,15 @@ rb_hash_aset(class_index, address, class_name); } } } } catch (simdjson::simdjson_error error) { - rb_raise(rb_eHeapProfilerError, "exc: %s", error.what()); + if (error.error() == CAPACITY) { + rb_raise(rb_eHeapProfilerCapacityError, "The parser batch size is too small to parse this heap dump"); + } else { + rb_raise(rb_eHeapProfilerError, "exc: %s", error.what()); + } } VALUE return_value = rb_ary_new(); rb_ary_push(return_value, class_index); rb_ary_push(return_value, string_index); @@ -247,13 +252,13 @@ { Check_Type(arg, T_STRING); Check_Type(batch_size, T_FIXNUM); dom::parser *parser = get_parser(self); - + dom::document_stream objects; try { - auto [objects, error] = parser->load_many(RSTRING_PTR(arg), FIX2INT(batch_size)); + auto error = parser->load_many(RSTRING_PTR(arg), FIX2INT(batch_size)).get(objects); if (error != SUCCESS) { rb_raise(rb_eHeapProfilerError, "%s", error_message(error)); } if (RTEST(since)) { @@ -278,11 +283,15 @@ } } return Qnil; } catch (simdjson::simdjson_error error) { - rb_raise(rb_eHeapProfilerError, "%s", error.what()); + if (error.error() == CAPACITY) { + rb_raise(rb_eHeapProfilerCapacityError, "The parser batch size is too small to parse this heap dump"); + } else { + rb_raise(rb_eHeapProfilerError, "exc: %s", error.what()); + } } } extern "C" { void Init_heap_profiler(void) { @@ -301,9 +310,12 @@ VALUE rb_mHeapProfiler = rb_const_get(rb_cObject, rb_intern("HeapProfiler")); rb_eHeapProfilerError = rb_const_get(rb_mHeapProfiler, rb_intern("Error")); rb_global_variable(&rb_eHeapProfilerError); + + rb_eHeapProfilerCapacityError = rb_const_get(rb_mHeapProfiler, rb_intern("CapacityError")); + rb_global_variable(&rb_eHeapProfilerCapacityError); VALUE rb_mHeapProfilerParserNative = rb_const_get(rb_const_get(rb_mHeapProfiler, rb_intern("Parser")), rb_intern("Native")); rb_define_alloc_func(rb_mHeapProfilerParserNative, parser_allocate); rb_define_method(rb_mHeapProfilerParserNative, "_build_index", reinterpret_cast<VALUE (*)(...)>(rb_heap_build_index), 2); rb_define_method(rb_mHeapProfilerParserNative, "parse_address", reinterpret_cast<VALUE (*)(...)>(rb_heap_parse_address), 1);