Fastruby IS NOT a separated ruby interpreter. Then, the design is simple
Fastruby IS NOT a DSL to generate C code using ruby or a Ruby to C translator, the goal of fastruby is to execute RUBY code
Core Concepts
Native build
All code processed by fastruby ends with a native representation, in the current version, this is acomplished using RubyParser to parse rubycode.
The ruby code is translated to C and then processed with RubyInline
Transparent multimethods (and multiblocks)
The methods processed by fastruby has multiple internal implementations depending on the type of the arguments.
Each possible signature has a version of the method and this are built in runtime when the method is called with a new signature.
The same concept will be applied to blocks (anonymous methods) in future releases
Type inference
Each version of a method is built for a specific signature, so, the builder can assume a type for the arguments and build method calls using that assumption.
Whereever the translator can asume a type for a expression involved in a method call (used as argument or as receiver), this information can be used to encode
direct calls instead of normal and expensive ruby calls.
The currently implementation only can infer types for method and block arguments, and for literals
Customization through build directives and API
To compensate for the described limitations, fastruby suport a few build directives to allow the programmer help the inference.
The syntaxis of these directives are the same as normal ruby call (see examples)
Also, fastruby will define a API to customize aspects of fastruby internals. E.g the build method to invoke the build of methods with a specific signature (see examples)
The Image
The image of this post shows a display with the execution of the current benchmarks of the test
All of them follow the pattern of executing the code with normal ruby and with fastruby. The fourth benchmark adds a few additional measures (see the benchmark source for more info)
You can locate the benchmark sources installed in the gem directory of fastruby or in the github repository and the full resolution version of the image here
Installation
The install is as simple as execute the well-known gem install:
sudo gem install fastruby
Examples
The syntax is simple since one of the main of goals of fastruby is to be transparent. The current API serves for testing and customization of this poc version of fastruby
Prebuild of methods:
require "fastruby"NOTE: this is not necessary to do this, the methods will built automatically when there are called for first time
class X
fastruby '
def foo(a,b)
a+b
end
'
end
X.build([X,String,String] , :foo)
p X.new.foo("fast", "ruby") # will use the prebuilded method
p X.new.foo(["fast"], ["ruby"]) # will build foo for X,Array,Array signature and then execute it
Variable "types"
Like static languages, you can define a type for a variable to help the inference and gain some performance. This can be done by using lvar_type directive
class X
fastruby '
def foo
lvar_type(i, Fixnum)
i = 100
while (i > 0)
i = i - 1
end
nil
end
'
end
With no lvar_type, the calls to Fixnum#> and Fixnum#- will be dynamic and more expensive
Links
- Github page: https://github.com/tario/fastruby
No hay comentarios:
Publicar un comentario