[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

# 10. Internals

10.1 Why C++ ? |  |
10.2 Memory efficiency |  |
10.3 Speed efficiency |  |
10.4 Garbage collection |  |

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 10.1 Why C++ ?

Using C++ as an implementation language provides

- Efficiency: It compiles to machine code.

- Portability: It runs on all platforms supporting a C++ compiler. Because of the availability of GNU C++, this includes all currently used 32-bit and 64-bit platforms, independently of the quality of the vendor's C++ compiler.

- Type safety: The C++ compilers knows about the number types and complains if, for example, you try to assign a float to an integer variable. However, a drawback is that C++ doesn't know about generic types, hence a restriction like that `operator+ (const cl_MI&, const cl_MI&)` requires that both arguments belong to the same modular ring cannot be expressed as a compile-time information.

- Algebraic syntax: The elementary operations `+`, `-`, `*`, `=`, `==`, ... can be used in infix notation, which is more convenient than Lisp notation ``(+ x y)`' or C notation ``add(x,y,&z)`'.

With these language features, there is no need for two separate languages, one for the implementation of the library and one in which the library's users can program. This means that a prototype implementation of an algorithm can be integrated into the library immediately after it has been tested and debugged. No need to rewrite it in a low-level language after having prototyped in a high-level language.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 10.2 Memory efficiency

In order to save memory allocations, CLN implements:

- Object sharing: An operation like `x+0` returns `x` without copying it.
- Garbage collection: A reference counting mechanism makes sure that any number object's storage is freed immediately when the last reference to the object is gone.
- Small integers are represented as immediate values instead of pointers to heap allocated storage. This means that integers `> -2^29`, `< 2^29` don't consume heap memory, unless they were explicitly allocated on the heap.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 10.3 Speed efficiency

Speed efficiency is obtained by the combination of the following tricks and algorithms:

- Small integers, being represented as immediate values, don't require memory access, just a couple of instructions for each elementary operation.
- The kernel of CLN has been written in assembly language for some CPUs (`i386`, `m68k`, `sparc`, `mips`, `arm`).
- On all CPUs, CLN may be configured to use the superefficient low-level routines from GNU GMP version 3.
- For large numbers, CLN uses, instead of the standard `O(N^2)` algorithm, the Karatsuba multiplication, which is an `O(N^1.6)` algorithm.
- For very large numbers (more than 12000 decimal digits), CLN uses Schnhage-Strassen multiplication, which is an asymptotically optimal multiplication algorithm.
- These fast multiplication algorithms also give improvements in the speed of division and radix conversion.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 10.4 Garbage collection

All the number classes are reference count classes: They only contain a pointer to an object in the heap. Upon construction, assignment and destruction of number objects, only the objects' reference count are manipulated.

Memory occupied by number objects are automatically reclaimed as soon as their reference count drops to zero.

For number rings, another strategy is implemented: There is a cache of, for example, the modular integer rings. A modular integer ring is destroyed only if its reference count dropped to zero and the cache is about to be resized. The effect of this strategy is that recently used rings remain cached, whereas undue memory consumption through cached rings is avoided.

[ << ] | [ >> ] |  |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

This document was generated by Richard B. Kreckel on October, 22 2005 using texi2html