Skip to content

perf(modeling): Expensive string key generation for Map lookups in scissionGeom3.js #1426

@jbroll

Description

@jbroll

Summary

scissionGeom3.js converts vec3 arrays to strings for use as Map keys, which is expensive when done repeatedly.

Usage Frequency: LOW

Only used by scission() operation.

Typical model usage: Rare - maybe 1% of models

Problem

File: packages/modeling/src/operations/booleans/scissionGeom3.js
Lines: 9-22, 35

const insertMapping = (map, point, index) => {
  const key = \`\${point}\`  // Calls toString on array, creates string
  const mapping = map.get(key)
  if (mapping === undefined) {
    map.set(key, [index])
  } else {
    mapping.push(index)
  }
}

const findMapping = (map, point) => {
  const key = \`\${point}\`  // String creation again
  return map.get(key)
}

Called in loops:

polygons.forEach((polygon, index) => {
  polygon.vertices.forEach((point) => {
    insertMapping(indexesPerPoint, vec3.snap(temp, point, eps), index)
  })
})

For a geometry with 10,000 vertices, this creates 10,000+ strings just for lookups.

Suggested Fix

Consider alternative approaches:

  1. Use a spatial hash with integer keys
  2. Cache the string representation on first computation
  3. Use a custom Map implementation that handles vec3 keys directly
// Option 1: Spatial hash with integer key
const spatialKey = (point, eps) => {
  const scale = 1 / eps
  return \`\${Math.round(point[0] * scale)},\${Math.round(point[1] * scale)},\${Math.round(point[2] * scale)}\`
}

// This still creates strings but they're shorter and computed once per lookup

Impact

Low overall (rare operation), but contributes to overhead when scission is used.

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