Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -934,7 +934,9 @@ mrb_ary_set(mrb_state *mrb, mrb_value ary, mrb_int n, mrb_value val)
static struct RArray*
ary_dup(mrb_state *mrb, struct RArray *a)
{
return ary_new_from_values(mrb, ARY_LEN(a), ARY_PTR(a));
struct RArray *dup = ary_new_capa(mrb, 0);
ary_replace(mrb, dup, a);
return dup;
Comment on lines +937 to +939

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The current implementation uses ary_replace to duplicate the array, which might introduce a slight performance overhead due to the additional function call and potential re-allocation. Consider directly allocating and copying the array contents for better performance in the duplication scenario. A more explicit check for sharing conditions upfront could improve both clarity and performance.

For example:

mrb_int len = ARY_LEN(a);
struct RArray *dup = ary_new_capa(mrb, len);
if (ARY_SHARED_P(a)) {
  ary_replace(mrb, dup, a);
} else {
  array_copy(ARY_PTR(dup), ARY_PTR(a), len);
  ARY_SET_LEN(dup, len);
}
return dup;
mrb_int len = ARY_LEN(a);
struct RArray *dup = ary_new_capa(mrb, len);
if (ARY_SHARED_P(a)) {
  ary_replace(mrb, dup, a);
} else {
  array_copy(ARY_PTR(dup), ARY_PTR(a), len);
  ARY_SET_LEN(dup, len);
}
return dup;

}

MRB_API mrb_value
Expand Down
Loading