Skip to content

Commit

Permalink
move skipping anonymous classes to the tracepoint, add struct to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
anmarchenko committed Jul 12, 2024
1 parent 3fc3ac0 commit 4007592
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
18 changes: 8 additions & 10 deletions ext/datadog_cov/datadog_cov.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,16 +235,6 @@ static int process_instantiated_klass(st_data_t key, st_data_t _value, st_data_t
return ST_CONTINUE;
}

// Skip anonymous classes starting with "#<Class".
// This small snippet improves performance for worst case by 20% for rubocop test suite: it allows
// us to skip the source location lookup that will always fail for anonymous classes.
const char *klass_name_ptr = RSTRING_PTR(klass_name);
const unsigned long klass_name_len = RSTRING_LEN(klass_name);
if (klass_name_len >= 2 && klass_name_ptr[0] == '#' && klass_name_ptr[1] == '<')
{
return ST_CONTINUE;
}

VALUE source_location = safely_get_source_location(klass_name);
if (source_location == Qnil || RARRAY_LEN(source_location) == 0)
{
Expand Down Expand Up @@ -283,6 +273,14 @@ static void on_newobj_event(VALUE tracepoint_data, void *data)
{
return;
}
// Skip anonymous classes starting with "#<Class".
// it allows us to skip the source location lookup that will always fail
const char *name = rb_obj_classname(new_object);
const unsigned long klass_name_len = strlen(name);
if (klass_name_len >= 2 && name[0] == '#' && name[1] == '<')
{
return;
}

if (st_lookup(dd_cov_data->klasses_table, (st_data_t)klass, 0))
{
Expand Down
1 change: 1 addition & 0 deletions spec/ddcov/app/model/my_struct.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
User = Struct.new(:name, :email)
5 changes: 4 additions & 1 deletion spec/ddcov/ddcov_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require "datadog_cov.#{RUBY_VERSION}_#{RUBY_PLATFORM}"

require_relative "app/model/my_model"
require_relative "app/model/my_struct"
require_relative "calculator/calculator"
require_relative "calculator/code_with_❤️"

Expand Down Expand Up @@ -329,6 +330,7 @@ def thread_local_cov
subject.start

MyModel.new
User.new("john doe", "johndoe@mail.test")
c = Class.new(Object) do
end
c.new
Expand All @@ -342,8 +344,9 @@ def thread_local_cov
end

coverage = subject.stop
expect(coverage.size).to eq(1)
expect(coverage.size).to eq(2)
expect(coverage.keys).to include(absolute_path("app/model/my_model.rb"))
expect(coverage.keys).to include(absolute_path("app/model/my_struct.rb"))
end
end

Expand Down

0 comments on commit 4007592

Please sign in to comment.