Skip to content

perf(modeling): O(n²) array concatenation in scissionGeom3.js #1420

@jbroll

Description

@jbroll

Summary

scissionGeom3.js uses Array.concat() inside nested loops, causing O(n²) memory operations.

Usage Frequency: LOW

Used only by scission() operation which splits a geometry into disconnected pieces. Most typical models don't use scission - it's a specialized operation for:

  • Separating imported models that contain multiple parts
  • Post-processing boolean results that created separate islands

Typical model usage: Rare - maybe 1% of models

Problem

File: packages/modeling/src/operations/booleans/scissionGeom3.js
Lines: 40-42

const indexesPerPolygon = polygons.map((polygon) => {
  let indexes = []
  polygon.vertices.forEach((point) => {
    indexes = indexes.concat(findMapping(indexesPerPoint, vec3.snap(temp, point, eps)))
  })
  return { e: 1, d: sortNb(indexes) }
})

For each vertex of each polygon, concat() creates a new array. With N polygons averaging V vertices, this is O(N*V) array allocations.

Suggested Fix

Use push() with spread:

const indexesPerPolygon = polygons.map((polygon) => {
  const indexes = []
  polygon.vertices.forEach((point) => {
    indexes.push(...findMapping(indexesPerPoint, vec3.snap(temp, point, eps)))
  })
  return { e: 1, d: sortNb(indexes) }
})

Impact

Low overall (rare operation), but when used on complex geometry it can be slow.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions