Skip to content

Conversation

@gmalette
Copy link

This is a prototype, please don't actually merge this as I cannot write C code and this is all Claude-generated.

The idea is to add a C-level fast path for serializing and deserializing Ruby Struct types, bypassing Ruby proc callbacks entirely.

  factory.register_type(0x01, MyStruct, optimized_struct: true)

The wire format is identical to the recursive proc-based approach.

Adds a C-level fast path for serializing and deserializing Ruby Struct
types, bypassing Ruby proc callbacks entirely.

Usage:
  factory.register_type(0x01, MyStruct, optimized_struct: true)

Benefits:
- Directly accesses struct fields via RSTRUCT_GET in C
- No Ruby method calls for field access
- No proc allocation or invocation overhead
- Supports both regular and keyword_init structs

The wire format is identical to the recursive proc-based approach,
ensuring compatibility with existing serialized data.

This provides significant performance improvements for applications
that serialize many Struct objects.

puts "\nBenchmarking scenario: P:#{scenario[:products]} V:#{scenario[:variants]} PM:#{scenario[:product_metafields]} VM:#{scenario[:variant_metafields]} SPG:#{scenario[:spg]} SP:#{scenario[:sp]}"

payloads = coders.map { |coder| coder.factory.dump(data) }
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
payloads = coders.map { |coder| coder.factory.dump(data) }
payloads = coders.map { |coder| coder.factory.dump(data).freeze }

FYI: If the payload is frozen, it saves a bunch of work for msgpack. The diff isn't massive, but on you simple benchmark it gives another ~0.10x over marshal.

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.

2 participants