Skip to content

Update dtoa to version from Ruby 4.0#528

Open
jhawthorn wants to merge 1 commit intoruby:masterfrom
jhawthorn:update_dtoa
Open

Update dtoa to version from Ruby 4.0#528
jhawthorn wants to merge 1 commit intoruby:masterfrom
jhawthorn:update_dtoa

Conversation

@jhawthorn
Copy link
Copy Markdown
Member

@jhawthorn jhawthorn commented Apr 15, 2026

Fixes #527

Upstream commits:

}
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

dtoa is not ractor safe

3 participants