@jbroll/jscad-modeling 2.12.8 → 2.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,143 @@
1
+ /**
2
+ * Direct benchmark for splitPolygonByPlane - the hot path in boolean operations
3
+ *
4
+ * Run with: node --expose-gc bench/splitPolygon.bench.js
5
+ */
6
+
7
+ const { sphere, torus } = require('../src/primitives')
8
+ const splitPolygonByPlane = require('../src/operations/booleans/trees/splitPolygonByPlane')
9
+ const poly3 = require('../src/geometries/poly3')
10
+ const plane = require('../src/maths/plane')
11
+
12
+ // Benchmark helper
13
+ const benchmark = (name, setup, fn, iterations = 1000) => {
14
+ const data = setup()
15
+
16
+ // Force GC if available
17
+ if (global.gc) global.gc()
18
+
19
+ // Warmup
20
+ for (let i = 0; i < 100; i++) fn(data)
21
+
22
+ if (global.gc) global.gc()
23
+ const heapBefore = process.memoryUsage().heapUsed
24
+
25
+ const start = process.hrtime.bigint()
26
+ for (let i = 0; i < iterations; i++) {
27
+ fn(data)
28
+ }
29
+ const end = process.hrtime.bigint()
30
+
31
+ if (global.gc) global.gc()
32
+ const heapAfter = process.memoryUsage().heapUsed
33
+
34
+ const totalNs = Number(end - start)
35
+ const avgNs = totalNs / iterations
36
+ const avgUs = avgNs / 1000
37
+ const heapDelta = (heapAfter - heapBefore) / 1024
38
+
39
+ console.log(`${name.padEnd(50)} ${avgUs.toFixed(2).padStart(8)} µs/op heap: ${heapDelta > 0 ? '+' : ''}${heapDelta.toFixed(0)} KB`)
40
+ return avgUs
41
+ }
42
+
43
+ console.log('='.repeat(80))
44
+ console.log('splitPolygonByPlane Direct Benchmark')
45
+ console.log('='.repeat(80))
46
+ console.log()
47
+
48
+ // Get polygons from a sphere
49
+ const testSphere = sphere({ radius: 5, segments: 32 })
50
+ const polygons = testSphere.polygons
51
+
52
+ console.log(`Test geometry: sphere(32) with ${polygons.length} polygons`)
53
+ console.log(`Average vertices per polygon: ${(polygons.reduce((sum, p) => sum + p.vertices.length, 0) / polygons.length).toFixed(1)}`)
54
+ console.log()
55
+
56
+ // Test 1: Coplanar case (fast path - type 0 or 1)
57
+ console.log('--- Coplanar Cases (fast path) ---')
58
+ benchmark('coplanar-front (type 0)', () => {
59
+ const polygon = polygons[0]
60
+ const pplane = poly3.plane(polygon)
61
+ return { polygon, splane: pplane }
62
+ }, (data) => splitPolygonByPlane(data.splane, data.polygon), 10000)
63
+
64
+ // Test 2: Entirely front (type 2)
65
+ console.log()
66
+ console.log('--- One-side Cases (no split needed) ---')
67
+ benchmark('entirely front (type 2)', () => {
68
+ const polygon = polygons[0]
69
+ // Create a plane far behind the polygon
70
+ const splane = [0, 0, 1, -100]
71
+ return { polygon, splane }
72
+ }, (data) => splitPolygonByPlane(data.splane, data.polygon), 10000)
73
+
74
+ benchmark('entirely back (type 3)', () => {
75
+ const polygon = polygons[0]
76
+ // Create a plane far in front of the polygon
77
+ const splane = [0, 0, 1, 100]
78
+ return { polygon, splane }
79
+ }, (data) => splitPolygonByPlane(data.splane, data.polygon), 10000)
80
+
81
+ // Test 3: Spanning case (type 4) - this is where allocations hurt
82
+ console.log()
83
+ console.log('--- Spanning Cases (allocations happen here) ---')
84
+ benchmark('spanning split (type 4) - triangle', () => {
85
+ // A triangle that will be split
86
+ const polygon = poly3.create([[0, 0, 0], [10, 0, 0], [5, 10, 0]])
87
+ const splane = [1, 0, 0, 5] // Split down the middle
88
+ return { polygon, splane }
89
+ }, (data) => splitPolygonByPlane(data.splane, data.polygon), 10000)
90
+
91
+ benchmark('spanning split (type 4) - quad', () => {
92
+ const polygon = poly3.create([[0, 0, 0], [10, 0, 0], [10, 10, 0], [0, 10, 0]])
93
+ const splane = [1, 0, 0, 5]
94
+ return { polygon, splane }
95
+ }, (data) => splitPolygonByPlane(data.splane, data.polygon), 10000)
96
+
97
+ benchmark('spanning split (type 4) - hexagon', () => {
98
+ const polygon = poly3.create([
99
+ [0, 0, 0], [5, 0, 0], [7.5, 4, 0],
100
+ [5, 8, 0], [0, 8, 0], [-2.5, 4, 0]
101
+ ])
102
+ const splane = [1, 0, 0, 2.5]
103
+ return { polygon, splane }
104
+ }, (data) => splitPolygonByPlane(data.splane, data.polygon), 10000)
105
+
106
+ // Test 4: Realistic mix from actual boolean operation
107
+ console.log()
108
+ console.log('--- Realistic Mix (simulating boolean op) ---')
109
+ benchmark('mixed types from sphere polygons', () => {
110
+ // Use multiple polygons with a plane that hits various cases
111
+ const splane = [0.577, 0.577, 0.577, 0] // Diagonal plane through origin
112
+ return { polygons: polygons.slice(0, 50), splane }
113
+ }, (data) => {
114
+ let spanning = 0
115
+ for (const polygon of data.polygons) {
116
+ const result = splitPolygonByPlane(data.splane, polygon)
117
+ if (result.type === 4) spanning++
118
+ }
119
+ return spanning
120
+ }, 1000)
121
+
122
+ // Measure allocation overhead specifically
123
+ console.log()
124
+ console.log('--- Allocation Stress Test ---')
125
+ benchmark('10k spanning splits (allocation heavy)', () => {
126
+ const polygon = poly3.create([[0, 0, 0], [10, 0, 0], [10, 10, 0], [0, 10, 0]])
127
+ const splane = [1, 0, 0, 5]
128
+ return { polygon, splane }
129
+ }, (data) => {
130
+ for (let i = 0; i < 100; i++) {
131
+ splitPolygonByPlane(data.splane, data.polygon)
132
+ }
133
+ }, 100)
134
+
135
+ console.log()
136
+ console.log('='.repeat(80))
137
+ console.log('Benchmark complete')
138
+ console.log()
139
+ console.log('Note: "spanning split" cases allocate the most:')
140
+ console.log(' - result object { type, front, back }')
141
+ console.log(' - vertexIsBack[] array')
142
+ console.log(' - frontvertices[] and backvertices[] arrays')
143
+ console.log(' - Two new poly3 objects (front and back)')