Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Conversation

st0012
Copy link
Member

@st0012 st0012 commented Apr 10, 2025

RBS C Parser Library Refactoring

This PR refactors the RBS parser and related components into a standalone C library that no longer depends on the Ruby runtime. This architectural change enables direct integration with static analysis tools like Sorbet while potentially improving performance.

Sorbet's RBS support already runs on this new architecture and we haven't discovered any major issues around it.

This work was a collaborative effort by

Key Improvements

  • Ruby-Independent Implementation: Extracted parser from ext folder into a standalone C library with a clean API, which can now be embedded in non-Ruby tools without Ruby runtime dependency (e.g. Sorbet, JRuby)
  • Enhanced Memory Management: Implemented arena allocator to efficiently manage parser object lifecycles
  • Improved Architecture: Clear separation between public API (headers) and implementation
  • Performance: Potential performance gains from custom memory management and reduced overhead

Enhanced Memory Management

Arena allocator handles all memory for parser objects, including parser itself, lexer, constant pool, strings...etc. When the parser is freed by calling rbs_parser_free, the allocator will free all the objects it allocated. This eliminates the need to manually free individual objects and reduces the risk of memory leaks.

Component Architecture

graph TD
    RubyClient[Ruby Client] --> RubyAPI[Ruby API]
    CClient[C Client] --> CAPI[C API]

    RubyAPI --> CExtension[C Extension]
    CExtension --> CLibrary
    CAPI --> CLibrary

    subgraph CLibrary[C Library]
        subgraph Parser1[Parser Instance 1]
            direction TB
            ConstantPool1[Constant Pool]
            Lexer1[Lexer]
            ArenaAllocator1[Arena Allocator]
        end

        subgraph Parser2[Parser Instance 2]
            direction TB
            ConstantPool2[Constant Pool]
            Lexer2[Lexer]
            ArenaAllocator2[Arena Allocator]
        end
    end

    subgraph "Public API"
        RubyAPI
        CAPI
    end

    %% Parser1 --> ConstantPool1
    %% Parser1 --> Lexer1
    %% Parser1 --> ArenaAllocator1
    %% Parser2 --> ConstantPool2
    %% Parser2 --> Lexer2
    %% Parser2 --> ArenaAllocator2
Loading

amomchilov and others added 30 commits February 25, 2025 18:14
Initial template for C structs

Use allocator in node constructors
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>

Add linked list implementation

Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>

Type `Class#super_class` field

Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>

Type fields of `RBS::Types::Block`

Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>

Type `block` fields

Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>

Type `RBS::Types::Proc#self_type` field

Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>

Refactor `parse_function`

Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>

Copy value in `rbs_struct_to_ruby_value`

Remove usages of `rbs_loc` from `parser.c`

Extract `rbs_location.h`

Migrate `RBS::Types::Function::Param` fields

Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>

Type `RBS::Types::UntypedFunction` fields

Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>

Type fields of `RBS::AST::TypeParam`

Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>

Type some more fields of `RBS::AST::Members::Attr`

Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>

Type fields in `RBS::AST::Members::MethodDefinition`

Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>

Type `RBS::AST::Directives::Use::SingleClause#new_name`

Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>

Type `RBS::Namespace#absolute`

Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>

Temporary handle nil types

Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>

Handle `bool` type

Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>

Type all fields of `RBS::Types::Variable`

Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>

Migrate `RBS::TypeName`

Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>

Migrate `parse_use_clauses`

Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>

Migrate `class_instance_name`

Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>

Handle overloads as a rbs_node_list

Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>

Remove more `builds_ruby_object_internally` flags

Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>

Invert `builds_ruby_object_internally` default value

Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>

Introduce `rbs_location_t`

Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>

Store C structs instead of Ruby `VALUE`s

Introduce +rbs_ast_symbol_t and migrate to it

Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>

Remove ZzzTmpNotImplemented node

Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>

Remove one more instance of EMPTY_ARRAY

Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>

Migrate from VALUE array to rbs_node_list_t

Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>

Migrate `method_params` from taking a VALUE arrays

Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>

Migrate `parse_type_list` from taking a VALUE array

Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>

Forward all C-typed params as-is

Get types on constructor params

Handle mix of C types and Ruby VALUE

Move Ruby object construction into `new` functions

Conditionally construct `ruby_value` internally

Type Attr* field `ivar_name`

Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>

Add `AST::Bool`

Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>

Use two less VALUE values

Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>

Use more instance of `bool`

Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>

Add Hash implementation

Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>

Use C hash for `check_key_duplication`

Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>

Use C hash to represent Record fields

Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>

Migrate `memo` to using a C hash

Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>

Uses C hashes for keyword parameters

Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>

Remove parser call to `todo!`

Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>

Remove calls to `rbs_struct_to_ruby_value`

Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>

TMP symbol

Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>

Replace 2 fake nodes by one

Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>

Set fields for `Record::FieldType`

Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>

Make comment use a `rbs_ast_comment_t` instead of a `VALUE`

Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>

Add `rbs_ast_string_t`

Add `rbs_ast_integer_t`

Migrate `literal` to store C nodes

Remove `cached_ruby_string`

Remove useless templating stuff

Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>

Remove `cached_ruby_value` from `rbs_node_list`

Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>

Remove `cached_ruby_value` from `rbs_hash`

Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>

Add `rbs_string`, and use it for annotations

Add `rbs_ast_symbol_t` to model symbols in the AST

Co-Authored-By: Alexander Momchilov <alexander.momchilov@shopify.com>
And rename it to `class_constants` to disambiguate it from `rbs_constant_id`, `rbs_constant_pool`, etc.
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>

Do not create comments using a VALUE

Use a rbs_string instead

Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
.idea/.gitignore Outdated Show resolved Hide resolved
.vscode/launch.json Outdated Show resolved Hide resolved
demo.rb Outdated Show resolved Hide resolved
make_signed_ruby.rb Outdated Show resolved Hide resolved
sync_from_prism.rb Outdated Show resolved Hide resolved
trace_rbs.sh Outdated Show resolved Hide resolved
st0012 added 3 commits April 25, 2025 16:35
`rbs_node_destroy`, `rbs_hash_free`, `rbs_node_list_free` are only
calling each other recursively without any real freeing logic.

This is the result of previous efforts to allocate all nodes on the
arena. So we don't need these functions anymore.

Discovered while working on #41
@Morriar Morriar mentioned this pull request Apr 25, 2025
7 tasks
st0012 and others added 4 commits April 30, 2025 21:23
Co-authored-by: Alexander Momchilov <amomchilov@users.noreply.github.com>
Co-authored-by: Alexander Momchilov <amomchilov@users.noreply.github.com>
@st0012 st0012 changed the title [Feedback Wanted] Introduce standalone C parser for RBS with arena allocation Introduce standalone C parser for RBS with arena allocation Apr 30, 2025
@st0012 st0012 marked this pull request as ready for review April 30, 2025 14:48
@soutaro soutaro added this to the RBS 4.0 milestone May 1, 2025
@soutaro
Copy link
Member

soutaro commented May 1, 2025

Thank you all for this contribution! I really appreciate all of you. 👏
The code generally looks great, but to confirm everything works I would like to implement another parsing function by myself. I will try it tomorrow. 🙏

Copy link
Member

@soutaro soutaro left a comment

Choose a reason for hiding this comment

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

I tried to add a new inline annotation at #2443, and confirmed that I feel I can work on this codebase. 👍

@soutaro soutaro added this pull request to the merge queue May 2, 2025
Merged via the queue into ruby:master with commit 4ecd51a May 2, 2025
20 checks passed
@soutaro
Copy link
Member

soutaro commented May 2, 2025

Opened ruby/ruby#13237 to test if the new C code works with Ruby CI compilers. (I should have done the test before merging... 💦 )

@amomchilov amomchilov deleted the c-api branch July 2, 2025 13:59
soutaro added a commit that referenced this pull request Oct 6, 2025
Introduce standalone C parser for RBS with arena allocation
@soutaro soutaro mentioned this pull request Oct 6, 2025
soutaro added a commit that referenced this pull request Oct 6, 2025
Introduce standalone C parser for RBS with arena allocation
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.

5 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.