The gt file format#

The gt format is the binary file format of graph-tool. It stores a Graph together with all its property maps, of every value type supported by the library, in a compact form that is fast to read and write. Files with the extension .gt are read by load_graph() and written by save(), and the extensions .gt.gz, .gt.bz2, .gt.xz, and .gt.zst add a compression with gzip, bzip2, xz, or zstd, respectively. In the following we describe version 2 of the format, and use as an example the network of co-appearances of the characters of the novel Les Misérables (lesmis), as included in the graph_tool.collection module. We list the differences between the versions in Sec. Version history, and compare the format with GraphML in Sec. Performance.

Overview#

A file consists of three consecutive parts, without padding between them:

Part

Contents

Header

Magic string, version, byte order, and comment

Adjacency

Directedness, number of vertices, and the out-neighbors of each vertex

Property maps

Number of property maps, and for each of them its key type, name, value type, and values

All integers have a fixed size of 1, 2, 4, or 8 bytes, according to their types uint8_t, int16_t, int32_t, and int64_t or their unsigned counterparts, and all sizes and counts are of type uint64_t. A flag in the header gives the byte order of every value of more than one byte that follows it, including the length of the comment. When writing, graph-tool uses the byte order of the machine, and when reading, it swaps the bytes of files with the other byte order. Values of type double are IEEE 754 binary64 numbers. Values of type long double occupy 16 bytes in the representation of the machine that wrote them, i.e. the 80-bit extended precision format in the first 10 bytes on x86-64, so that they are not portable between machines with different representations. Strings are stored as their length followed by their bytes, without a terminating null byte, and vectors as their number of elements followed by the elements. The format imposes no encoding on strings, but graph-tool uses UTF-8.

Adjacency#

After the header we find the adjacency, which begins with a byte that indicates whether the graph is directed (0x01) or undirected (0x00), followed by the number of vertices N. The vertices are identified by their indices, from 0 to N - 1. For each of them, in the order of the indices, follows the vector of its out-neighbors, given by their indices. These indices have the smallest of the types uint8_t, uint16_t, uint32_t, and uint64_t whose maximum value is not smaller than N, i.e. one byte for N ≤ 255, two bytes for N ≤ 65535, and so on.

Each edge appears only once. An edge between the vertices u and v of an undirected graph therefore appears either in the list of u or in the list of v, since otherwise it would be read as two parallel edges. Self-loops and parallel edges are allowed, and the order in which the edges appear determines the order of the values of the edge property maps. In total, we need 1 + 8 + 8 N + d E bytes for the adjacency, where E is the number of edges and d the size of the indices.

In the lesmis network, we find the adjacency at offset 0xa7, in the bytes

000000a0  65 20 70 72 6f 70 73 00  4d 00 00 00 00 00 00 00  |e props.M.......|
000000b0  00 00 00 00 00 00 00 00  01 00 00 00 00 00 00 00  |................|
000000c0  00 01 00 00 00 00 00 00  00 00 02 00 00 00 00 00  |................|
000000d0  00 00 00 02 01 00 00 00  00 00 00 00 00 01 00 00  |................|

where the first byte, 0x00, indicates an undirected graph with N = 0x4d = 77 vertices, so that the indices have one byte. Vertex 0 has no out-neighbors, since its edges appear in the lists of the other vertices. Vertices 1 and 2 have vertex 0 as their only out-neighbor, and vertex 3 has the out-neighbors 0 and 2.

Property maps#

After the adjacency we find the number of property maps, followed by the property maps themselves. Each property map consists of 1. its key type (uint8_t), equal to 0x00 for a graph property map, 0x01 for a vertex property map, and 0x02 for an edge property map; 2. its name, as a string; 3. its value type (uint8_t), according to the table below; and 4. its values. A graph property map has one value, a vertex property map has N values in the order of the vertex indices, and an edge property map has E values in the order in which the edges appear in the adjacency.

Value types of the property maps#

Value type

Bytes per value

Index

bool

1

0x00

int16_t

2

0x01

int32_t

4

0x02

int64_t

8

0x03

double

8

0x04

long double

16

0x05

string

8 + length

0x06

vector<bool>

8 + length

0x07

vector<int16_t>

8 + 2 * length

0x08

vector<int32_t>

8 + 4 * length

0x09

vector<int64_t>

8 + 8 * length

0x0a

vector<double>

8 + 8 * length

0x0b

vector<long double>

8 + 16 * length

0x0c

vector<string>

8 + the sizes of the strings

0x0d

python::object

8 + length

0x0e

Values of type bool are the bytes 0x00 and 0x01, and the elements of values of type vector<string> are strings. Values of type python::object are strings with the pickle of the objects, as we describe in Sec. Python objects. The vertex and edge index maps (vertex_index and edge_index) are stored as values of type int64_t, and are read back as ordinary property maps.

In the lesmis network, the property maps begin at offset 0x416, as we see in the excerpt

00000410  41 42 3f 3e 30 3a 05 00  00 00 00 00 00 00 00 0b  |AB?>0:..........|
00000420  00 00 00 00 00 00 00 64  65 73 63 72 69 70 74 69  |.......descripti|
00000430  6f 6e 06 24 01 00 00 00  00 00 00 4c 65 73 20 4d  |on.$.......Les M|
00000440  69 73 65 72 61 62 6c 65  73 3a 20 63 6f 61 70 70  |iserables: coapp|

where the first of the five property maps is a graph property map (0x00) named description (0x0b = 11 bytes), of type string (0x06), with a value of 0x124 = 292 bytes. In total, we obtain a file of 7094 bytes.

Python objects#

Values of type python::object are stored as strings with their pickle, made with the highest pickle protocol available to the writer. In version 2, a single pickler processes all these values of a file, in the order in which they appear, so that they share its memo. Each value is then a complete pickle, from the PROTO to the STOP opcode, but it may refer to objects pickled in previous values, of the same or of other property maps. An object that appears in several values is thus stored only once and restored as a single object, and references between values are preserved, including cycles. For this reason, a reader must unpickle all these values with a single unpickler, in the order of the file. This includes the values of property maps that it discards, such as those given by the ignore_vp, ignore_ep, and ignore_gp parameters of load_graph().

Warning

The pickle protocol identifies memoized objects by their position in the memo. A value of a version 2 file that is unpickled on its own therefore does not necessarily raise an error, but may silently produce wrong objects. This is why version 2 has a different number than version 1, where each value is an independent pickle.

We may also store values that refer to the graph being saved, e.g. a graph property map whose value is the graph itself, or a value that contains a property map of the graph. Such a reference cannot be pickled as the graph, since the graph contains the reference again. Instead, it is pickled as a placeholder that is replaced, when the file is read, by the graph being loaded. Since a graph pickled within the property values of another graph is itself saved in the gt format, the placeholder also records which of the nested graphs being loaded it refers to. A reference to the base graph of a GraphView being saved is pickled in the same way, and restored as a reference to the loaded graph. A property map or graph view contained in a value is restored as a new property map or graph view of the loaded graph, with the same values.

Warning

Unpickling can execute arbitrary code. Files with property maps of type python::object should therefore only be loaded from trusted sources.

As an example, we save an undirected graph with two vertices and one edge, together with a vertex property map named obj whose two values are the same list, ["shared"]. Omitting the lines of the comment, we obtain the version 2 file

00000000  e2 9b be 20 67 74 02 00  94 00 00 00 00 00 00 00  |... gt..........|
          ...
000000a0  72 6f 70 73 00 02 00 00  00 00 00 00 00 01 00 00  |rops............|
000000b0  00 00 00 00 00 01 00 00  00 00 00 00 00 00 01 00  |................|
000000c0  00 00 00 00 00 00 01 03  00 00 00 00 00 00 00 6f  |...............o|
000000d0  62 6a 0e 18 00 00 00 00  00 00 00 80 05 95 0d 00  |bj..............|
000000e0  00 00 00 00 00 00 5d 94  8c 06 73 68 61 72 65 64  |......]...shared|
000000f0  94 61 2e 05 00 00 00 00  00 00 00 80 05 68 00 2e  |.a...........h..|

where the adjacency begins at offset 0xa4: the graph is undirected, with N = 2, vertex 0 has the out-neighbor 1, and vertex 1 has none. Then follow the number of property maps, 1, and the property map, with the key type 0x01 (vertex), the name obj, and the value type 0x0e (python::object). The first value, of 0x18 = 24 bytes, is the pickle of the list. The second value, of 5 bytes, only points to the list in the memo, with the opcodes PROTO 5 (80 05), BINGET 0 (68 00), and STOP (2e). When we load the file, both values are therefore the same object, as we can verify by running

>>> g = gt.Graph(directed=False)
>>> vs = g.add_vertex(2)
>>> e = g.add_edge(0, 1)
>>> g.vp.obj = g.new_vp("object")
>>> x = ["shared"]
>>> g.vp.obj[0] = x
>>> g.vp.obj[1] = x
>>> g.save("shared.gt")
>>> h = gt.load_graph("shared.gt")
>>> h.vp.obj[0] is h.vp.obj[1]
True

Reserved property maps#

Besides its structure, a graph has a state: its vertex and edge filters (see GraphView), whether it is reversed, and whether fast edge removal and lookup are enabled. save() stores this state as property maps with reserved names, and load_graph() restores and removes them:

Name

Key type

Value

_Graph__save__vfilter

vertex

The vertex filter (bool)

_Graph__save__efilter

edge

The edge filter (bool)

_Graph__reversed

graph

Whether the graph is reversed (bool)

_Graph__fast_edge_removal

graph

Whether fast edge removal is enabled (bool)

_Graph__fast_edge_lookup

graph

Whether fast edge lookup is enabled (bool)

The filters are stored only if save() is called with preserve_filter=True (the default), in which case the adjacency contains all vertices and edges of the graph, including those that are filtered out. Otherwise, it contains only the vertices and edges that are not filtered out, with the vertices numbered consecutively in the order of their indices.

Version history#

We list the versions of the format, together with the versions of graph-tool that introduced them, in the following table:

Version

Introduced in

Changes

1

graph-tool 2.2.32

Initial version.

2

graph-tool 4.0

The values of type python::object are pickled with a memo that is shared by the whole file, and references to the graphs that are being saved are pickled as placeholders for the graphs being loaded (see Sec. Python objects).

Since versions 1 and 2 differ only in the pickling of the values of type python::object, graph-tool writes files without such property maps as version 1, so that they can be read by older versions. Files with them are written as version 2, and both versions are read.

Performance#

The lesmis network has 7094 bytes in the gt format, compared to 35689 bytes in GraphML, and 2836 compared to 4452 bytes with xz compression. The difference grows for larger graphs. For the strongly connected component of the PGP web of trust (pgp_strong), with 39796 vertices and 301498 edges, and without its property maps, we obtain 921552 bytes in the gt format and 21324583 bytes in GraphML, or 408504 and 1444432 bytes with xz. On our machine, the graph is written in 1.7 ms in the gt format, compared to 32 ms in GraphML, and read in 12 ms, compared to 143 ms. We measured these values with the following code, whose timings depend on the machine:

>>> import timeit
>>> g = gt.collection.data["pgp-strong-2009"].copy()
>>> g.properties.clear()   # Only the structure of the graph is compared.
>>> timeit.Timer(lambda: g.save("/tmp/pgp_graph.xml")).timeit(number=1)
0.0319...
>>> timeit.Timer(lambda: g.save("/tmp/pgp_graph.xml.xz")).timeit(number=1)
0.4072...
>>> timeit.Timer(lambda: g.save("/tmp/pgp_graph.gt")).timeit(number=1)
0.0017...
>>> timeit.Timer(lambda: g.save("/tmp/pgp_graph.gt.xz")).timeit(number=1)
0.0637...
>>> timeit.Timer(lambda: gt.load_graph("/tmp/pgp_graph.xml")).timeit(number=1)
0.1428...
>>> timeit.Timer(lambda: gt.load_graph("/tmp/pgp_graph.xml.xz")).timeit(number=1)
0.1939...
>>> timeit.Timer(lambda: gt.load_graph("/tmp/pgp_graph.gt")).timeit(number=1)
0.0118...
>>> timeit.Timer(lambda: gt.load_graph("/tmp/pgp_graph.gt.xz")).timeit(number=1)
0.0223...
>>> import subprocess
>>> print(subprocess.check_output("du -b /tmp/pgp_graph* | sort -n", shell=True).decode("utf-8"))
408504       /tmp/pgp_graph.gt.xz
921552       /tmp/pgp_graph.gt
1444432      /tmp/pgp_graph.xml.xz
21324583     /tmp/pgp_graph.xml