Skip to content

Commit

Permalink
port 442-call-stack for eps-build
Browse files Browse the repository at this point in the history
  • Loading branch information
ToddFincannonEI committed Mar 12, 2024
1 parent 806b3be commit ccf5f92
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions packages/compile/src/model/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,14 +352,12 @@ function removeUnusedVariables(spec) {
// ensures that we include all subscripts for a variable, which might mean we
// include some subscripts that aren't needed, but it is safer than trying to
// eliminate those and possibly omit something that is needed.
const referencedVarNames = []
const referencedVarNames = new Set()

// Add the given variable name to the list of referenced variables, if it's not
// already there.
const recordUsedVarName = varName => {
if (!referencedVarNames.includes(varName)) {
referencedVarNames.push(varName)
}
referencedVarNames.add(varName)
}

// Add the given variable to the list of referenced variables, and do the same for
Expand Down Expand Up @@ -400,18 +398,23 @@ function removeUnusedVariables(spec) {
// that are referenced by this variable, either directly (`v.references`) or
// in an "INITIAL" expression (`v.initReferences`). It's OK if we end up with
// duplicates in this list, because we will examine each reference only once.
let refIds = refIdsWithName(v.varName)
refIds = refIds.concat(v.references)
refIds = refIds.concat(v.initReferences)
for (const refId of refIds) {
let refStack = []
function pushRefs(v) {
refStack.push(...refIdsWithName(v.varName))
refStack.push(...v.references)
refStack.push(...v.initReferences)
}
pushRefs(v)
while (refStack.length > 0) {
const refId = refStack.pop()
if (!referencedRefIds.has(refId)) {
referencedRefIds.add(refId)
const refVar = varWithRefId(refId)
if (refVar) {
recordUsedVariable(refVar)
recordRefsOfVariable(refVar)
pushRefs(refVar)
} else {
console.error(`No var found for ${refId}`)
throw new Error(`No var found for ${refId} when recording references for ${v.varName}`)
console.error(v)
process.exit(1)
}
Expand Down Expand Up @@ -445,7 +448,7 @@ function removeUnusedVariables(spec) {
}

// Filter out unneeded variables so we're left with the minimal set of variables to emit
variables = R.filter(v => referencedVarNames.includes(v.varName), variables)
variables = R.filter(v => referencedVarNames.has(v.varName), variables)

// Rebuild the variables-by-name map
variablesByName.clear()
Expand Down

0 comments on commit ccf5f92

Please sign in to comment.