Feature #6763


Introduce Flonum technique to speedup floating computation on th 64bit environment

Added by ko1 (Koichi Sasada) about 10 years ago. Updated almost 10 years ago.

Target version:



= Abstract

Introducing Flonum technique, which is similar to Fixnum against Integer, to speedup floating number calculation on the 64bit CPU.

On our measurements, we can achieve x2 performance improvement for simple floating calculation.

= Background

Ruby (CRuby) have a Float class to achieve floating number calculation. However, the Float object is allocated as object every time.

For example,

x = 0.0
a = b = 1.2
x += (a + b)

It create over 20 floating number objects.
(a+b) creates one Float object and x+=(a+b) create one more.
This is a one of the biggest reasons why floating number calculation is slow using (C)Ruby.

On the other hands, Fixnum calculation doesn't make new object.

x = 0
a = b = 1
x += (a + b)

No object allocated by the above code because Fixnum representation doesn't need object allocation (immediate value).

= Proposal

Introducing Flonum technique for Float such as Fixnum for
Integer on the 64bit CPU enviroment.
A limited ranged Float object can be represented similar way of
Fixnum (immediate object).

Accuracy of the floating calculation is not affected.

We already have tried preliminary implementation and evaluation [1].
In article [1], I describe a technique to introduce Flonum into
CRuby on 64bit CPU environment.

Key idea of our technique is represent a small Float object (mantissa is small enough)
in special bit pattern.
Otherwise, a big float numbers are represented by current object representation.

[1] A Lightweight Representation of Floting-Point Numbers on Ruby Interpreter
Sorry, they are written in Japanese.
Note that we need to change the technique described in this article because proposed technique uses the Fixnum bit pattern for Flonum.

On our implementation, class of Flonum object is Float class.
It is different relation from the relation between Fixnum and Integer.
I think it is a point to discuss.

I heard that MacRuby has similar optimization.

= Compatibility issue

(1) A result of float calculation always return different object if it is same result

a = 1.1 + 1.2
b = 1.1 + 1.2
p(a.object_id == b.object_id) #=> false

After introducing flonum, it will be same objects.

(2) Breaking binary compatibility for C extension

Now, Float object is always `struct RFloat' data type.
After introducing Flonum, the assumption will be break.


Related issues 1 (0 open1 closed)

Related to Ruby master - Feature #6936: Forbid singleton class and instance variabls for floatClosedko1 (Koichi Sasada)08/27/2012Actions

Also available in: Atom PDF