{"id":13879333,"url":"https://github.com/agrafix/rubyspeed","last_synced_at":"2025-03-16T23:31:39.505Z","repository":{"id":66320106,"uuid":"295319256","full_name":"agrafix/rubyspeed","owner":"agrafix","description":"Compile ruby functions to C","archived":false,"fork":false,"pushed_at":"2020-09-21T01:24:05.000Z","size":59,"stargazers_count":183,"open_issues_count":0,"forks_count":2,"subscribers_count":11,"default_branch":"master","last_synced_at":"2025-03-08T20:48:57.947Z","etag":null,"topics":["c","compiler","performance","ruby"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/agrafix.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-09-14T06:04:24.000Z","updated_at":"2025-02-19T06:07:26.000Z","dependencies_parsed_at":null,"dependency_job_id":"812d38d0-c27e-4aeb-a297-3acc774727a1","html_url":"https://github.com/agrafix/rubyspeed","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/agrafix%2Frubyspeed","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/agrafix%2Frubyspeed/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/agrafix%2Frubyspeed/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/agrafix%2Frubyspeed/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/agrafix","download_url":"https://codeload.github.com/agrafix/rubyspeed/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243833007,"owners_count":20355188,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["c","compiler","performance","ruby"],"created_at":"2024-08-06T08:02:17.473Z","updated_at":"2025-03-16T23:31:39.498Z","avatar_url":"https://github.com/agrafix.png","language":"Ruby","readme":"# Rubyspeed\n\n_Work in progress._\n\nWelcome to Rubyspeed. Right now, Rubyspeed is a basic proof of concept (horribly hacked together) that allows annotating method declarations to automatically be specialized and compiled to C. Here's an example:\n\n``` ruby\nrequire 'rubyspeed'\n\nclass TestClass\n  extend(Rubyspeed::Compiles)\n    \n  compile!(params: [Rubyspeed::T.array(Rubyspeed::T.int), Rubyspeed::T.array(Rubyspeed::T.int)], return_type: Rubyspeed::T.int)\n  def self.dot(a, b)\n    c = Rubyspeed::Let.int(0)\n    a.each_with_index do |a_val, idx|\n      c += a_val * b[idx]\n    end\n    c\n  end\nend\n```\n\nThis will automatically replace the `dot` implementation with a compiled C version, that runs quite a bit (5x) faster than the native ruby version:\n\n```shellsession\n$ rake bench\n               user     system      total        real\ncompiled   0.000021   0.000004   0.000025 (  0.000018)\nruby       0.000105   0.000002   0.000107 (  0.000103)\n```\n\n## How does this work?\n\nIn short:\n\n* Use a neat annotation trick inspired by the [sorbet runtime](https://github.com/sorbet/sorbet/blob/d4c80e0ac3b04e64770f1b050fbab3c6c39b58eb/gems/sorbet-runtime/lib/types/private/methods/_methods.rb#L461) to emulate annotations (compare to `@Deprecated` in Java for example)\n* Extract the ruby source from the given method\n* Transform it to s-expressions\n* Generate C code from the s-expressions\n* Use a C compiler to compile to a ruby module\n* Replace original implementation with a call to the compiled ruby module\n\n## Inspiration\n\nThis project was inspired by [Stephen Diehl's LLVM specializer for Python](http://dev.stephendiehl.com/numpile/) and [rubyinline](https://github.com/seattlerb/rubyinline).\n\n## Current Status\n\nThe project can only compile extremely primitive functions (basically only simple numeric computations). I am open to any pull requests for improvements, but would discourage using this in production anywhere :-)\n\n## Hacking\n\n``` shellsession\n$ bundle install\n$ rake test # run the tests\n$ rake bench # run the benchmarks\n```\n","funding_links":[],"categories":["Ruby"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fagrafix%2Frubyspeed","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fagrafix%2Frubyspeed","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fagrafix%2Frubyspeed/lists"}