osmflow 0.11.0

Fast, parallel reading of OpenStreetMap PBF files, and node location stores, from a city extract to the planet
Documentation
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
//! The encoding of node locations shared by all stores.
//!
//! Locations are 1e-7 degree integers, as in OSM PBF files, so they come back exactly. Node IDs are
//! grouped into blocks of 64 consecutive IDs. A block stores which of its IDs are present, its
//! first location as the step from its page's base location, and each further location as the step
//! from the previous one: bit-packed, as varints, or as plain integers, whichever is smallest.
//!
//! A page is a run of blocks in ID order, starting with a table of its blocks, so a lookup goes
//! straight to its block. Its header has a CRC-32 of the page, to find damaged pages.

use std::alloc::{self, Layout};
use std::ptr;

use crate::Location;

pub(crate) const BLOCK_SHIFT: u32 = 6;
pub(crate) const BLOCK_LEN: usize = 1 << BLOCK_SHIFT;
/// Blocks a reader keeps decoded: 2 MiB, which a way's nodes seldom go beyond
const READER_SLOTS: usize = 4096;

/// first block (u64), bytes used (u16), block count (u16), base location (2 × i32), CRC-32 of the
/// page with this field zero (u32)
pub(crate) const PAGE_HEADER: usize = 24;
const CRC_AT: usize = 20;
/// per block: its number above the page's first block (u32), and where it ends (u16)
pub(crate) const ENTRY: usize = 6;

pub(crate) const MASK_FULL: u8 = 0;
pub(crate) const MASK_RUN: u8 = 1;
pub(crate) const MASK_BITS: u8 = 2;
pub(crate) const CODEC_RAW: u8 = 0;
pub(crate) const CODEC_VARINT: u8 = 1;
pub(crate) const CODEC_PACKED: u8 = 2;

#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub(crate) struct Block {
    pub(crate) num: u64,
    pub(crate) mask: u64,
    pub(crate) locs: Vec<Location>,
}

impl Block {
    pub(crate) fn single(num: u64, bit: u64, loc: Location) -> Self {
        Self {
            num,
            mask: bit,
            locs: vec![loc],
        }
    }

    /// The block's nodes and their locations, in ID order
    pub(crate) fn nodes(&self) -> impl Iterator<Item = (u64, Location)> + '_ {
        let mut mask = self.mask;
        self.locs.iter().map(move |&loc| {
            let id = (self.num << BLOCK_SHIFT) | u64::from(mask.trailing_zeros());
            mask &= mask - 1;
            (id, loc)
        })
    }
}

/// The block number of node `id`, and the node's bit in the block's mask
#[inline]
pub(crate) fn block_of(id: u64) -> (u64, u64) {
    (id >> BLOCK_SHIFT, 1 << (id & (BLOCK_LEN as u64 - 1)))
}

/// The index of the node at `bit` among the block's present nodes
#[inline]
pub(crate) fn rank(mask: u64, bit: u64) -> usize {
    (mask & (bit - 1)).count_ones() as usize
}

pub(crate) struct PageHeader {
    pub(crate) first_block: u64,
    pub(crate) used: usize,
    pub(crate) n_blocks: usize,
    pub(crate) base: Location,
}

#[inline]
pub(crate) fn u16_at(b: &[u8], at: usize) -> u16 {
    u16::from_le_bytes(b[at..at + 2].try_into().unwrap())
}

#[inline]
pub(crate) fn u32_at(b: &[u8], at: usize) -> u32 {
    u32::from_le_bytes(b[at..at + 4].try_into().unwrap())
}

#[inline]
pub(crate) fn i32_at(b: &[u8], at: usize) -> i32 {
    i32::from_le_bytes(b[at..at + 4].try_into().unwrap())
}

#[inline]
pub(crate) fn u64_at(b: &[u8], at: usize) -> u64 {
    u64::from_le_bytes(b[at..at + 8].try_into().unwrap())
}

#[inline]
pub(crate) fn page_header(page: &[u8]) -> PageHeader {
    PageHeader {
        first_block: u64_at(page, 0),
        used: usize::from(u16_at(page, 8)),
        n_blocks: usize::from(u16_at(page, 10)),
        base: Location::new(i32_at(page, 12), i32_at(page, 16)),
    }
}

pub(crate) fn set_page_header(page: &mut [u8], first_block: u64, n_blocks: usize, base: Location) {
    let used = u16::try_from(page.len()).expect("page too large");
    let n_blocks = u16::try_from(n_blocks).expect("too many blocks");
    page[0..8].copy_from_slice(&first_block.to_le_bytes());
    page[8..10].copy_from_slice(&used.to_le_bytes());
    page[10..12].copy_from_slice(&n_blocks.to_le_bytes());
    page[12..16].copy_from_slice(&base.lat.to_le_bytes());
    page[16..20].copy_from_slice(&base.lon.to_le_bytes());
}

/// Block `i`'s number above its page's first block, and where it ends
#[inline]
pub(crate) fn entry(page: &[u8], i: usize) -> (u32, usize) {
    let at = PAGE_HEADER + ENTRY * i;
    (u32_at(page, at), usize::from(u16_at(page, at + 4)))
}

pub(crate) fn set_entry(page: &mut [u8], i: usize, rel: u32, end: usize) {
    let at = PAGE_HEADER + ENTRY * i;
    let end = u16::try_from(end).expect("page too large");
    page[at..at + 4].copy_from_slice(&rel.to_le_bytes());
    page[at + 4..at + 6].copy_from_slice(&end.to_le_bytes());
}

/// The CRC-32 a page stores, and the one its bytes have.
pub(crate) fn page_crcs(page: &[u8]) -> (u32, u32) {
    let used = usize::from(u16_at(page, 8)).clamp(PAGE_HEADER, page.len());
    let mut hasher = crc32fast::Hasher::new();
    hasher.update(&page[..CRC_AT]);
    hasher.update(&[0; 4]);
    hasher.update(&page[CRC_AT + 4..used]);
    (u32_at(page, CRC_AT), hasher.finalize())
}

/// Stores the CRC-32 of an encoded page in its header.
pub(crate) fn set_page_crc(page: &mut [u8]) {
    page[CRC_AT..CRC_AT + 4].fill(0);
    let crc = crc32fast::hash(page);
    page[CRC_AT..CRC_AT + 4].copy_from_slice(&crc.to_le_bytes());
}

pub(crate) fn zigzag(v: i64) -> u64 {
    ((v << 1) ^ (v >> 63)).cast_unsigned()
}

#[inline]
pub(crate) fn unzigzag(v: u64) -> i64 {
    (v >> 1).cast_signed() ^ -((v & 1).cast_signed())
}

pub(crate) fn bit_width(v: u64) -> u32 {
    64 - v.leading_zeros()
}

pub(crate) fn varint_len(v: u64) -> usize {
    bit_width(v).max(1).div_ceil(7) as usize
}

#[expect(clippy::cast_possible_truncation)]
pub(crate) fn write_varint(out: &mut Vec<u8>, mut v: u64) {
    while v >= 0x80 {
        out.push((v as u8) | 0x80);
        v >>= 7;
    }
    out.push(v as u8);
}

#[inline]
pub(crate) fn read_varint(b: &[u8], pos: &mut usize) -> u64 {
    let mut v = 0_u64;
    let mut shift = 0;
    loop {
        let byte = b[*pos];
        *pos += 1;
        v |= u64::from(byte & 0x7f) << shift;
        if byte < 0x80 {
            return v;
        }
        shift += 7;
    }
}

/// Reads `width` bits (at most 57) starting at bit `bit` of `b`.
#[inline]
pub(crate) fn read_bits(b: &[u8], bit: usize, width: usize) -> u64 {
    let start = bit / 8;
    let word = if let Some(w) = b.get(start..start + 8) {
        u64::from_le_bytes(w.try_into().unwrap())
    } else {
        let mut word = [0_u8; 8];
        let tail = b.get(start..).unwrap_or_default();
        word[..tail.len()].copy_from_slice(tail);
        u64::from_le_bytes(word)
    };
    (word >> (bit % 8)) & ((1 << width) - 1)
}

pub(crate) struct BitWriter<'a> {
    pub(crate) out: &'a mut Vec<u8>,
    pub(crate) acc: u64,
    pub(crate) bits: u32,
}

#[expect(clippy::cast_possible_truncation)]
impl BitWriter<'_> {
    pub(crate) fn push(&mut self, v: u64, width: u32) {
        self.acc |= v << self.bits;
        self.bits += width;
        while self.bits >= 8 {
            self.out.push(self.acc as u8);
            self.acc >>= 8;
            self.bits -= 8;
        }
    }

    pub(crate) fn finish(self) {
        if self.bits > 0 {
            self.out.push(self.acc as u8);
        }
    }
}

pub(crate) fn step(from: Location, to: Location) -> (u64, u64) {
    (
        zigzag(i64::from(to.lat) - i64::from(from.lat)),
        zigzag(i64::from(to.lon) - i64::from(from.lon)),
    )
}

#[expect(clippy::cast_possible_truncation)]
#[inline]
pub(crate) fn apply(from: Location, (dlat, dlon): (u64, u64)) -> Location {
    Location::new(
        (i64::from(from.lat) + unzigzag(dlat)) as i32,
        (i64::from(from.lon) + unzigzag(dlon)) as i32,
    )
}

/// The start and length of `mask`'s bits, if they are one run shorter than a block
#[expect(clippy::cast_possible_truncation)]
pub(crate) fn as_run(mask: u64) -> Option<(u8, u8)> {
    let start = mask.trailing_zeros();
    let count = (mask >> start).trailing_ones();
    (mask >> start >> count == 0 && count < 64).then_some((start as u8, count as u8))
}

/// Encodes a block whose page's base location is `base`.
pub(crate) fn encode_block(out: &mut Vec<u8>, b: &Block, base: Location) {
    encode_parts(out, b, Some(step(base, b.locs[0])));
}

/// Encodes a block without its first location, which is a step from its page's base: the kind and
/// mask, then the steps. Returns where the steps start, for [`pack_block`]. Encoding blocks this
/// way needs no page, so it can run on many threads.
pub(crate) fn encode_detached(out: &mut Vec<u8>, b: &Block) -> usize {
    encode_parts(out, b, None)
}

/// Appends a block from [`encode_detached`] to a page whose base location is `base`.
pub(crate) fn pack_block(
    out: &mut Vec<u8>,
    detached: &[u8],
    steps: usize,
    first: Location,
    base: Location,
) {
    out.extend_from_slice(&detached[..steps]);
    let (dlat, dlon) = step(base, first);
    write_varint(out, dlat);
    write_varint(out, dlon);
    out.extend_from_slice(&detached[steps..]);
}

/// The size of a block from [`encode_detached`] in a page whose base location is `base`.
pub(crate) fn packed_len(detached_len: usize, first: Location, base: Location) -> usize {
    let (dlat, dlon) = step(base, first);
    detached_len + varint_len(dlat) + varint_len(dlon)
}

/// Encodes the kind and mask, the first location's step if given, and the other steps. Returns
/// where the step of the first location is, or would be.
#[expect(clippy::cast_possible_truncation)]
fn encode_parts(out: &mut Vec<u8>, b: &Block, first: Option<(u64, u64)>) -> usize {
    let mut buf = [(0, 0); BLOCK_LEN - 1];
    let steps = &mut buf[..b.locs.len() - 1];
    for (s, w) in steps.iter_mut().zip(b.locs.windows(2)) {
        *s = step(w[0], w[1]);
    }
    let n = steps.len();
    let (wlat, wlon) = steps.iter().fold((0, 0), |(wlat, wlon), &(dlat, dlon)| {
        (wlat.max(bit_width(dlat)), wlon.max(bit_width(dlon)))
    });
    let varint: usize = steps
        .iter()
        .map(|&(dlat, dlon)| varint_len(dlat) + varint_len(dlon))
        .sum();
    let packed = 2 + ((wlat + wlon) as usize * n).div_ceil(8);
    let codec = if 8 * n <= varint.min(packed) {
        CODEC_RAW
    } else if varint <= packed {
        CODEC_VARINT
    } else {
        CODEC_PACKED
    };

    let start = out.len();
    let kind = codec << 2;
    if b.mask == u64::MAX {
        out.push(MASK_FULL | kind);
    } else if let Some((start, count)) = as_run(b.mask) {
        out.extend_from_slice(&[MASK_RUN | kind, start, count]);
    } else {
        out.push(MASK_BITS | kind);
        out.extend_from_slice(&b.mask.to_le_bytes());
    }
    let head = out.len() - start;
    if let Some((dlat, dlon)) = first {
        write_varint(out, dlat);
        write_varint(out, dlon);
    }
    match codec {
        CODEC_RAW => {
            for l in &b.locs[1..] {
                out.extend_from_slice(&l.lat.to_le_bytes());
                out.extend_from_slice(&l.lon.to_le_bytes());
            }
        }
        CODEC_VARINT => {
            for &(dlat, dlon) in &*steps {
                write_varint(out, dlat);
                write_varint(out, dlon);
            }
        }
        _ => {
            out.extend_from_slice(&[wlat as u8, wlon as u8]);
            let mut w = BitWriter {
                out,
                acc: 0,
                bits: 0,
            };
            for &(dlat, _) in &*steps {
                w.push(dlat, wlat);
            }
            for &(_, dlon) in &*steps {
                w.push(dlon, wlon);
            }
            w.finish();
        }
    }
    head
}

/// A block's header, read from its page.
pub(crate) struct Head {
    pub(crate) num: u64,
    pub(crate) mask: u64,
    pub(crate) mask_kind: u8,
    pub(crate) codec: u8,
    pub(crate) first: Location,
    /// where the block's steps start in the page
    pub(crate) steps: usize,
}

/// The header of block `num`, if `page` has it
#[inline]
pub(crate) fn find_in_page(page: &[u8], num: u64) -> Option<Head> {
    let h = page_header(page);
    let rel = u32::try_from(num.checked_sub(h.first_block)?).ok()?;
    let table = page[PAGE_HEADER..PAGE_HEADER + ENTRY * h.n_blocks]
        .as_chunks::<ENTRY>()
        .0;
    let i = table
        .binary_search_by_key(&rel, |e| u32::from_le_bytes([e[0], e[1], e[2], e[3]]))
        .ok()?;
    Some(read_block(page, &h, i))
}

/// The header of block `i` of a page
#[inline]
pub(crate) fn read_block(page: &[u8], h: &PageHeader, i: usize) -> Head {
    let pos = if i == 0 {
        PAGE_HEADER + ENTRY * h.n_blocks
    } else {
        entry(page, i - 1).1
    };
    let kind = page[pos];
    let (mask, mask_len) = match kind & 3 {
        MASK_FULL => (u64::MAX, 0),
        MASK_RUN => (((1_u64 << page[pos + 2]) - 1) << page[pos + 1], 2),
        _ => (u64_at(page, pos + 1), 8),
    };
    let mut pos = pos + 1 + mask_len;
    let dlat = read_varint(page, &mut pos);
    let dlon = read_varint(page, &mut pos);
    Head {
        num: h.first_block + u64::from(entry(page, i).0),
        mask,
        mask_kind: kind & 3,
        codec: kind >> 2,
        first: apply(h.base, (dlat, dlon)),
        steps: pos,
    }
}

impl Head {
    #[inline]
    pub(crate) fn len(&self) -> usize {
        self.mask.count_ones() as usize
    }

    /// The location of the block's `rank`-th present node.
    #[inline]
    pub(crate) fn location(&self, page: &[u8], rank: usize) -> Location {
        if self.codec == CODEC_RAW && rank > 0 {
            return raw_location(&page[self.steps..], rank);
        }
        let mut out = self.first;
        self.decode(page, rank + 1, |_, loc| out = loc);
        out
    }

    /// Decodes the locations of the block's present nodes into the start of `locs`.
    #[inline]
    pub(crate) fn decode_into(&self, page: &[u8], locs: &mut [Location; BLOCK_LEN]) {
        locs[0] = self.first;
        self.decode(page, self.len(), |r, loc| locs[r] = loc);
    }

    /// Calls `f` with the rank and location of each present node from rank 1 to below `end`.
    #[inline]
    pub(crate) fn decode(&self, page: &[u8], end: usize, mut f: impl FnMut(usize, Location)) {
        let p = &page[self.steps..];
        let mut loc = self.first;
        match self.codec {
            CODEC_RAW => (1..end).for_each(|r| f(r, raw_location(p, r))),
            CODEC_VARINT => {
                let mut pos = 0;
                for r in 1..end {
                    let dlat = read_varint(p, &mut pos);
                    loc = apply(loc, (dlat, read_varint(p, &mut pos)));
                    f(r, loc);
                }
            }
            _ => {
                let (wlat, wlon) = (usize::from(p[0]), usize::from(p[1]));
                // the bits run on to the end of the page, so most reads need no bounds care
                let (bits, lon_start) = (&p[2..], (self.len() - 1) * wlat);
                for r in 1..end {
                    let i = r - 1;
                    let dlat = read_bits(bits, i * wlat, wlat);
                    loc = apply(loc, (dlat, read_bits(bits, lon_start + i * wlon, wlon)));
                    f(r, loc);
                }
            }
        }
    }
}

/// The plain location of rank `rank`, from a block's steps
#[inline]
pub(crate) fn raw_location(steps: &[u8], rank: usize) -> Location {
    Location::new(i32_at(steps, 8 * rank - 8), i32_at(steps, 8 * rank - 4))
}

pub(crate) fn decode_page(page: &[u8]) -> (u64, Vec<Block>) {
    let h = page_header(page);
    let blocks = (0..h.n_blocks)
        .map(|i| {
            let head = read_block(page, &h, i);
            let mut locs = [Location::default(); BLOCK_LEN];
            head.decode_into(page, &mut locs);
            Block {
                num: head.num,
                mask: head.mask,
                locs: locs[..head.len()].to_vec(),
            }
        })
        .collect();
    (h.first_block, blocks)
}

/// Encodes a page; its blocks must be within `u32::MAX` of `first_block`, as node IDs up to
/// [`super::MAX_NODE_ID`] are.
pub(crate) fn encode_page(first_block: u64, blocks: &[Block], out: &mut Vec<u8>) {
    out.clear();
    out.resize(PAGE_HEADER + ENTRY * blocks.len(), 0);
    let base = blocks.first().map_or_else(Location::default, |b| b.locs[0]);
    for (i, b) in blocks.iter().enumerate() {
        encode_block(out, b, base);
        let rel = u32::try_from(b.num - first_block).expect("block too far above its page's first");
        let end = out.len();
        set_entry(out, i, rel, end);
    }
    set_page_header(out, first_block, blocks.len(), base);
}

/// Reads locations for one thread, keeping blocks it read decoded, as a way's nodes are mostly in a
/// few blocks: block `num` in slot `num % READER_SLOTS`, so finding it is one comparison.
pub(crate) struct Slots {
    /// each slot's block number plus 1, or 0 for none
    nums: Box<[u64]>,
    masks: Box<[u64]>,
    locs: Box<[[Location; BLOCK_LEN]]>,
}

/// `n` zeroed values, allocated so that the OS gives memory only to the parts that are used
#[expect(
    unsafe_code,
    reason = "`vec!` asks the allocator for zeroed memory only for some types"
)]
fn zeroed<T: Copy>(n: usize) -> Box<[T]> {
    let layout = Layout::array::<T>(n).unwrap();
    assert!(layout.size() > 0);
    // SAFETY: the layout is not empty.
    let p = unsafe { alloc::alloc_zeroed(layout) }.cast::<T>();
    if p.is_null() {
        alloc::handle_alloc_error(layout);
    }
    // SAFETY: `p` holds `n` zeroed values of `T`, allocated with the layout a `Box<[T]>` frees, and
    // all zero is a valid `T`: this is only used for integers and `Location`s.
    unsafe { Box::from_raw(ptr::slice_from_raw_parts_mut(p, n)) }
}

impl Default for Slots {
    fn default() -> Self {
        Self {
            nums: zeroed(READER_SLOTS),
            masks: zeroed(READER_SLOTS),
            locs: zeroed(READER_SLOTS),
        }
    }
}

impl Slots {
    /// The location of node `id`. A block not kept is decoded into its slot by `load`, which
    /// returns the block's mask (0 if it is not stored).
    #[inline]
    pub(crate) fn get(
        &mut self,
        id: u64,
        load: impl FnOnce(u64, &mut [Location; BLOCK_LEN]) -> u64,
    ) -> Option<Location> {
        let (num, bit) = block_of(id);
        #[expect(clippy::cast_possible_truncation)]
        let slot = num as usize % READER_SLOTS;
        if self.nums[slot] != num + 1 {
            self.nums[slot] = num + 1;
            self.masks[slot] = load(num, &mut self.locs[slot]);
        }
        let mask = self.masks[slot];
        (mask & bit != 0).then(|| self.locs[slot][rank(mask, bit)])
    }
}