Friday, February 19, 2010

Could we reuse Garbage collector of V8 javascript engine?

V8 javascript engine is well optimized, and use an efficient way to manage memory.
And I wonder if I can use the garbage collector easily in other projects? and to answer to this question we have to talk about coupling metrics.

There is a whole range of interesting code metrics relative to coupling. The simplest ones are named Afferent Coupling (Ca) and Efferent Coupling (Ce). Basically, the Ca for a code element is the number of code elements that use it and the Ce is the number of code elements that it uses.



You can define Ca and Ce for the graph of projects dependencies, the graph of namespaces dependencies, the graph of types dependencies and the graph of methods dependencies of a code base. You can also define the Ca metric on the fields of a program as the number of methods that access the field. This leads to 9 metrics all supported by the tool CppDepend We precise that when computing Ce.

With CppDepend, if you wish to know which methods of your program are massively used you can write the following CQL Query :

SELECT TOP 10 METHODS ORDER BY MethodCa DESC

Being used a lot is not necessarily a problem. However it is still interesting to know which part of your code base is used a lot.

High Efferent Coupling and design flaws

If you wish to know which types of your program are heavy users of other types you just have to write:

SELECT TOP 10 TYPES ORDER BY TypeCe DESC

High Ce might reveal a design problem. Types with high Ce are entangled with many other implementations. The higher the Ce, the higher the number of responsibilities the type has.


What about reusing garbage collector?


The class representing the garbage collector is MarkCompactCollector, this class has a TypeCe equal to 34, but how many types this class use indirectly.

SELECT TYPES WHERE IsUsedBy "v8.internal.MarkCompactCollector"

And there’s the result



And the metric view shows better relation between MarkCompactCollector class and other V8 types:




So it’s very difficult to isolate only the garbage collector mechanism from V8 source code, Maybe in the future the V8 team will isolate this garbage collector, and it will be used in other projects.

No comments:

Post a Comment