clojure的Var和Namespace在内存中的布局

Clojure中,Var和Namespace的内存布局。

Layout of Var

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/** Layout of Var:

每个线程都有自己的一套形如下图的Var存储布局,即这套内容是ThreadLocal的。

ThreadLocal<Frame> dvals
|
TOP v
+----------+ +----------+ +----------+
| null | <---- | prev | <--- .... <--- | prev |
+----------+ +----------+ +----------+
| bindings | | bindings | | bindings |
+----------+ +----------+ +----------+
| | |
| | |
v v v
PersistentHashMap.EMPTY +-------+--------+ +-------+--------+
| var_0 | thread | | var_0 | thread |
| | val_0 | | | val_0 |
+-------+--------+ +-------+--------+
| var_1 | thread | | var_1 | thread |
| | val_1 | | | val_1 |
+-------+--------+ +-------+--------+
| var_2 | thread | | var_2 | thread |
| | val_2 | | | val_2 |
+-------+--------+ +-------+--------+
| ....... | | var_3 | thread |
+----------------+ | | val_3 |
+-------+--------+
| ....... |
+----------------+
*/

Layout of Namespace

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/** Layout of Namespace:

namespace 是全局的,mappings, aliases是依附于单独Namespace对象的。

mappings

map: Symbol -> Var
+-----> +--------+---------+
| | quote | Var obj |
| +------------------+
| | vector | Var obj |
| +------------------+
| | cons | Var obj |
| +--------+---------+
namespace | | . . . . . . |
| +------------------+
map: Symbol -> Namesapce |
+--------------+---------------+ |
| clojure.core | Namespace ojb | ----+ +--> +--------+---------+
+--------------+---------------+ | | var_0 | Var obj |
| user | Namespace obj | -------+ +--------+---------+
+--------------+---------------+ | var_01 | Var obj |
| | +--------+---------+
| . . . . . . | | var_02 | Var obj |
| | +--------+---------+
+--------------+---------------+ | . . . . . . |
| other nsname | Namespace obj | ------+ +------------------+
+--------------+---------------+ |
+---> +--------+---------+
| var_0 | Var obj |
+--------+---------+
| var_01 | Var obj |
+--------+---------+
| var_02 | Var obj |
+--------+---------+
| . . . . . . |
+------------------+
aliases

map: Symbol -> Namesapce
+--------------+---------------+
| alias_0 | Namespace ojb |
+--------------+---------------+
| alias_1 | Namespace obj |
+--------------+---------------+
| |
| . . . . . . |
| |
+--------------+---------------+
| alias_n | Namespace obj |
+--------------+---------------+

*/