Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion ext/bigdecimal/bigdecimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -2709,7 +2709,7 @@ rb_float_convert_to_BigDecimal(VALUE val, size_t digs, int raise_exception)
len10 = BIGDECIMAL_DOUBLE_FIGURES;
}
memcpy(buf, p, len10);
xfree(p);
free(p);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Shouldn't we fix dtoa to use xmalloc instead?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I'm not sure there's value in doing so. These allocations are short-lived and running the GC doesn't help us remove them (they aren't associated with a Ruby heap object) so I'm not sure it helps for them to contribute to GC pressure.

This was changed upstream in ruby/ruby@bc8cc68

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Sorry, I should have explained my train of thoughts. I agree there's no GC benefits, especially since when you use xfree (instead of the new xfree_sized) this likely cause a malloc_usable_size call which make the whole thing slower.

However, I recently ran into a bunch of bugs related to using xfree with malloc and vice-versa, so I tend to think it's simpler to just always use the Ruby allocator.

But really up to you.

Anyway, as you said bgidecimal should probably move away from vendoring dtoa.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'd like to use malloc and free instead of xmalloc and xfree in bigdecimal's dtoa.
dtoa and strtod may call MALLOC several times, and if the second call fails to allocate, dtoa frees the first allocated one.

b1 = Balloc(b->k+1);
if (!b1) {
    Bfree(b);
    return NULL;
}
bd = Balloc(bd0->k);
if (!bd) goto retfree;

If we use ruby_xmalloc, and the second allocation raises NoMemoryError, this mechanism will fail, and will cause memory leak.
So I think current dtoa.c in ruby/ruby can be considered designed to be use with malloc and free

move away from vendoring dtoa.

Agree


VALUE inum;
size_t RB_UNUSED_VAR(prec) = 0;
Expand Down
Loading