Skip to content

Commit

Permalink
Merge pull request #1397 from larskanis/improve-nodeset-to_a
Browse files Browse the repository at this point in the history
Simplify NodeSet#to_a with a minor speed-up.
  • Loading branch information
larskanis committed Jan 6, 2016
2 parents 2ccfd2f + fbe32c4 commit 0733136
Showing 1 changed file with 3 additions and 11 deletions.
14 changes: 3 additions & 11 deletions ext/nokogiri/xml_node_set.c
Original file line number Diff line number Diff line change
Expand Up @@ -321,25 +321,17 @@ static VALUE slice(int argc, VALUE *argv, VALUE self)
static VALUE to_array(VALUE self, VALUE rb_node)
{
xmlNodeSetPtr node_set ;
VALUE *elts;
VALUE list;
int i;

Data_Get_Struct(self, xmlNodeSet, node_set);

elts = (VALUE *)calloc((size_t)(node_set->nodeNr), sizeof(VALUE));
list = rb_ary_new2(node_set->nodeNr);
for(i = 0; i < node_set->nodeNr; i++) {
elts[i] = Nokogiri_wrap_xml_node_set_node(node_set->nodeTab[i], self);
rb_gc_register_address(&elts[i]);
VALUE elt = Nokogiri_wrap_xml_node_set_node(node_set->nodeTab[i], self);
rb_ary_push( list, elt );
}

list = rb_ary_new4((long)node_set->nodeNr, elts);

for(i = 0; i < node_set->nodeNr; i++) {
rb_gc_unregister_address(&elts[i]);
}
free(elts);

return list;
}

Expand Down

0 comments on commit 0733136

Please sign in to comment.