Skip to content

Instantly share code, notes, and snippets.

View nyurik's full-sized avatar

Yuri Astrakhan nyurik

View GitHub Profile
https://docs.google.com/presentation/d/1iQFFMCuIPLYdUiEgeFsO8fKTQB6ArSM8GrxLKXxY-RQ/edit?usp=sharing
https://docs.google.com/presentation/d/1Rl3k_bu7e3YZ-p8mGoQ-rqeJIUUlr6JfDSTCg3cWAog
@nyurik
nyurik / format.rs
Last active June 1, 2023 01:21
Rust format! double referencing performance impact
// Place this page as /benches/format.rs in a rust project created with `cargo new fmttest --lib`
// Add to Cargo.toml:
//
// [dev-dependencies]
// criterion = { version = "0.4", features = ["html_reports"] }
//
// [[bench]]
// name = "format"
// harness = false
@nyurik
nyurik / benches_iters.rs
Last active May 9, 2023 02:37
Add “iterate with separators” iterator function
// Benchmarks for the Rust iterator extension discussion at
// https://internals.rust-lang.org/t/add-iterate-with-separators-iterator-function/18781/13
// Place this page as /benches/iters.rs in a rust project created with `cargo new itertest --lib`
// Add to Cargo.toml:
//
// [dependencies]
// itertools = "0.10"
//
@nyurik
nyurik / bench.rs
Created April 5, 2023 19:30
Benchmark to evaluate linear vs dup-indexer performance
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use dup_indexer::DupIndexer;
fn benchmark_strings(c: &mut Criterion) {
let mut group = c.benchmark_group("Strings");
group.bench_function("String", |b| {
b.iter(|| {
let mut di = DupIndexer::new();
for _ in 0..100 {
@nyurik
nyurik / to-logical.py
Last active January 16, 2023 08:29
Script to convert SCSS files from physical to logical values for RTL and vertical languages
#
# This script converts margins, padding, and borders to logical values,
# allowing RTL and vertical languages to show correctly.
# Supports both *.css and *.scss files.
#
# Some renames are not yet implemented widely, and may require CSS plugin
# https://github.com/csstools/postcss-logical
# They have been commented out for now, but feel free to try them out.
#
# Full spec: https://drafts.csswg.org/css-logical/
@nyurik
nyurik / query.sql
Created January 16, 2023 05:58
Statistics of MVT tile GIS errors when encoding/decoding with ST_AsMVTGeom
SELECT x,
y,
ST_Y(p_mid) mid_lat,
ST_Y(p_min) min_lat,
ST_Y(p_max) max_lat,
ST_Y(d_mid) mid_lat_decoded,
ST_Y(d_min) min_lat_decoded,
ST_Y(d_max) max_lat_decoded,
abs(ST_Y(p_mid) - ST_Y(d_mid)) mid_lat_error,
abs(ST_Y(p_min) - ST_Y(d_min)) min_lat_error,
@nyurik
nyurik / optimized-mbtiles.sql
Created June 10, 2022 19:25
Some ideas on optimizing mbtiles file storage size with a single 32-bit index instead of z/x/y
create table map
(
tile_index INTEGER,
tmp_zoom INTEGER GENERATED ALWAYS AS ((tile_index & 0xFC000000) >> 26) VIRTUAL,
tile_column INTEGER GENERATED ALWAYS AS (CASE
WHEN tmp_zoom <= 13 THEN (tile_index & 0x3FFE000) >> 13
ELSE (tile_index & 0x3FFF8000) >> 15 END) VIRTUAL,
tile_row INTEGER GENERATED ALWAYS AS (CASE
WHEN tmp_zoom <= 13 THEN tile_index & 0x1FFF
ELSE tile_index & 0x7FFF END) VIRTUAL,
@nyurik
nyurik / getmvt.sql
Created October 18, 2019 22:00
OpenMapTiles getmvt(z,x,y) function with empty tile detection
CREATE OR REPLACE FUNCTION getmvt(zoom integer, x integer, y integer)
RETURNS bytea AS $$
SELECT STRING_AGG(mvtl, '') AS mvt FROM (
SELECT IsEmpty, count(*) OVER () AS LayerCount, mvtl FROM (
SELECT CASE zoom <= 8 WHEN TRUE THEN FALSE ELSE ST_WITHIN(ST_GeomFromText('POLYGON(
(0 4096,0 0,4096 0,4096 4096,0 4096))', 3857), ST_COLLECT(mvtgeometry)) END AS IsEmpty, ST_AsMVT(tile, 'water', 4096, 'mvtgeometry') as mvtl FROM (SELECT ST_AsMVTGeom(geometry, TileBBox(zoom, x, y), 4096, 4, true) AS mvtgeometry, class, intermittent FROM layer_water(TileBBox(zoom, x, y), zoom) WHERE ST_AsMVTGeom(geometry, TileBBox(zoom, x, y), 4096, 4, true) IS NOT NULL) AS tile HAVING COUNT(*) > 0
UNION ALL
SELECT FALSE AS IsEmpty, ST_AsMVT(tile, 'waterway', 4096, 'mvtgeometry') as mvtl FROM (SELECT ST_AsMVTGeom(geometry, TileBBox(zoom, x, y), 4096, 4, true) AS mvtgeometry, name, name_en, name_de, NULLIF(tags->'name:ar', '') AS "name:ar", NULLIF(tags->'name:az', '') AS "name:az", NULLIF(tags->'name:be', '') AS "name:be", NULLIF(t