-
Notifications
You must be signed in to change notification settings - Fork 137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reduce memory pressure from compat layer by disabling Heimdall node gathering during OneShotPlugin #915
Reduce memory pressure from compat layer by disabling Heimdall node gathering during OneShotPlugin #915
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,27 +2,65 @@ import Plugin from 'broccoli-plugin'; | |
import { Node } from 'broccoli-node-api'; | ||
import { Builder } from 'broccoli'; | ||
import { copySync } from 'fs-extra'; | ||
import heimdall from 'heimdalljs'; | ||
|
||
class NerfHeimdallReparentingBuilder extends Builder { | ||
/* | ||
Replace the code used to track heimdall nodes: https://github.com/broccolijs/broccoli/blob/v3.5.2/lib/builder.ts#L463-L503 | ||
|
||
This reduces the amount of memory that these one-shot's create by: | ||
|
||
- Avoiding creating Heimdall nodes for each broccoli plugin | ||
- Disabling the "re-parenting" process done by Broccoli builder (which ends up creating **double** the heimdall nodes) | ||
*/ | ||
setupHeimdall() {} | ||
buildHeimdallTree() {} | ||
} | ||
|
||
// Wraps a broccoli tree such that it (and everything it depends on) will only | ||
// build a single time. | ||
export default class OneShot extends Plugin { | ||
private builder: Builder; | ||
private didBuild = false; | ||
private builder: NerfHeimdallReparentingBuilder | null; | ||
|
||
constructor(originalTree: Node) { | ||
constructor(originalTree: Node, private addonName: string) { | ||
// from broccoli's perspective, we don't depend on any input trees! | ||
super([], { | ||
annotation: `@embroider/compat: ${addonName}`, | ||
persistentOutput: true, | ||
}); | ||
this.builder = new Builder(originalTree); | ||
|
||
// create a nested builder in order to isolate the specific addon | ||
this.builder = new NerfHeimdallReparentingBuilder(originalTree); | ||
} | ||
|
||
async build() { | ||
if (this.didBuild) { | ||
const { builder } = this; | ||
|
||
// only build the first time | ||
if (builder === null) { | ||
return; | ||
} | ||
await this.builder.build(); | ||
copySync(this.builder.outputPath, this.outputPath, { dereference: true }); | ||
await this.builder.cleanup(); | ||
this.didBuild = true; | ||
this.builder = null; | ||
|
||
// Make a heimdall node so that we know for sure, all nodes created during our | ||
// inner builder can be remove easily | ||
const oneshotCookie = heimdall.start({ | ||
name: `@embroider/compat: OneShot (${this.addonName})`, | ||
}); | ||
const oneshotHeimdallNode = heimdall.current; | ||
|
||
try { | ||
await builder.build(); | ||
copySync(builder.outputPath, this.outputPath, { dereference: true }); | ||
await builder.cleanup(); | ||
} finally { | ||
oneshotCookie.stop(); | ||
/* | ||
Remove any of the current node's direct children, this ensures that we do not bloat the | ||
current Broccoli builder's heimdall node graph (e.g. the one that is calling | ||
OneShotPlugin; **not** the one that the OneShotPlugin internally creates). | ||
*/ | ||
oneshotHeimdallNode.remove(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should allow you to disable this based on addon name if you want to see detail |
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
declare module 'heimdalljs' { | ||
export default any | ||
} | ||
Comment on lines
+1
to
+3
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, this is bad and I feel bad. We should fix the types publishing of [email protected] if we actually want good types here. I'm not sure I want to shave that particular yak at the moment... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mean, the existence of OneShot also seems bad and I feel bad. 😅 Do you have any sense for whether BROCOLLI_ENABLED_MEMOIZE is going to get widely enabled? It would let us drop OneShot. We could also profile how much OneShot is really saving us on rebuilds. I think it mattered when I added it, but a lot has changed since then. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The main problem is that we need to provide a reasonable escape hatch that isn't "restart the server". @stefanpenner and I have chatted about a few options, I think that the best one was to add a way to trigger a full rebuild (e.g. ignoring the memoization for that specific rebuild). The two ways we thought of doing that were something like
I suspect it still saves us quite a bit of time, and will until we start moving the needle on usage of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OneShot will already be the name of this broccoli-plugin this will look like
OneShot > @embroider/compat: OneShot addonName