Skip to content

perf(modeling): Excessive vec3.create() allocations in extrudeWalls.js #1424

@jbroll

Description

@jbroll

Summary

extrudeWalls.js creates new vec3 arrays inside nested loops, causing GC pressure during extrusions.

Usage Frequency: HIGH

Called by extrudeFromSlices() which powers all extrusion operations:

  • extrudeLinear() / extrudeLinearGeom2()
  • extrudeRotate()
  • extrudeHelical()

Typical model usage: Every extruded shape. A model with 5 extrusions with 32 segments each = 160+ calls to extrudeWalls, each creating many vec3 arrays.

Problem

File: packages/modeling/src/operations/extrusions/extrudeWalls.js
Lines: 32-39

edges.forEach((edge) => {
  const increment = vec3.subtract(vec3.create(), edge[1], edge[0])  // New array
  vec3.divide(increment, increment, divisor)

  let prev = edge[0]
  for (let i = 1; i <= multiple; ++i) {
    const next = vec3.add(vec3.create(), prev, increment)  // New array in inner loop
    newEdges.push([prev, next])
    prev = next
  }
})

For each edge, this creates 1 + multiple vec3 arrays. With many edges and multiple > 1, this generates significant allocations.

Suggested Fix

Reuse temporary vectors:

const tempIncrement = vec3.create()
edges.forEach((edge) => {
  vec3.subtract(tempIncrement, edge[1], edge[0])
  vec3.divide(tempIncrement, tempIncrement, divisor)

  let prev = edge[0]
  for (let i = 1; i <= multiple; ++i) {
    const next = vec3.clone(prev)  // Still need new arrays for storage
    vec3.add(next, next, tempIncrement)
    newEdges.push([prev, next])
    prev = next
  }
})

Note: The inner loop still needs allocations since edges are stored, but the increment calculation can be reused.

Impact

Medium - contributes to GC pressure during extrusion operations. More noticeable with high segment counts.

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