Debian Codegraph @

docs/source/performance.rst

raw
1 .. _performance:
2
3 Performance
4 ------------------------------------------------------------------------------
5
6 See the `benchmarks.py`_ file for a comparison of various query methods
7 and how much acceleration can be obtained from using Rtree.
8
9 .. _benchmarks.py: https://github.com/Toblerity/rtree/blob/main/benchmarks/benchmarks.py
10
11 There are a few simple things that will improve performance.
12
13 Use stream loading
14 ..............................................................................
15
16 This will substantially (orders of magnitude in many cases) improve
17 performance over :py:meth:`~rtree.index.Index.insert` by allowing the data to
18 be pre-sorted
19
20 .. code-block:: pycon
21
22 >>> from rtree import index
23 >>> def generator_function(somedata):
24 ... for i, obj in enumerate(somedata):
25 ... yield (i, (obj.xmin, obj.ymin, obj.xmax, obj.ymax), obj)
26 ...
27 >>> r = index.Index(generator_function(somedata)) # doctest: +SKIP
28
29 After bulk loading the index, you can then insert additional records into
30 the index using :py:meth:`~rtree.index.Index.insert`
31
32 Override :py:data:`~rtree.index.Index.dumps` to use the highest pickle protocol
33 ...............................................................................
34
35 .. code-block:: pycon
36
37 >>> import pickle
38 >>> import rtree
39 >>> class FastRtree(rtree.Rtree):
40 ... def dumps(self, obj):
41 ... return pickle.dumps(obj, -1)
42 ...
43 >>> r = FastRtree()
44
45 .. topic:: Update from January 2024
46
47 Pickling is currently broken and awaiting a pull request to fix it.
48 For more information, see the `pull request on GitHub`_.
49
50 .. _pull request on GitHub: https://github.com/Toblerity/rtree/pull/197
51
52 Use objects="raw"
53 ...............................................................................
54
55 In any :py:meth:`~rtree.index.Index.intersection` or
56 :py:meth:`~rtree.index.Index.nearest` or query, use ``objects="raw"`` keyword
57 argument:
58
59 .. code-block:: pycon
60
61 >>> xmin, ymin, xmax, ymax = 0.0, 0.0, 1.0, 1.0
62 >>> objs = r.intersection((xmin, ymin, xmax, ymax), objects="raw")
63
64
65 Adjust index properties
66 ...............................................................................
67
68 Adjust :py:class:`rtree.index.Property` appropriate to your index.
69
70 * Set your :py:data:`~rtree.index.Property.leaf_capacity` to a higher value
71 than the default 100. 1000+ is fine for the default pagesize of 4096 in
72 many cases.
73
74 * Increase the :py:data:`~rtree.index.Property.fill_factor` to something
75 near 0.9. Smaller fill factors mean more splitting, which means more
76 nodes. This may be bad or good depending on your usage.
77
78 Limit dimensionality to the amount you need
79 ...............................................................................
80
81 Don't use more dimensions than you actually need. If you only need 2, only use
82 two. Otherwise, you will waste lots of storage and add that many more floating
83 point comparisons for each query, search, and insert operation of the index.
84
85 Use the correct query method
86 ...............................................................................
87
88 Use :py:meth:`~rtree.index.Index.count` if you only need a count and
89 :py:meth:`~rtree.index.Index.intersection` if you only need the ids.
90 Otherwise, lots of data may potentially be copied. If possible also
91 make use of the bulk query methods suffixed with `_v`.