criticalpath.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // SPDX-FileCopyrightText: 2012-2026 MOD Audio UG
  2. // SPDX-License-Identifier: AGPL-3.0-or-later
  3. // Slack of every node in the pedalboard graph.
  4. //
  5. // Plugins run on parallel jack threads, so a plugin's cpu cost and its contribution
  6. // to jack's DSP load are different numbers: only the longest chain through the graph
  7. // bounds how long a cycle takes. A plugin with slack has headroom -- it finishes while
  8. // something slower is still running, so making it cheaper buys nothing. A plugin with
  9. // zero slack is on the critical path, and every microsecond off it is a microsecond
  10. // off the cycle.
  11. //
  12. // weights: {node: cost}, missing nodes count as 0 (hardware ports, unmeasured clients)
  13. // edges: [[from, to], ...], signal flow direction
  14. //
  15. // returns {critical: <cost of longest chain>, slack: {node: cost}}
  16. function graphSlack(weights, edges) {
  17. var nodes = {}
  18. var succ = {}
  19. var pred = {}
  20. var adj = {}
  21. var i, n, m, e
  22. function touch(node) {
  23. if (nodes[node] === undefined) {
  24. nodes[node] = weights[node] || 0
  25. succ[node] = []
  26. pred[node] = []
  27. adj[node] = []
  28. }
  29. }
  30. for (n in weights) { touch(n) }
  31. for (i = 0; i < edges.length; i++) {
  32. touch(edges[i][0])
  33. touch(edges[i][1])
  34. adj[edges[i][0]].push(edges[i][1])
  35. }
  36. // Drop back edges so what is left is a DAG. A pedalboard can contain a feedback
  37. // loop; jack breaks those the same way, by ordering on the edges it kept.
  38. //
  39. // Which edge gets dropped depends on where the walk starts, so start from the
  40. // sources -- the capture ports signal actually enters through -- and only then
  41. // pick up anything left inside a cycle. Sorted throughout: without it the answer
  42. // would change with object key order.
  43. var color = {}
  44. var seen = {}
  45. var incoming = {}
  46. for (n in nodes) { color[n] = 0; incoming[n] = 0 }
  47. for (i = 0; i < edges.length; i++) { incoming[edges[i][1]]++ }
  48. var roots = Object.keys(nodes).sort()
  49. var sources = []
  50. var rest = []
  51. for (i = 0; i < roots.length; i++) {
  52. if (incoming[roots[i]] === 0) { sources.push(roots[i]) } else { rest.push(roots[i]) }
  53. }
  54. roots = sources.concat(rest)
  55. for (n in adj) { adj[n].sort() }
  56. function visit(root) {
  57. var stack = [[root, 0]]
  58. color[root] = 1
  59. while (stack.length) {
  60. var top = stack[stack.length - 1]
  61. var out = adj[top[0]]
  62. if (top[1] >= out.length) {
  63. color[top[0]] = 2
  64. stack.pop()
  65. continue
  66. }
  67. m = out[top[1]++]
  68. if (color[m] === 1) { continue } // back edge, drop it
  69. var key = top[0] + " " + m
  70. if (!seen[key]) {
  71. seen[key] = true
  72. succ[top[0]].push(m)
  73. pred[m].push(top[0])
  74. }
  75. if (color[m] === 0) {
  76. color[m] = 1
  77. stack.push([m, 0])
  78. }
  79. }
  80. }
  81. for (i = 0; i < roots.length; i++) { if (color[roots[i]] === 0) { visit(roots[i]) } }
  82. // Kahn topological order
  83. var indeg = {}
  84. var queue = []
  85. var topo = []
  86. for (n in nodes) { indeg[n] = pred[n].length }
  87. var ordered = Object.keys(nodes).sort()
  88. for (i = 0; i < ordered.length; i++) { if (indeg[ordered[i]] === 0) { queue.push(ordered[i]) } }
  89. while (queue.length) {
  90. n = queue.pop()
  91. topo.push(n)
  92. for (i = 0; i < succ[n].length; i++) {
  93. m = succ[n][i]
  94. if (--indeg[m] === 0) { queue.push(m) }
  95. }
  96. }
  97. // earliest finish, walking forwards
  98. var ef = {}
  99. var critical = 0
  100. for (i = 0; i < topo.length; i++) {
  101. n = topo[i]
  102. var start = 0
  103. for (e = 0; e < pred[n].length; e++) { start = Math.max(start, ef[pred[n][e]]) }
  104. ef[n] = start + nodes[n]
  105. critical = Math.max(critical, ef[n])
  106. }
  107. // latest finish that still fits inside the critical path, walking backwards
  108. var lf = {}
  109. var slack = {}
  110. for (i = topo.length - 1; i >= 0; i--) {
  111. n = topo[i]
  112. var latest = critical
  113. for (e = 0; e < succ[n].length; e++) {
  114. latest = Math.min(latest, lf[succ[n][e]] - nodes[succ[n][e]])
  115. }
  116. lf[n] = latest
  117. slack[n] = latest - ef[n]
  118. }
  119. return { critical: critical, slack: slack }
  120. }