manby 0.1.7

Builds vector tilesets from geographic features, like tippecanoe, several times faster
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
//! Memory: how much the process uses.

/// The resident set size of the process in bytes, if the platform says.
pub fn resident() -> Option<u64> {
    #[cfg(target_os = "macos")]
    {
        let mut info: libc::proc_taskinfo = unsafe { std::mem::zeroed() };
        let size = std::mem::size_of::<libc::proc_taskinfo>() as libc::c_int;
        // SAFETY: info is a proc_taskinfo of the size given.
        let n = unsafe {
            libc::proc_pidinfo(
                libc::getpid(),
                libc::PROC_PIDTASKINFO,
                0,
                &mut info as *mut _ as *mut libc::c_void,
                size,
            )
        };
        if n == size {
            return Some(info.pti_resident_size);
        }
        None
    }
    #[cfg(target_os = "linux")]
    {
        let s = std::fs::read_to_string("/proc/self/statm").ok()?;
        let pages: u64 = s.split_whitespace().nth(1)?.parse().ok()?;
        // SAFETY: sysconf has no preconditions.
        let page = unsafe { libc::sysconf(libc::_SC_PAGESIZE) } as u64;
        Some(pages * page)
    }
    #[cfg(not(any(target_os = "macos", target_os = "linux")))]
    {
        None
    }
}

/// Lets the pages of part of a file mapping go: they are read from the file
/// again if they are needed, but no longer count as the process's memory.
pub fn release_mapped(part: &[u8]) {
    // SAFETY: sysconf has no preconditions.
    let page = unsafe { libc::sysconf(libc::_SC_PAGESIZE) } as usize;
    let start = (part.as_ptr() as usize).next_multiple_of(page);
    let end = (part.as_ptr() as usize + part.len()) / page * page;
    if end <= start {
        return;
    }
    let (addr, len) = (start as *mut libc::c_void, end - start);
    // SAFETY: the range lies inside a read-only mapping of a file, whose
    // contents these calls leave as they are.
    unsafe {
        #[cfg(target_os = "macos")]
        libc::msync(addr, len, libc::MS_INVALIDATE);
        #[cfg(not(target_os = "macos"))]
        libc::madvise(addr, len, libc::MADV_DONTNEED);
    }
}

/// The peak resident set size of the process in bytes.
pub fn peak_resident() -> Option<u64> {
    // SAFETY: rusage is plain data that getrusage fills in.
    let mut ru: libc::rusage = unsafe { std::mem::zeroed() };
    // SAFETY: ru is a valid rusage.
    if unsafe { libc::getrusage(libc::RUSAGE_SELF, &mut ru) } != 0 {
        return None;
    }
    let max = ru.ru_maxrss as u64;
    // bytes on macOS, kilobytes elsewhere
    if cfg!(target_os = "macos") {
        Some(max)
    } else {
        Some(max * 1024)
    }
}

use std::collections::BTreeMap;
use std::fs::{File, OpenOptions};
use std::os::unix::fs::FileExt;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering::Relaxed};
use std::sync::{Arc, Mutex, OnceLock, RwLock};

use tippecanoe::errors::{EXIT_OPEN, EXIT_READ, EXIT_WRITE};

use crate::Error;
use crate::buf::Buf;

/// How many bytes a spool collects in one buffer.
const REGION: usize = 256 << 10;
/// Writers go to one of this many buffers, by thread.
const SPOOL_SHARDS: usize = 8;
/// Regions read back from the file that a spool keeps for the next reads.
const CACHED: usize = 64;
const NO_SHARD: u32 = u32::MAX;

/// How many bytes of data at rest may stay in memory; the rest goes to
/// temporary files.
pub struct Rest {
    used: AtomicU64,
    allowance: u64,
}

impl Rest {
    pub fn new(allowance: usize) -> Arc<Rest> {
        Arc::new(Rest {
            used: AtomicU64::new(0),
            allowance: allowance as u64,
        })
    }

    /// Takes room for `n` bytes, if there is room.
    fn take(&self, n: usize) -> bool {
        let n = n as u64;
        self.used
            .fetch_update(Relaxed, Relaxed, |u| {
                (u + n <= self.allowance).then_some(u + n)
            })
            .is_ok()
    }

    fn give(&self, n: usize) {
        self.used.fetch_sub(n as u64, Relaxed);
    }
}

/// Data at rest: written once and read back later, kept in memory in large
/// blocks while `Rest` has room and otherwise in an unlinked temporary file,
/// whose pages are in the page cache but not part of the process's memory.
/// Writes are collected per thread into regions; a full region stays in
/// memory or is written out in one piece.
pub struct Spool {
    dir: PathBuf,
    rest: Arc<Rest>,
    file: OnceLock<std::result::Result<File, String>>,
    len: AtomicU64,
    shards: Vec<Mutex<Option<Region>>>,
    /// Full regions in memory, by start.
    kept: RwLock<BTreeMap<u64, Vec<u8>>>,
    kept_bytes: AtomicU64,
    /// Regions in the file, by start, with their lengths.
    written: RwLock<BTreeMap<u64, usize>>,
    cache: Mutex<Vec<(u64, Arc<Vec<u8>>)>>,
}

/// A region being filled.
struct Region {
    start: u64,
    buf: Vec<u8>,
}

impl Spool {
    pub fn new(dir: &Path, rest: &Arc<Rest>) -> Arc<Spool> {
        Arc::new(Spool {
            dir: dir.to_path_buf(),
            rest: rest.clone(),
            file: OnceLock::new(),
            len: AtomicU64::new(0),
            shards: (0..SPOOL_SHARDS).map(|_| Mutex::new(None)).collect(),
            kept: RwLock::new(BTreeMap::new()),
            kept_bytes: AtomicU64::new(0),
            written: RwLock::new(BTreeMap::new()),
            cache: Mutex::new(Vec::new()),
        })
    }

    fn file(&self) -> crate::Result<&File> {
        self.file
            .get_or_init(|| create_unlinked(&self.dir, "spool"))
            .as_ref()
            .map_err(|m| Error::new(EXIT_OPEN, m.clone()))
    }

    /// Keeps a full region in memory if there is room, or writes it out.
    fn retire(&self, start: u64, buf: Vec<u8>) -> crate::Result<()> {
        if self.rest.take(buf.capacity()) {
            self.kept_bytes.fetch_add(buf.capacity() as u64, Relaxed);
            self.kept.write().unwrap().insert(start, buf);
            return Ok(());
        }
        self.file()?
            .write_all_at(&buf, start)
            .map_err(|e| Error::new(EXIT_WRITE, format!("{}: {}", self.dir.display(), e)))?;
        self.written.write().unwrap().insert(start, buf.len());
        Ok(())
    }

    /// Stores `data`, returning where it is.
    pub fn store(self: &Arc<Self>, data: &[u8]) -> crate::Result<Stored> {
        self.store_parts(&[data])
    }

    /// Stores the parts one after the other, returning where they are.
    pub fn store_parts(self: &Arc<Self>, parts: &[&[u8]]) -> crate::Result<Stored> {
        let len: usize = parts.iter().map(|p| p.len()).sum();
        if len >= REGION / 4 {
            let at = self.len.fetch_add(len as u64, Relaxed);
            if self.rest.take(len) {
                self.kept_bytes.fetch_add(len as u64, Relaxed);
                self.kept.write().unwrap().insert(at, parts.concat());
            } else {
                let mut off = at;
                for p in parts {
                    self.file()?.write_all_at(p, off).map_err(|e| {
                        Error::new(EXIT_WRITE, format!("{}: {}", self.dir.display(), e))
                    })?;
                    off += p.len() as u64;
                }
                self.written.write().unwrap().insert(at, len);
            }
            return Ok(Stored::Spooled {
                spool: self.clone(),
                shard: NO_SHARD,
                at,
                len,
            });
        }
        let shard = {
            use std::hash::{Hash, Hasher};
            let mut h = std::collections::hash_map::DefaultHasher::new();
            std::thread::current().id().hash(&mut h);
            (h.finish() % SPOOL_SHARDS as u64) as usize
        };
        let mut region = self.shards[shard].lock().unwrap();
        if region.as_ref().is_none_or(|r| r.buf.len() + len > REGION) {
            if let Some(r) = region.take() {
                self.retire(r.start, r.buf)?;
            }
            *region = Some(Region {
                start: self.len.fetch_add(REGION as u64, Relaxed),
                buf: Vec::with_capacity(REGION),
            });
        }
        let r = region.as_mut().unwrap();
        let at = r.start + r.buf.len() as u64;
        for p in parts {
            r.buf.extend_from_slice(p);
        }
        Ok(Stored::Spooled {
            spool: self.clone(),
            shard: shard as u32,
            at,
            len,
        })
    }

    /// Reads `out.len()` bytes from `at`. With `sequential`, a region read
    /// from the file is kept for the reads that follow, for a reader that
    /// goes through the data in about the order it was written.
    fn read(&self, shard: u32, at: u64, out: &mut [u8], sequential: bool) -> crate::Result<()> {
        let n = out.len() as u64;
        let within = |start: u64, len: usize| at >= start && at + n <= start + len as u64;
        // a region leaves its shard only after it is kept or written out
        if let Some(region) = self.shards.get(shard as usize) {
            let region = region.lock().unwrap();
            if let Some(r) = region.as_ref()
                && within(r.start, r.buf.len())
            {
                let from = (at - r.start) as usize;
                out.copy_from_slice(&r.buf[from..from + out.len()]);
                return Ok(());
            }
        }
        {
            let kept = self.kept.read().unwrap();
            if let Some((&start, buf)) = kept.range(..=at).next_back()
                && within(start, buf.len())
            {
                let from = (at - start) as usize;
                out.copy_from_slice(&buf[from..from + out.len()]);
                return Ok(());
            }
        }
        let region = self
            .written
            .read()
            .unwrap()
            .range(..=at)
            .next_back()
            .map(|(&s, &l)| (s, l));
        let file = self.file()?;
        let err =
            |e: std::io::Error| Error::new(EXIT_READ, format!("{}: {}", self.dir.display(), e));
        match region {
            Some((start, len)) if sequential && len <= REGION && within(start, len) => {
                let cached = self
                    .cache
                    .lock()
                    .unwrap()
                    .iter()
                    .find(|(s, _)| *s == start)
                    .map(|(_, b)| b.clone());
                let buf = match cached {
                    Some(b) => b,
                    None => {
                        let mut b = vec![0u8; len];
                        file.read_exact_at(&mut b, start).map_err(err)?;
                        let b = Arc::new(b);
                        let mut cache = self.cache.lock().unwrap();
                        if cache.len() >= CACHED {
                            cache.remove(0);
                        }
                        cache.push((start, b.clone()));
                        b
                    }
                };
                let from = (at - start) as usize;
                out.copy_from_slice(&buf[from..from + out.len()]);
                Ok(())
            }
            _ => file.read_exact_at(out, at).map_err(err),
        }
    }
}

impl Drop for Spool {
    fn drop(&mut self) {
        self.rest.give(self.kept_bytes.load(Relaxed) as usize);
    }
}

/// Bytes in memory, or stored in a spool.
pub enum Stored {
    Mem(Buf<u8>),
    Spooled {
        spool: Arc<Spool>,
        /// The buffer it went through, if any.
        shard: u32,
        at: u64,
        len: usize,
    },
}

impl Stored {
    pub fn len(&self) -> usize {
        match self {
            Stored::Mem(v) => v.len(),
            Stored::Spooled { len, .. } => *len,
        }
    }

    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    pub fn load(&self) -> crate::Result<Loaded<'_>> {
        self.load1(false)
    }

    /// `load`, for a reader that goes through a spool in about the order it
    /// was written.
    pub fn load_sequential(&self) -> crate::Result<Loaded<'_>> {
        self.load1(true)
    }

    fn load1(&self, sequential: bool) -> crate::Result<Loaded<'_>> {
        match self {
            Stored::Mem(v) => Ok(Loaded::Borrowed(v)),
            Stored::Spooled {
                spool,
                shard,
                at,
                len,
            } => {
                let mut out = Buf::with_capacity(*len);
                out.resize(*len, 0u8);
                spool.read(*shard, *at, &mut out, sequential)?;
                Ok(Loaded::Owned(out))
            }
        }
    }
}

/// Stored bytes at hand.
pub enum Loaded<'a> {
    Borrowed(&'a [u8]),
    Owned(Buf<u8>),
}

impl std::ops::Deref for Loaded<'_> {
    type Target = [u8];

    fn deref(&self) -> &[u8] {
        match self {
            Loaded::Borrowed(b) => b,
            Loaded::Owned(b) => b,
        }
    }
}

/// Creates a file in `dir` and unlinks it at once, as tippecanoe does with
/// its temporary files, so that it goes away however the process ends.
pub fn create_unlinked(dir: &Path, what: &str) -> std::result::Result<File, String> {
    for attempt in 0..100u32 {
        let name = dir.join(format!(
            "manby-{}-{}-{}-{}",
            what,
            std::process::id(),
            attempt,
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .map(|d| d.subsec_nanos())
                .unwrap_or(0)
        ));
        match OpenOptions::new()
            .read(true)
            .write(true)
            .create_new(true)
            .open(&name)
        {
            Ok(f) => {
                let _ = std::fs::remove_file(&name);
                return Ok(f);
            }
            Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => continue,
            Err(e) => return Err(format!("{}: {}", name.display(), e)),
        }
    }
    Err(format!("{}: cannot create a temporary file", dir.display()))
}