Skip to content
openedgesPublic

Latest commit

 

History

565 Commits

Folders and files

Repository files navigation

elk-rs

npm crates.io CI License

Pure Rust port of the Eclipse Layout Kernel (ELK), engineered for 1:1 layout parity with Java ELK 0.12.2 and high-throughput execution across native Rust, Node.js (NAPI), and WebAssembly (simd128).

elk-rs ships as:

  • @openedges/elk-layout on npm — drop-in replacement for elkjs with automatic prebuilt native NAPI addons and WebAssembly fallback for Node.js and browsers.
  • elk-layout on crates.io — single facade crate re-exporting all ported org-eclipse-elk-* plugins and the elkjs-compatible JSON layout API.

Highlights

  • 1:1 Parity with Java ELK 0.12.2: 1,989 / 1,989 comparable models match on the full elk-models corpus (0 drift), 196,407 intermediate layered processor states verified across all 1,998 inputs, and 550 / 550 Java matches across NAPI, Web WASM, and Node.js WASM.
  • High Performance: On Apple M1 across 26 synthetic benchmarks (0.12.4), rust_api averages 5.53 ms (2.07x faster across 1,447 realworld models vs 0.12.3 baseline), NAPI ELK.layout averages 5.72 ms (within 0.19 ms of raw Rust), and WASM (simd128 + ELKB v1 binary I/O) averages 9.48 ms (5.4 MB .wasm binary) — ~12x–21x faster than Java ELK 0.12.2 and ~153x–263x faster than elkjs 0.12.2.
  • Drop-In elkjs API: Compatible layout(), knownLayoutAlgorithms(), knownLayoutOptions(), knownLayoutCategories(), Web Worker, and Node.js worker_threads support with no GWT runtime overhead.
  • Cross-Platform Delivery: Prebuilt native NAPI binaries for 6 platforms (macOS, Linux glibc/musl, Windows) with transparent WASM fallback for browsers and other targets.
  • Full Algorithm Coverage: Layered, Stress, Force, MrTree, Radial, DisCo, Rect Packing, SPOrE (Overlap & Compaction), Top-down Packing, VertiFlex, Box, Fixed, and Random.

JavaScript / TypeScript (@openedges/elk-layout)

Installation

npm install @openedges/elk-layout
  • Node.js: Requires Node.js 18 or later. On supported platforms, npm installs the matching native NAPI addon via optionalDependencies; other targets automatically fall back to the bundled Node.js WASM module.
  • Browsers: Uses the bundled ESM WebAssembly module (simd128 enabled; requires Chrome 96+, Firefox 89+, or Safari 16.4+).

Usage

ESM / TypeScript

import ELK from '@openedges/elk-layout';

const elk = new ELK();

const graph = {
  id: 'root',
  layoutOptions: { 'elk.algorithm': 'layered' },
  children: [
    { id: 'n1', width: 30, height: 30 },
    { id: 'n2', width: 30, height: 30 },
  ],
  edges: [{ id: 'e1', sources: ['n1'], targets: ['n2'] }],
};

const laidOut = await elk.layout(graph);

CommonJS

const ELK = require('@openedges/elk-layout');
const elk = new ELK();

elk.layout({
  id: 'root',
  layoutOptions: { 'elk.algorithm': 'layered' },
  children: [
    { id: 'n1', width: 30, height: 30 },
    { id: 'n2', width: 30, height: 30 },
  ],
  edges: [{ id: 'e1', sources: ['n1'], targets: ['n2'] }],
}).then(console.log);

Worker Mode (worker_threads / Web Worker)

const ELK = require('@openedges/elk-layout');

const elk = new ELK({
  workerUrl: require.resolve('@openedges/elk-layout/js/elk-worker.js'),
});

elk.layout(graph).finally(() => elk.terminateWorker());

Prebuilt Native NAPI Platforms

Platform Optional Package
macOS ARM64 (Apple Silicon) @openedges/elk-layout-darwin-arm64
macOS x64 (Intel) @openedges/elk-layout-darwin-x64
Linux x64 (glibc) @openedges/elk-layout-linux-x64-gnu
Linux x64 (musl / Alpine) @openedges/elk-layout-linux-x64-musl
Linux ARM64 (glibc) @openedges/elk-layout-linux-arm64-gnu
Windows x64 (MSVC) @openedges/elk-layout-win32-x64-msvc

See plugins/org.eclipse.elk.js/README.md for the full JS/TS API reference and browser/worker details.

Rust (elk-layout)

Installation

cargo add elk-layout

JSON String API

let graph = r#"{
  "id": "root",
  "layoutOptions": { "elk.algorithm": "layered" },
  "children": [
    { "id": "n1", "width": 30, "height": 30 },
    { "id": "n2", "width": 30, "height": 30 }
  ],
  "edges": [{ "id": "e1", "sources": ["n1"], "targets": ["n2"] }]
}"#;

// Second argument supplies global default layout options as JSON.
let laid_out: String = elk_layout::layout_json(graph, "{}")?;

serde_json::Value API

When you already hold a serde_json::Value, use layout_value to avoid a JSON text round trip:

let graph_value: serde_json::Value = serde_json::from_str(graph)?;
let laid_out_value = elk_layout::layout_value(graph_value, "{}")?;

All underlying ELK plugins (org-eclipse-elk-graph, org-eclipse-elk-core, org-eclipse-elk-alg-*, org-eclipse-elk-graph-json, org-eclipse-elk-conn-gmf) are also published individually at the same version with exact intra-workspace pins and re-exported under elk_layout::graph, elk_layout::core, elk_layout::alg::*, and elk_layout::graph_json. See crates/elk-layout/README.md.

Supported Algorithms

Algorithm ELK ID
Layered org.eclipse.elk.layered
Stress org.eclipse.elk.stress
Force org.eclipse.elk.force
MrTree org.eclipse.elk.mrtree
Radial org.eclipse.elk.radial
DisCo org.eclipse.elk.disco
Rect Packing org.eclipse.elk.rectpacking
SPOrE Overlap org.eclipse.elk.sporeOverlap
SPOrE Compaction org.eclipse.elk.sporeCompaction
Top-down Packing org.eclipse.elk.topdownpacking
VertiFlex org.eclipse.elk.vertiflex
Box / Fixed / Random org.eclipse.elk.box, org.eclipse.elk.fixed, org.eclipse.elk.random

Performance (0.12.4)

Measured on a single machine (Mac mini Macmini9,1, Apple M1 8-core 4P+4E, 8 GB RAM, macOS 27.2, Rust 1.97.1, Node.js 24) using alternating A/B medians across 26 synthetic scenarios (10 iterations, 3 warmups, 5 rounds) and the 1,447-model realworld corpus (3 rounds):

Benchmark / Engine Baseline 0.12.4 Speedup
1,447 realworld models total (rust_api, vs 0.12.3 c7279b48) 2,183.57 ms 1,052.68 ms 2.07x (-51.8%)
26 synthetic mean — rust_native (mimalloc-alloc, vs c7279b48) 7.69 ms 5.89 ms 1.31x (-23.4%, 26/26)
26 synthetic mean — rust_api (layout_json, vs c7279b48) 7.14 ms 5.53 ms 1.29x (-22.6%, 26/26)
26 synthetic mean — NAPI ELK.layout (darwin-arm64, vs Phase 3 dist_p3) 7.24 ms 5.72 ms 1.27x (-21.0%, 26/26)
26 synthetic mean — WASM ELK.layout (simd128 + ELKB v1, vs dist_p3) 12.13 ms 9.48 ms 1.28x (-21.8%, 24/26)

Selected 0.12.4 synthetic scenarios (ms, lower is faster; same-machine Java ELK 0.12.2 reference from tests/upgrade_0_12_2.md):

Scenario rust_api (0.12.4) NAPI ELK.layout WASM ELK.layout Java ELK 0.12.2 (ref)
layered_medium 0.75 0.89 3.74 11.40
layered_xlarge 54.16 54.77 96.72 457.77
force_xlarge 28.79 30.74 37.70 1,059.44
stress_xlarge 38.97 38.83 51.78 1,122.14
radial_xlarge 1.02 1.23 4.08 17.41
mrtree_xlarge 1.51 1.79 5.38 31.81
rectpacking_xlarge 0.64 0.95 1.77 7.99

Full 9-phase methodology, alternating A/B tables, and verification records are published in tests/optimization_0_13_0.md and tests/upgrade_0_12_2.md.

Parity & Quality Verification

Each release is validated against pinned upstream references (external/elk v0.12.2, external/elkjs 0.12.2, external/elk-models):

  • Model Parity: 1,989 / 1,989 comparable models match Java ELK v0.12.2 (0 drift, 0 errors).
  • Strict Phase-Step Trace: 196,407 intermediate layered processor states match across all 1,998 corpus inputs (1,997 layouts + 1 matching UnsupportedGraphException).
  • 3-Way JS Parity: 550 / 550 Java matches on NAPI, browser ESM WASM, and Node.js CJS WASM (547 PASS + 3 ELKJS_DRIFT, 0 failures).
  • Workspace & JS Tests: 1,224 Rust tests + 70 Vitest tests (0 failures), plus recursive 60 ms runtime budget 8 / 8 (max 0.71 ms).
  • Memory & Lifetime Regression: 1,998 / 1,998 full-corpus release memory_allocations pass with 0 retained bytes and 0 retained allocations (including exception unwind paths), plus 0 bytes steady-state WASM heap growth across 60 consecutive layouts.

See tests/optimization_0_13_0.md, tests/upgrade_0_12_2.md, and CHANGELOG.md for the complete 0.12.4 verification report and release notes.

Building from Source

Prerequisites

  • Rust 1.97.1 (rust-toolchain.toml / mise.toml), with wasm32-unknown-unknown target for WASM builds
  • Node.js 24 (mise.toml) for JS tests and NAPI/WASM packaging
  • wasm-pack and @napi-rs/cli (for building the npm package locally)
  • Full parity runs against Java ELK require JDK 21 and Maven (see TESTING.md § 1)

Rust Workspace

git submodule update --init --recursive
cargo build --workspace
cargo clippy --workspace --all-targets
cargo test --workspace

JS / NAPI / WASM Package

cd plugins/org.eclipse.elk.js
sh build.sh                    # Browser ESM WASM + Node.js CJS WASM + local NAPI addon
npm install --ignore-scripts
npm test

Repository Layout & Documentation

  • crates/elk-layout/: crates.io facade crate re-exporting all plugins and JSON APIs
  • plugins/org.eclipse.elk.*: 1:1 Rust ports of ELK plugins, plus org.eclipse.elk.napi, org.eclipse.elk.wasm, and org.eclipse.elk.js
  • scripts/: build, parity, benchmark, and release automation scripts
  • tests/: parity reports, validation docs, and benchmarks
  • external/: pinned upstream submodules (elk, elkjs, elk-models) — read-only reference trees; do not modify files under external/
Document Purpose
CHANGELOG.md User-facing release history
VERSIONING.md Versioning, branching, submodule pinning, and publishing policy
TESTING.md Environment setup, verification gates, and release checklist
tests/optimization_0_13_0.md 0.12.4 9-phase optimization benchmarks and verification gates
tests/upgrade_0_12_2.md 0.12.3 (ELK 0.12.2) porting, parity, and performance report
tests/PARITY.md Parity verification architecture and rules

License

Eclipse Public License 2.0 (EPL-2.0 OR GPL-3.0-or-later). This Source Code may also be made available under GPL-3.0-or-later when the conditions for secondary licensing set forth in EPL-2.0 are satisfied, matching Java ELK 0.12.2. See LICENSE and NOTICE.