Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Ruby outperforms C that is a Ruby extension, solving a problem by wrangling with Ruby types.

When you write a run-time for a dynamic, high level language, and then library functions in C which use the API's and objects of that run time, the resulting code is reasonably fast, but never as fast as solving the problem using direct C idioms.

There are ways to make improvements in that type of code, but they tend to be tedious to code. For instance, you can use split the code into cases by type, and then use faster, more type-specific routines.

Sometimes it's possible to get all the inputs out of the objects, work with lower, less encapsulated level C representations to do the bulk of the problem, and then put results back into the run time's objects again. Or do that partially at certain steps.

In the TXR Lisp mapcar implementation, alloca is used to create a native C array of stack-allocated iterator objects. (For the parallel iteration over multiple sequences: mapcar is an N-ary function!). The element values are pulled from all these iterators and stuffed into a stack allocated args structure destructively; that same structure is re-used for each call to the projection function.

That's not as fast as it could be. Beyond that it would be possible to separately handle the case when there is only one sequence argument, and to split that by type, and other such approaches.

It's faster, though, than if it allocated a dynamic vector of dynamic iterators, and/or if it built a dynamic list of arguments in each iteration which were applied to the function as a list.



Consider applying for YC's Winter 2026 batch! Applications are open till Nov 10

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: