Skip to content

Rewrite kothic in Rust - #38

Open
meenbeese wants to merge 1 commit into
organicmaps:masterfrom
meenbeese:rust
Open

Rewrite kothic in Rust#38
meenbeese wants to merge 1 commit into
organicmaps:masterfrom
meenbeese:rust

Conversation

@meenbeese

@meenbeese meenbeese commented Aug 10, 2026

Copy link
Copy Markdown

Rust rewrite of kothic

This PR replaces the Python kothic MapCSS parser/processor and drules generator
with a complete Rust implementation. The behavior and output are byte-identical
to the Python reference
; the native drawing-rules wire format is unchanged.

What changed

  • The whole pipeline is now native Rust: kothic (single style), gen_all
    (all 6 styles), merge_variants and merge_styles (family/merged files).
  • No Python interpreter, no runtime dependencies beyond libc — the binaries are
    self-contained (LTO release builds, ~3 MB each).
  • All Python-isms from the port were removed: CamelCase methods renamed to
    idiomatic snake_case (updateStylesupdate_styles, testChains
    test_chains, parseZoomparse_zoom, addZoom/addCondition/addStyles
    add_*, …), whatever_to_rgb/hex/cairoparse_color_*, Python-flavored
    helpers (Number()number(), py_list_reprlist_repr,
    komap_mapswithmegenerate_drules), dead code dropped (empty newGroup(),
    unused TOKEN_SUBPART, the unused Parser._src field), and the
    #![allow(non_snake_case)] banners are gone.

Speed

On my personal machine, generating all 6 Organic Maps styles (gen_all):

time peak RSS
Python ~14.6 s ~109 MB
Rust ~2.4 s ~47 MB

6× faster than Python, and peak memory usage is less than half of before.

Verified byte-identical output

Rust and Python were both run against the real Organic Maps data/ tree:

  • All 6 single-variant *.bin and *.txt drules files: identical.
  • Side files types.txt, classificator.txt, visibility.txt, colors.txt,
    patterns.txt: identical.
  • The full generate_drules.sh flow (single-style → merge_variants
    merge_styles) reproduces the committed
    drules_default/outdoors/vehicle.{bin,txt}, drules_design.bin and
    drules_merged.{bin,txt} byte-for-byte.

Tests / lint

  • cargo build clean, cargo test: all 65 tests pass.
  • cargo clippy --all-targets only produces 3 warnings.

@meenbeese

Copy link
Copy Markdown
Author

PTAL @biodranik

@biodranik

Copy link
Copy Markdown
Member

What is the motivation for the rewrite? Only the speed? Is speed now the main issue with Kothic?

Python code is easier to maintain/validate by humans. I like Rust, but we don't have enough experts at the moment to maintain/review these changes efficiently. How good is your experience with Rust and this Kothic code?

@meenbeese

Copy link
Copy Markdown
Author

My motivation with the rewrite was to mostly improve the architecture, but the speed improvement doesn't hurt at all. I find this Rust version way easier to understand and review, and we can also split into more modules as needed. I think for a tool like this, it makes more sense to use a fast and compiled language that produces a single binary.

@meenbeese

Copy link
Copy Markdown
Author

Rebased on top of, and integrated the changes from master.

@meenbeese

Copy link
Copy Markdown
Author

@biodranik It would be great to merge this PR (I will make a subsequent PR on the main repo to integrate the new version), as I want to pursue the open style issues and the Rust version is so much more easier to understand and work with, which is also the case for LLMs and agentic coding tools as well.

Signed-off-by: Kuzey Bilgin <kuzeybilgin@proton.me>
@biodranik

Copy link
Copy Markdown
Member

I have doubts, because we don't have other contributors to do quality code reviews for vibecoded rust. Blindly trusting LLMs is not a great idea.

Which model/efforts did you use to port btw?

@meenbeese

Copy link
Copy Markdown
Author

Well, I don't think converting to Rust suddenly makes the code more complicated. And it can be a learning opportunity as well :) I tried to keep the old structure as much as possible and didn't use too much idiomatic Rust.

I used the Codex cli fyi.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants