Those outside of the ruby community (and some inside) oft reference ruby applications as being slow. For many of us this is an irrelevance, IO bound operati开发者_Python百科ons etc. However, when it does become a problem there is little to hold us back from taking advantage of native code to speed things up. To this end I am wondering why RoR (itself the target of many 'slow' comments) doesn't make use of any native elements to speed itself up?
Is there a particular reason? Is there a lack of tight loops to optimise within the codebase?
Rails takes advantage of "native", aka compiled, extensions, but it keeps them as separate and optional libraries.
For instance, Rails lets you use nokogiri
as an XML parser, instead of the standard Ruby-based parser.
There are at least 3 reasons why Rails probably won't replace internal functions with C/C++ equivalents.
- Rails is a Ruby framework. Because it's a Ruby framework, you can expect contributors to know about Ruby, but by using C libraries you force the entire Ruby ecosystem to also be C programmers. And this would probably mean a smaller number of contributors to the framework itself.
- If you ever tried to install on Windows a Gem which includes C extensions, then you probably already know why it's a bad idea to use C code within Rails.
- Rails happily runs on standard Ruby and the most part of alternative Ruby distributions, such as JRuby. Using C code in Ruby, would require to provide Java/Python/... counterparts of the same code. In fact, Gems with embedded C extensions currently are not compatible with JRuby.
it does use them, if you install them as gems (the mysql gem, memcache gem, RedCloth, etc.)
In general, though, it relies on the VM to optimize the code. Otherwise it would be hard for it to work on all the platforms ruby does.
The standard library Date class has been found to be the bottleneck in some Ruby applications. Jeremy Evans has implemented the Date/DateTime classes in C and got a 20-200x performance gain.
home_run
精彩评论