Skip to content

Optimize allocations with embedded TypedData#852

Draft
amomchilov wants to merge 3 commits into
Alex/Ruby-owned-graph-allocfrom
Alex/embed-graph-alloc
Draft

Optimize allocations with embedded TypedData#852
amomchilov wants to merge 3 commits into
Alex/Ruby-owned-graph-allocfrom
Alex/embed-graph-alloc

Conversation

@amomchilov

@amomchilov amomchilov commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Closes #839

Copy link
Copy Markdown
Contributor Author

Warning

This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
Learn more

This stack of pull requests is managed by Graphite. Learn more about stacking.

@amomchilov amomchilov changed the title Extract out ruby_compat.h Optimize allocations with embedded TypedData Jun 9, 2026
@amomchilov amomchilov force-pushed the Alex/embed-graph-alloc branch from 19b682a to 64dd2dd Compare June 9, 2026 20:21
@amomchilov amomchilov marked this pull request as ready for review June 9, 2026 20:44
@amomchilov amomchilov requested a review from a team as a code owner June 9, 2026 20:44
@amomchilov amomchilov linked an issue Jun 9, 2026 that may be closed by this pull request
Comment thread ext/rubydex/extconf.rb
append_cflags("-Werror=unused-but-set-variable")
append_cflags("-Werror=implicit-function-declaration")

# Compiles a minimal program to test if the `RUBY_TYPED_EMBEDDABLE` C constant exists ( Ruby 3.3 or newer).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is verbose, but there's so much magic going on here that it's worth documenting.

For one, explicitly mentioning the constant name means that when you search for where HAVE_CONST_RUBY_TYPED_EMBEDDABLE comes from, you'll find this.

Comment thread ext/rubydex/handle.h
@@ -31,7 +36,7 @@ static const rb_data_type_t handle_type = {
},

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.

[Re: line +33]

Do you think you can do this?

#ifdef HAVE_RUBY_TYPED_EMBEDDABLE
        .dfree = RUBY_DEFAULT_FREE,
#else
        .dfree = handle_free,
#end

See this comment inline on Graphite.

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.

That helps us a lot in the GC because it makes the object freeable without calling the handle free function (which is a no-op thanks to embedding)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think so! And then handle_free doesn't have to have the ifdef i just put in it

Comment thread ext/rubydex/graph.c

#ifdef HAVE_RUBY_TYPED_EMBEDDABLE
// The storage is embedded in the TypedData Ruby object itself.
// Don't free `ptr`, because the GC will do that for us.

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.

nit: just to make it clearer which GC it means (I know Rust doesn't have GC, but maybe now everyone reading this knows)

Suggested change
// Don't free `ptr`, because the GC will do that for us.
// Don't free `ptr`, because the Ruby GC will do that for us.

Comment thread ext/rubydex/handle.h Outdated
if (ptr) {
#ifdef HAVE_RUBY_TYPED_EMBEDDABLE
// The storage is embedded in the TypedData Ruby object itself.
// Don't free `ptr`, because the GC will do that for us.

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.

Suggested change
// Don't free `ptr`, because the GC will do that for us.
// Don't free `ptr`, because the Ruby GC will do that for us.

@amomchilov amomchilov force-pushed the Alex/Ruby-owned-graph-alloc branch 2 times, most recently from efeb7e0 to 69e5332 Compare June 10, 2026 20:01
@amomchilov amomchilov force-pushed the Alex/embed-graph-alloc branch from 64dd2dd to 35513df Compare June 10, 2026 20:01
@amomchilov amomchilov requested a review from jhawthorn June 11, 2026 02:39

Copy link
Copy Markdown
Contributor Author

Benchmarked this and got paradoxical results showing that it got slower, not faster. Moving to draft for now.

@amomchilov amomchilov marked this pull request as draft June 11, 2026 17:39
@amomchilov amomchilov force-pushed the Alex/embed-graph-alloc branch from d3fc45d to d14b96b Compare June 11, 2026 18:31
@amomchilov amomchilov force-pushed the Alex/Ruby-owned-graph-alloc branch from 69e5332 to c9fd983 Compare June 11, 2026 18:31
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.

Optimize memory layout of our TypedData objects

4 participants