Skip to content

Commit

Permalink
Alternate node store (#590)
Browse files Browse the repository at this point in the history
* refactor NodeStore

I'd like to add an alternative NodeStore that can be used when the
`Type_then_ID` property is present in the PBF.

First, a small (?) refactor:

- make `NodeStore` an interface, with two concrete implementations
- extract the NodeStore related things to their own files
- this will cause some churn, as they'll depend on things that also
  need to get extracted to their own files. Short term pain, hopefully
  long term gain in faster compile times.

Changing the invocations of the functions to be virtual may have impact
on performance. Will need to revisit that before committing to virtual
methods.

* change how work is assigned for ReadPhase::Nodes

Currently, when a worker needs work, it gets the next unprocessed block.
This means blocks are read sequentially at a global level, but from
the perspective of each worker, there are gaps in the blocks they see.

For nodes, we'd prefer to give each worker thread contiguous blocks
from the underlying PBF. This will enable a more efficient storage
for PBFs with the `Sort.Type_then_ID` flag.

* add SortedNodeStore

SortedNodeStore is uesful for PBFs with the `Sort.Type_then_ID`
property, e.g. the planet and Geofabrik exports.

It stores nodes in a hierarchy:

- Level 1 is groups: there are 256K groups
- Level 2 is chunks: each group has 256 chunks
- Level 3 is nodes: each chunk has 256 nodes

This allows us to store 2^34 nodes, with a fixed overhead of
only 2M -- the space required for the level 1 pointers.

Groups and chunks store their data sparsely. If a group has 7 chunks,
it only uses storage for 7 chunks.

On Great Britain's 184M node PBF, it needs ~9.13 bytes per node.

Looking up a node can be done in fixed time:

First, get some offsets:
- Group: `nodeID / 65536`
- Chunk: `(nodeID / 65536) / 256`
- Position within chunk: `nodeID % 256`

For example, Cape Chignecto Provincial Park has ID 4855703, giving:
- Group 74
- Chunk 23
- Offset 151

Group 74's chunks may be sparse. To map chunk 23 to its physical
location, each group has a 256-bit bitmask indicating which
chunks are present.

Use its physical location to get its `chunkOffset`. That allows you
to get to the `ChunkInfo` struct.

From there, do the same thing to get the node data.

This design should also let us do some interesting things down the road,
like efficiently compressing each chunk using something like delta
encoding, zigzag encoding and bit packing. Then, to avoid paying a
decompression cost, we'd likely give each worker a cache of uncompressed
chunks.

* cmake build

* tidy up

* tweak

* tweak

* derp

* mac/windows build

* fix build?

I don't understand why these can't be passed as a copy in the Windows
and Mac builds. Whatever, try passing a reference.

* fix --store

I think nested containers may not be wired up quite correctly.
Instead, manage the char* buffers directly, rather than as
`std::vector<char>`

I'll fixup the other aspects (attributing libpopcnt, picking
Sorted vs BinarySearch on the fly) later

* attribution for libpopcnt

* simplify read_pbf

All read phases use the same striding-over-batches-of-blocks approach.

This required changing how progress is reported, as block IDs are no
longer globally montonically increasing.

Rather than thread the state into ReadBlock, I just adopted 2 atomic
counters for the whole class -- the progress reporter already assumes
that it's the only thing dumping to stdout, so the purity of avoiding
class-global doesn't buy us anything.

* clear allocatedMemory

* use scale factor 16, not 8

D'oh, if you get a full group where each chunk is full, you need to be
able to express a value _ever so slightly_ larger than 65,536.

North America and Europe have examples of this.

Use a scale factor of 16, not 8. This'll mean some chunks have up to 15
wasted bytes, but it's not a huge deal. (And I have some thoughts on how
to claw it back.)

* comment out debug stats

* windows build

* derp

* use SortedNodeStore if PBFs have Sort.Type_then_ID

* add --compress-nodes

If the user passes `--compress-nodes`, we use [streamvbyte](https://github.com/lemire/streamvbyte)
to compress chunks of nodes in memory.

The impact on read time is not much:
- GB with `--compress-nodes`: 1m42s
- without: 1m35s

But the impact on memory is worthwhile, even across very different
extracts:

North America - 5.52 bytes/node vs 8.48 bytes/node
169482 groups, 18364343 chunks, 1757589784 nodes, needed 9706167278 bytes
169482 groups, 18364343 chunks, 1757589784 nodes, needed 14916095182 bytes

Great Britain - 5.97 bytes/node vs 9.25 bytes/node
163074 groups, 4871807 chunks, 184655287 nodes, needed 1104024510 bytes
163074 groups, 4871807 chunks, 184655287 nodes, needed 1708093150 bytes

Nova Scota - 5.81 bytes/node vs 8.7 bytes/node
26777 groups, 157927 chunks, 12104733 nodes, needed 70337950 bytes
26777 groups, 157927 chunks, 12104733 nodes, needed 105367598 bytes

Monaco - 10.43 bytes/node vs 13.52 bytes/node
1196 groups, 2449 chunks, 30477 nodes, needed 318114 bytes
1196 groups, 2449 chunks, 30477 nodes, needed 412258 bytes

* build

* build

* remove __restrict__ to satisfy windows build

* remove debug print, small memory optimization

* use an arena for small groups

* omit needless words

* better short-circuiting for Type-then-ID PBFs

Track metadata about which blocks have nodes, ways and relations.
By default, we assume any block may contain nodes, ways or relations.

If the PBF supports Type-then-ID PBFs, do a binary search to find the first
blocks with ways and relations.

This means ReadPhase::Nodes can stop without scanning ways/relations.
In addition to avoiding needless work, it makes it easier to assign
each worker a balanced amount of work -- now each worker has only
blocks with nodes, which are about the same effort computationally.

It also makes ReadPhase::ScanRelations faster, as it scans exactly the
blocks with relations, skipping the blocks with ways.

Similarly, ReadPhase::Ways is a bit faster, as it doesn't have to read
the blocks with relations.

For North America, this reduces the time to complete the Nodes and
RelationsScan phase from 2m30s to 1m20s.

For GB, it reduces the time from 22s to 9s.

* ReadPhase::Relations - more parallelism

When processing relations for small extracts, there are often fewer
blocks than cores.

Instead, divide the work more granularly, assigning each of the N
threads 1/Nth of the block to process.

This saves 4-5 seconds (which is cumulatively ~20% of runtime) for
the Canadian province of Nova Scotia.

* extract WayStore, BinarySearchWayStore

* stub in SortedWayStore

...it just throws a lot of exceptions at the moment.

* put SortedNodeStore in a namespace

Also replace some `#define`s with `const`s.

I'm likely going to reuse some names in SortedWayStore, so namespacing
to avoid conflicts.

* don't use SortedWayStore if LocationsOnWays present

* stub in insertLatpLons/insertNodes

* change at() to return a non mmap vector

SortedWayStore won't create mmaped vectors, so we need to return the
lowest common denominator.

This pessimizes performance of BinarySearchWayStore, since it'll have
to allocate vectors on demand.

Longer term: it might be better to return an iterator that hides the heavy
lifting.

* begin drawing the rest of the owl

* flesh out types

* add unit test framework

* naive encoding of ways

Checkpointing since I have something that works.

Future optimizations:

- when all high ints are the same, don't encode them
- compression

* more efficient if high ints are all the same

* extract mmap_allocator.cpp

This is needed to unit test the way store without dragging
in osm_store.

* progress on publishGroup

checkpointing, going to extract a populateMask(...) function

* add populateMask function

* finish publishGroup

* SortedWayStore: implement at

* pass node store into SortedWayStore

* fix alignment

* better logs

* way stores should throw std::out_of_range

This is part of the contract, client code will catch it and reject
relations that have missing ways.

* sortednodestore: throw std::out_of_range

* support way compression

* remove dead code, robust against empty ways

* implement clear()

* maybe fix windows build?

very unclear why this is needed, but we seem to be getting C2131 on this
line.

* don't use variable-length arrays on stack

Workaround for MSVC

* avoid more variable-length arrays

* make the other vectors as thread-local

* --no-compress-ways, --no-compress-nodes
  • Loading branch information
cldellow authored Dec 9, 2023
1 parent f3c10da commit 8300b0c
Show file tree
Hide file tree
Showing 61 changed files with 7,376 additions and 912 deletions.
10 changes: 10 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,21 @@ file(GLOB tilemaker_src_files
src/helpers.cpp
src/osm_lua_processing.cpp
src/osm_store.cpp
src/mmap_allocator.cpp
src/pbf_blocks.cpp
src/read_shp.cpp
src/shp_mem_tiles.cpp
src/tilemaker.cpp
src/write_geometry.cpp
src/node_stores.cpp
src/coordinates_geom.cpp
src/sorted_node_store.cpp
src/sorted_way_store.cpp
src/way_stores.cpp
src/external/streamvbyte_decode.cc
src/external/streamvbyte_encode.cc
src/external/streamvbyte_zigzag.cc

)
add_executable(tilemaker vector_tile.pb.cc osmformat.pb.cc ${tilemaker_src_files})
target_include_directories(tilemaker PRIVATE include)
Expand Down
46 changes: 44 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,54 @@ LIB := -L$(PLATFORM_PATH)/lib -lz $(LUA_LIBS) -lboost_program_options -lsqlite3
INC := -I$(PLATFORM_PATH)/include -isystem ./include -I./src $(LUA_CFLAGS)

# Targets
.PHONY: test

all: tilemaker

tilemaker: include/osmformat.pb.o include/vector_tile.pb.o src/mbtiles.o src/pbf_blocks.o src/coordinates.o src/osm_store.o src/helpers.o src/output_object.o src/read_shp.o src/read_pbf.o src/osm_lua_processing.o src/write_geometry.o src/shared_data.o src/tile_worker.o src/tile_data.o src/osm_mem_tiles.o src/shp_mem_tiles.o src/attribute_store.o src/tilemaker.o src/geom.o
tilemaker: \
include/osmformat.pb.o \
include/vector_tile.pb.o \
src/attribute_store.o \
src/coordinates_geom.o \
src/coordinates.o \
src/external/streamvbyte_decode.o \
src/external/streamvbyte_encode.o \
src/external/streamvbyte_zigzag.o \
src/geom.o \
src/helpers.o \
src/mbtiles.o \
src/mmap_allocator.o \
src/node_stores.o \
src/osm_lua_processing.o \
src/osm_mem_tiles.o \
src/osm_store.o \
src/output_object.o \
src/pbf_blocks.o \
src/read_pbf.o \
src/read_shp.o \
src/shared_data.o \
src/shp_mem_tiles.o \
src/sorted_node_store.o \
src/sorted_way_store.o \
src/tile_data.o \
src/tilemaker.o \
src/tile_worker.o \
src/way_stores.o \
src/write_geometry.o
$(CXX) $(CXXFLAGS) -o tilemaker $^ $(INC) $(LIB) $(LDFLAGS)

test: test_sorted_way_store

test_sorted_way_store: \
src/external/streamvbyte_decode.o \
src/external/streamvbyte_encode.o \
src/external/streamvbyte_zigzag.o \
src/mmap_allocator.o \
src/sorted_way_store.o \
src/sorted_way_store.test.o
$(CXX) $(CXXFLAGS) -o test $^ $(INC) $(LIB) $(LDFLAGS) && ./test


%.o: %.cpp
$(CXX) $(CXXFLAGS) -o $@ -c $< $(INC)

Expand All @@ -110,6 +152,6 @@ install:
install docs/man/tilemaker.1 ${DESTDIR}${MANPREFIX}/man1/

clean:
rm -f tilemaker src/*.o include/*.o include/*.pb.h
rm -f tilemaker src/*.o src/external/*.o include/*.o include/*.pb.h

.PHONY: install
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,13 @@ Formatting: braces and indents as shown, hard tabs (4sp). (Yes, I know.) Please

Tilemaker is maintained by Richard Fairhurst and supported by [many contributors](https://github.com/systemed/tilemaker/graphs/contributors).

Copyright tilemaker contributors, 2015-2023. The tilemaker code is licensed as FTWPL; you may do anything you like with this code and there is no warranty. The included sqlite_modern_cpp (Amin Roosta) is MIT; [kaguya](https://github.com/satoren/kaguya) is licensed under the Boost Software Licence.
Copyright tilemaker contributors, 2015-2023.

The tilemaker code is licensed as FTWPL; you may do anything you like with this code and there is no warranty.

Licenses of third-party libraries:

- sqlite_modern_cpp (Amin Roosta) is licensed under MIT
- [kaguya](https://github.com/satoren/kaguya) is licensed under the Boost Software Licence
- [libpopcnt](https://github.com/kimwalisch/libpopcnt) is licensed under BSD 2-clause
- [streamvbyte](https://github.com/lemire/streamvbyte) is licensed under Apache 2
68 changes: 28 additions & 40 deletions include/coordinates.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,27 @@
#ifndef _COORDINATES_H
#define _COORDINATES_H

#include <iostream>
#include "geom.h"
// Lightweight types and functions for coordinates, for classes that don't
// need to pull in boost::geometry.
//
// Things that pull in boost::geometry should go in coordinates_geom.h

#include <cstdint>
#include <utility>
#include <vector>
#include <deque>
#include <unordered_set>

// A 36-bit integer can store all OSM node IDs; we represent this as 16 collections
// of 32-bit integers.
#define NODE_SHARDS 16
typedef uint32_t ShardedNodeID;
typedef uint64_t NodeID;
typedef uint64_t WayID;

typedef std::vector<WayID> WayVec;


#ifdef FAT_TILE_INDEX
// Supports up to z22
typedef uint32_t TileCoordinate;
Expand Down Expand Up @@ -92,18 +108,18 @@ double lat2latp(double lat);
double latp2lat(double latp);

// Tile conversions
double lon2tilexf(double lon, uint z);
double latp2tileyf(double latp, uint z);
double lat2tileyf(double lat, uint z);
uint lon2tilex(double lon, uint z);
uint latp2tiley(double latp, uint z);
uint lat2tiley(double lat, uint z);
double tilex2lon(uint x, uint z);
double tiley2latp(uint y, uint z);
double tiley2lat(uint y, uint z);
double lon2tilexf(double lon, uint8_t z);
double latp2tileyf(double latp, uint8_t z);
double lat2tileyf(double lat, uint8_t z);
uint32_t lon2tilex(double lon, uint8_t z);
uint32_t latp2tiley(double latp, uint8_t z);
uint32_t lat2tiley(double lat, uint8_t z);
double tilex2lon(uint32_t x, uint8_t z);
double tiley2latp(uint32_t y, uint8_t z);
double tiley2lat(uint32_t y, uint8_t z);

// Get a tile index
TileCoordinates latpLon2index(LatpLon ll, uint baseZoom);
TileCoordinates latpLon2index(LatpLon ll, uint8_t baseZoom);

// Earth's (mean) radius
// http://nssdc.gsfc.nasa.gov/planetary/factsheet/earthfact.html
Expand All @@ -115,36 +131,8 @@ double degp2meter(double degp, double latp);

double meter2degp(double meter, double latp);

void insertIntermediateTiles(Linestring const &points, uint baseZoom, std::unordered_set<TileCoordinates> &tileSet);
void insertIntermediateTiles(Ring const &points, uint baseZoom, std::unordered_set<TileCoordinates> &tileSet);

// the range between smallest y and largest y is filled, for each x
void fillCoveredTiles(std::unordered_set<TileCoordinates> &tileSet);

// ------------------------------------------------------
// Helper class for dealing with spherical Mercator tiles

class TileBbox {

public:
double minLon, maxLon, minLat, maxLat, minLatp, maxLatp;
double xmargin, ymargin, xscale, yscale;
TileCoordinates index;
uint zoom;
bool hires;
bool endZoom;
Box clippingBox;

TileBbox(TileCoordinates i, uint z, bool h, bool e);

std::pair<int,int> scaleLatpLon(double latp, double lon) const;
std::vector<Point> scaleRing(Ring const &src) const;
MultiPolygon scaleGeometry(MultiPolygon const &src) const;
std::pair<double, double> floorLatpLon(double latp, double lon) const;

Box getTileBox() const;
Box getExtendBox() const;
};

#endif //_COORDINATES_H

35 changes: 35 additions & 0 deletions include/coordinates_geom.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#ifndef _COORDINATES_GEOM_H
#define _COORDINATES_GEOM_H

#include "coordinates.h"
#include "geom.h"

void insertIntermediateTiles(Linestring const &points, uint baseZoom, std::unordered_set<TileCoordinates> &tileSet);
void insertIntermediateTiles(Ring const &points, uint baseZoom, std::unordered_set<TileCoordinates> &tileSet);

// ------------------------------------------------------
// Helper class for dealing with spherical Mercator tiles
class TileBbox {

public:
double minLon, maxLon, minLat, maxLat, minLatp, maxLatp;
double xmargin, ymargin, xscale, yscale;
TileCoordinates index;
uint zoom;
bool hires;
bool endZoom;
Box clippingBox;

TileBbox(TileCoordinates i, uint z, bool h, bool e);

std::pair<int,int> scaleLatpLon(double latp, double lon) const;
std::vector<Point> scaleRing(Ring const &src) const;
MultiPolygon scaleGeometry(MultiPolygon const &src) const;
std::pair<double, double> floorLatpLon(double latp, double lon) const;

Box getTileBox() const;
Box getExtendBox() const;
};


#endif
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 8300b0c

Please sign in to comment.