[C1][C2] DXF-/DWG-Import in Rust über acadrust; dxf-parser und libredwg-web entfernt
- fileio/src/import.rs: DXF (ASCII/Binär, R12–2018+) und DWG (R13–2018) lesen und wie der bisherige TS-Parser abbilden (Konturen inkl. Bulges, Kreise/Bögen mit Kurve, Ellipsen, Splines inkl. periodischer, Schraffuren, TEXT/MTEXT, 3DFACE/Polyface/Polygon-Mesh/MESH je Ebene, INSERT/MINSERT rekursiv, Ebenenfarben, $INSUNITS, Histogramm); Übergabe als flacher f64-Puffer + String-Tabelle; kaputte Dateien → Fehlermeldung (streng, dann tolerant). - fileio/src/split.rs: Aufteilung grosser ASCII-DXF für parallele Worker (Port von io/dxfEntityChunker.ts, jetzt auf Bytes, ab 4 MB); common.rs: Ebenenfarbe, Format-Erkennung. - io/dxfParser.ts / dwgParser.ts: dünne Fassaden (Rückgabetyp unverändert, neu insUnits), Worker liest Bytes und überträgt den Puffer; App liest DXF als Bytes (Binär-DXF, Code-Seiten). - Entfernt: dxf-parser, @mlightcad/libredwg-web (package.json/Lockfile), Vite-Aliase, libredwg-web.d.ts, dxfEntityChunker.ts, scripts/probe-dwg.mjs. Vite: Worker als ES-Module. - Tests: bestehende DXF-Tests unverändert grün; DWG-/Binär-DXF-Tests mit Testdateien aus acadrust-Rundreise (Polyface in Blöcken auf Roof/Wall, gleiche Erwartungen wie früher); Rust-Tests inkl. Rundreise ASCII/Binär/DWG und Aufteilung. - Benchmark-Skript bench-dxf-parallel.mjs auf den Rust-Import umgestellt.
This commit is contained in:
38 files changed
+2573
-2992
No files matched your search
+67
-53
@@ -1,24 +1,27 @@
|
||||
// Misst die parallele Chunk-Aufteilung (`dxfEntityChunker.ts`) gegen eine
|
||||
// ECHTE DXF-Datei: (1) Korrektheit — Gesamtzahlen (Konturen/Punkte/Texte/
|
||||
// Meshes) müssen zwischen Einzel-Pass und Chunk-Parallel-Pass IDENTISCH
|
||||
// sein; (2) Wall-Clock-Gewinn über echte `worker_threads` (Node-Äquivalent
|
||||
// der Browser-`Worker`-API, die im Produktionscode genutzt wird).
|
||||
// Misst den DXF-/DWG-Import (Rust-Format-Modul, `src/io/dxfParser.ts`) an einer ECHTEN Datei:
|
||||
// (1) Einzel-Pass, (2) für grosse ASCII-DXF die parallele Aufteilung (`split.rs`) über echte
|
||||
// `worker_threads` (Node-Äquivalent der Browser-Worker in `dxfParserAsync.ts`). Korrektheit:
|
||||
// Gesamtzahlen (Konturen/Punkte/Texte/Meshes) müssen zwischen beiden Läufen IDENTISCH sein.
|
||||
//
|
||||
// Lauf: `node scripts/bench-dxf-parallel.mjs <pfad.dxf> [workerPfad]`
|
||||
// Lauf: `node scripts/bench-dxf-parallel.mjs <pfad.dxf|pfad.dwg>` (vorher `npm run build:fileio`).
|
||||
|
||||
import { readFileSync } from "node:fs";
|
||||
import { resolve } from "node:path";
|
||||
import { Worker } from "node:worker_threads";
|
||||
import { parseDxf } from "../src/io/dxfParser";
|
||||
import { splitDxfForParallelParse, DXF_CHUNK_CLOSER } from "../src/io/dxfEntityChunker";
|
||||
import { initFileioSync } from "../src/engine/fileioWasm";
|
||||
import { decodeImport, importDrawingFlat } from "../src/io/dxfParser";
|
||||
import type { DxfImportResult } from "../src/io/dxfParser";
|
||||
|
||||
const path = process.argv[2];
|
||||
const workerPath = process.argv[3];
|
||||
if (!path || !workerPath) {
|
||||
console.error("Pfad zur DXF-Datei und zum gebündelten Worker-Skript angeben.");
|
||||
console.error("Pfad zur DXF-/DWG-Datei und zum gebündelten Worker-Skript angeben.");
|
||||
process.exit(1);
|
||||
}
|
||||
const wasmPath = resolve("src/engine/pkgFileIo/fileio_bg.wasm");
|
||||
const CLOSER = new TextEncoder().encode("0\nENDSEC\n0\nEOF\n");
|
||||
|
||||
function countAll(res: { contours: { contours: { pts: unknown[] }[] }[]; texts?: unknown[]; meshes: unknown[] }) {
|
||||
function countAll(res: DxfImportResult) {
|
||||
let contours = 0;
|
||||
let points = 0;
|
||||
for (const set of res.contours) {
|
||||
@@ -30,73 +33,84 @@ function countAll(res: { contours: { contours: { pts: unknown[] }[] }[]; texts?:
|
||||
return { contours, points, texts: res.texts?.length ?? 0, meshes: res.meshes.length };
|
||||
}
|
||||
|
||||
console.log(`\nParallel-Benchmark: ${path}\n`);
|
||||
const text = readFileSync(path, "utf8");
|
||||
console.log(` Datei: ${(text.length / 1024 / 1024).toFixed(0)} MB Text\n`);
|
||||
console.log(`\nImport-Benchmark: ${path}\n`);
|
||||
const bytes = new Uint8Array(readFileSync(path));
|
||||
console.log(` Datei: ${(bytes.length / 1024 / 1024).toFixed(0)} MB\n`);
|
||||
const t00 = performance.now();
|
||||
const m = await initFileioSync(readFileSync(wasmPath));
|
||||
console.log(` Modul laden: ${(performance.now() - t00).toFixed(0).padStart(7)} ms`);
|
||||
|
||||
// Erster Lauf wärmt die WASM-Engine auf (Tier-up von Liftoff auf TurboFan) — gemessen wird beides.
|
||||
const tw = performance.now();
|
||||
importDrawingFlat(bytes);
|
||||
console.log(` Erster Lauf: ${(performance.now() - tw).toFixed(0).padStart(7)} ms (kalt)`);
|
||||
const t0 = performance.now();
|
||||
const single = parseDxf(text);
|
||||
const flat = importDrawingFlat(bytes);
|
||||
const tRust = performance.now();
|
||||
const single = decodeImport(flat.geom, flat.strings);
|
||||
const singleMs = performance.now() - t0;
|
||||
const singleCounts = countAll(single);
|
||||
console.log(` Einzel-Pass: ${singleMs.toFixed(0).padStart(7)} ms `, singleCounts);
|
||||
console.log(
|
||||
` Einzel-Pass: ${singleMs.toFixed(0).padStart(7)} ms (Rust ${(tRust - t0).toFixed(0)} ms, Dekodieren ${(performance.now() - tRust).toFixed(0)} ms)`,
|
||||
singleCounts,
|
||||
);
|
||||
|
||||
const chunkCount = Math.min(4, 8);
|
||||
const split = splitDxfForParallelParse(text, chunkCount);
|
||||
if (!split) {
|
||||
console.log("\n splitDxfForParallelParse lieferte null (Datei zu klein oder keine ENTITIES-Sektion) — kein Parallel-Lauf möglich.\n");
|
||||
const chunkCount = 4;
|
||||
const tSplit = performance.now();
|
||||
const offsets = Array.from(m.dxf_split_offsets(bytes, chunkCount));
|
||||
const splitMs = performance.now() - tSplit;
|
||||
if (offsets.length < 3) {
|
||||
console.log("\n Keine Aufteilung (zu klein, Binär-DXF/DWG oder keine ENTITIES-Sektion) — kein Parallel-Lauf.\n");
|
||||
process.exit(0);
|
||||
}
|
||||
console.log(`\n Aufgeteilt in ${split.chunks.length} Chunks.\n`);
|
||||
|
||||
for (const [i, chunk] of split.chunks.entries()) {
|
||||
console.log(` Chunk ${i}: ${(chunk.length / 1024 / 1024).toFixed(1)} MB`);
|
||||
const preamble = bytes.subarray(0, offsets[0]);
|
||||
const chunks: Uint8Array[] = [];
|
||||
for (let i = 0; i + 1 < offsets.length; i++) {
|
||||
const body = bytes.subarray(offsets[i], offsets[i + 1]);
|
||||
const c = new Uint8Array(preamble.length + body.length + CLOSER.length);
|
||||
c.set(preamble, 0);
|
||||
c.set(body, preamble.length);
|
||||
c.set(CLOSER, preamble.length + body.length);
|
||||
chunks.push(c);
|
||||
}
|
||||
console.log(`\n Aufgeteilt in ${chunks.length} Stücke (Scan ${splitMs.toFixed(0)} ms).\n`);
|
||||
|
||||
const t1 = performance.now();
|
||||
const spawned: number[] = [];
|
||||
type Msg = { geom: Float64Array; strings: string[]; tGotInput: number; tParsed: number; tPosted: number };
|
||||
const results = await Promise.all(
|
||||
split.chunks.map(
|
||||
(chunk, i) =>
|
||||
new Promise((resolve, reject) => {
|
||||
chunks.map(
|
||||
(data, i) =>
|
||||
new Promise<Msg>((res, rej) => {
|
||||
spawned[i] = performance.now();
|
||||
const w = new Worker(workerPath, {
|
||||
workerData: { text: split.preamble + chunk + DXF_CHUNK_CLOSER, idPrefix: `c${i}-` },
|
||||
});
|
||||
w.once("message", (msg) => {
|
||||
const w = new Worker(workerPath, { workerData: { data, wasmPath } });
|
||||
w.once("message", (msg: Msg) => {
|
||||
w.terminate();
|
||||
resolve(msg);
|
||||
res(msg);
|
||||
});
|
||||
w.once("error", reject);
|
||||
w.once("error", rej);
|
||||
}),
|
||||
),
|
||||
);
|
||||
const decoded = results.map((r, i) => decodeImport(r.geom, r.strings, `c${i}-`));
|
||||
const parallelMs = performance.now() - t1;
|
||||
|
||||
for (const [i, r] of (results as any[]).entries()) {
|
||||
for (const [i, r] of results.entries()) {
|
||||
console.log(
|
||||
` Worker ${i}: Erzeugung→Input ${(r.tGotInput - spawned[i]).toFixed(0)} ms, ` +
|
||||
`Parse ${(r.tParsed - r.tGotInput).toFixed(0)} ms, ` +
|
||||
`(Nachricht gesendet bei t+${(r.tPosted - t1).toFixed(0)} ms)`,
|
||||
` Worker ${i}: Start→Modul ${(r.tGotInput - spawned[i]).toFixed(0)} ms, Rust ${(r.tParsed - r.tGotInput).toFixed(0)} ms`,
|
||||
);
|
||||
}
|
||||
|
||||
type Res = { contours: { contours: { pts: unknown[] }[] }[]; texts?: unknown[]; meshes: unknown[] };
|
||||
const mergedContours = [];
|
||||
const mergedTexts = [];
|
||||
const mergedMeshes = [];
|
||||
for (const { result: r } of results as { result: Res }[]) {
|
||||
for (const c of r.contours) mergedContours.push(c);
|
||||
for (const t of r.texts ?? []) mergedTexts.push(t);
|
||||
for (const m of r.meshes) mergedMeshes.push(m);
|
||||
}
|
||||
const merged = countAll({ contours: mergedContours, texts: mergedTexts, meshes: mergedMeshes });
|
||||
console.log(` Parallel (${split.chunks.length} Worker): ${parallelMs.toFixed(0).padStart(7)} ms `, merged);
|
||||
|
||||
const merged = decoded.reduce(
|
||||
(a, r) => {
|
||||
const c = countAll(r);
|
||||
return { contours: a.contours + c.contours, points: a.points + c.points, texts: a.texts + c.texts, meshes: a.meshes + c.meshes };
|
||||
},
|
||||
{ contours: 0, points: 0, texts: 0, meshes: 0 },
|
||||
);
|
||||
console.log(` Parallel (${chunks.length} Worker): ${(parallelMs + splitMs).toFixed(0).padStart(7)} ms inkl. Scan `, merged);
|
||||
const ok =
|
||||
merged.contours === singleCounts.contours &&
|
||||
merged.points === singleCounts.points &&
|
||||
merged.texts === singleCounts.texts &&
|
||||
merged.meshes === singleCounts.meshes;
|
||||
merged.texts === singleCounts.texts;
|
||||
console.log(`\n Korrektheit: ${ok ? "IDENTISCH ✓" : "ABWEICHUNG ✗"}`);
|
||||
console.log(` Speedup: ${(singleMs / parallelMs).toFixed(2)}x\n`);
|
||||
console.log(` Speedup: ${(singleMs / (parallelMs + splitMs)).toFixed(2)}x\n`);
|
||||
if (!ok) process.exit(1);
|
||||
Reference in new issue
Block a user