The grid over time

Changing the grid

addObject(x, y, z) adds an object and returns its index, one past the highest ever issued; the point must lie in the box, which is never refitted. removeObject(index) removes an object, whose index then remains invalid for the life of the grid. On the grid of getting started, with nearest the object nearest the centre:

grid.remove_object(nearest)
print(f"removed object {nearest}: {len(grid)} objects left, "
      f"is_alive({nearest}) = {grid.is_alive(nearest)}")

added = grid.add_object(0.5, 0.5, 0.5)

Neither call releases memory: a removed object’s storage remains allocated, and a cell that fills up moves its objects to a larger block, leaving the old one unused. Additions and removals suit occasional changes. To add a large share of the objects, build a new grid from the full set of coordinates: additions do not compact the storage, and the new grid is compact.

Memory

What a grid allocates is determined by the number of objects and of cells. Each object costs 37 bytes: its coordinates and the bookkeeping that maps indices to storage. Each cell costs 48 bytes on a grid with no periodic axis: 16 for the cell and 32 for its share of a table of cell offsets that the queries walk. Along a periodic axis the table needs fewer entries, so a grid periodic along all three axes spends about 20 bytes per cell. These figures hold on grids of a million cells or more; on smaller grids a cell costs somewhat more. At one mean separation there are about as many cells as objects: at ten million objects, 353 MiB for the objects and 461 MiB for the cells, about 0.8 GiB; at two mean separations there are an eighth as many cells. Empty cells cost the same as full ones, which matters on clustered data.

Construction allocates nothing beyond the grid itself, and the process grows by no more than what the grid allocates (performance page). In Python, a C-contiguous float64 array, numpy’s default, or any other contiguous float64 buffer is read where it is. Any other input is converted first, which costs a temporary 8 bytes per object for each converted coordinate array: a float32 array, a list, or the columns of an (N, 3) array, which are strided; pass such an array whole, as MeshGrid(pos, cellsize), to read it in place. The copy is freed when the constructor returns, but the allocator may keep the memory in the process.

Reproducibility

The same input gives the same results, in the same order, with the same version of the library, on Linux and macOS, on x86-64 and ARM, with any number of threads and of workers. The same input means the same coordinates in the same order, the same cell side and options, the same sequence of additions and removals, and the same queries. This covers the order of the indices closeObjects returns, the order of objects within a cell that get_objectsInCell reports, and every batch; a batch’s result equals that of its single queries. A copy of a grid answers every query as the original does, in the same order.

The order of the objects within a cell, and therefore the order of the results of closeObjects and get_objectsInCell, is not part of the interface: it may change between versions, and any such change is announced in the changelog. Code that requires a particular order should sort the results.

Copies and the version

A grid is copied by its copy constructor and assignment in C++, and by copy.copy and copy.deepcopy in Python. A copy is deep and independent: changing one grid leaves the other unchanged. Moves in C++ transfer the storage, and leave the source fit only to be destroyed or assigned to. A default-constructed C++ grid holds nothing: every query raises or returns empty until a grid is assigned to it.

Grids cannot be saved to a file, and a Python grid cannot be pickled. To use a grid in several processes, rebuild it in each from the coordinates, or use threads, which share one grid.

meshsearch.__version__ in Python, and MESHSEARCH_VERSION in C++ from the header meshsearch/Version.h, give the version the library was built as; the header also defines MESHSEARCH_VERSION_MAJOR, _MINOR and _PATCH.