# Re: converting some autogenerated ruby code to C

**URL:** <https://www.ruby-forum.com/t/re-converting-some-autogenerated-ruby-code-to-c/100244>\
**Category:** Ruby\
**Created:** [July 27, 2006, 11:50pm UTC](https://www.ruby-forum.com/t/re-converting-some-autogenerated-ruby-code-to-c/100244 "2006-07-27T23:50:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [July 27, 2006, 11:50pm UTC](https://www.ruby-forum.com/t/re-converting-some-autogenerated-ruby-code-to-c/100244/1 "2006-07-27T23:50:15Z")

</div>

> few methods with giant expressions in each. I would like to  
> try improving the performance by generating and compiling C  
> code instead. My preference would be to convert my small  
> subset of ruby code to C code (easily have an option to use  
> pure ruby or use ruby/C), but I could also autogenerate C  
> code instead. Anybody have any opinions/ideas about what  
> approach I whould take?

Couldn’t you start with Ruby, see how it performs, and add C support  
later? Or does it become more difficult later?

Just remember, not everyone has a C compiler on their system…

Regards,

Dan

This communication is the property of Qwest and may contain confidential  
or  
privileged information. Unauthorized use of this communication is  
strictly  
prohibited and may be unlawful. If you have received this communication  
in error, please immediately notify the sender by reply e-mail and  
destroy  
all copies of the communication and any attachments.

---

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [July 28, 2006, 5:12pm UTC](https://www.ruby-forum.com/t/re-converting-some-autogenerated-ruby-code-to-c/100244/2 "2006-07-28T17:12:53Z")

</div>

On 7/27/06, Berger, Daniel [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Couldn’t you start with Ruby, see how it performs, and add C support  
> later? Or does it become more difficult later?

I’ve already have a pure ruby solution. There are a couple reasons I  
would like to have an alternate C solution:

- I’m not using regular expressions at the lowest level. The solution  
I have has a superset of the functionality of regular expressions  
(full parsing, works on more than just strings). I would rather not  
move to use regular expressions just for the sake of performance. I  
may still do it at some point, but it will add ugliness since I have  
to buffer into a string. Instead, I would rather just get the same  
advantage that regular expressions have - they are written in C. I’m  
not sure if I would be able to quite get to the regex performance or  
not. On one hand, I’m doing code generation/compiling and Regexp is  
just putting the regex into a more manageable structure. On the other  
hand, my solution is duck-typed and not tied to String, so I’ll have  
messaging overhead for each additional char/token/element read  
(input.read1next) and checked (pattern.===).

- I would like to get this parsing on par with parsers from other  
languages - YACC, ANTLR, etc. I would like to get some bragging  
rights relative to these. Of course, when you add ruby actions  
(blocks) in the parsing this could slow things down quite a bit, since  
you’re back into ruby instead of C.

> Just remember, not everyone has a C compiler on their system…

That’s why I’ll provide a pure ruby and a ruby/C solution.

Anybody want to point me to some examples/docs of ruby2c, rb2c, etc?

---

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [July 28, 2006, 5:19pm UTC](https://www.ruby-forum.com/t/re-converting-some-autogenerated-ruby-code-to-c/100244/3 "2006-07-28T17:19:46Z")

</div>

On Sat, 29 Jul 2006, Eric M. wrote:

> That’s why I’ll provide a pure ruby and a ruby/C solution.
> 
> Anybody want to point me to some examples/docs of ruby2c, rb2c, etc?

why not just use ruby-inline? it’s probably the easiest way to generate  
c  
from ruby.

-a

---

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [July 28, 2006, 6:57pm UTC](https://www.ruby-forum.com/t/re-converting-some-autogenerated-ruby-code-to-c/100244/4 "2006-07-28T18:57:00Z")

</div>

On 7/28/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Sat, 29 Jul 2006, Eric M. wrote:
> 
> > That’s why I’ll provide a pure ruby and a ruby/C solution.
> > 
> > Anybody want to point me to some examples/docs of ruby2c, rb2c, etc?
> 
> why not just use ruby-inline? it’s probably the easiest way to generate c  
> from ruby.

well, Ruby2C generates C from Ruby … RubyInline lets you put some C  
in your Ruby.
