-
-
Notifications
You must be signed in to change notification settings - Fork 158
perf(hir): lower static method literals directly #8793
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Static-key object literals containing methods now use the ordinary final-shape object lowering when every value is independent of the hidden home object. This removes the synthetic builder closure and property-by-property mutation while preserving evaluation order, dynamic `this`, capture semantics, and inferred method names; `super`, computed keys, spreads, accessors, prototype setters, and other source-ordered forms retain the fail-closed builder path. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| //! Runtime regression for direct lowering of static-key method literals. | ||
| //! | ||
| //! Method-only object literals without a `super` home dependency can use the | ||
| //! ordinary final-shape object path instead of a synthetic builder IIFE. The | ||
| //! controls below cover the source-ordered forms that must retain the IIFE. | ||
|
|
||
| use std::path::{Path, PathBuf}; | ||
| use std::process::{Command, Output}; | ||
| use std::sync::Once; | ||
|
|
||
| fn perry_bin() -> PathBuf { | ||
| PathBuf::from(env!("CARGO_BIN_EXE_perry")) | ||
| } | ||
|
|
||
| fn workspace_root() -> PathBuf { | ||
| PathBuf::from(env!("CARGO_MANIFEST_DIR")) | ||
| .join("../..") | ||
| .canonicalize() | ||
| .expect("canonicalize workspace root") | ||
| } | ||
|
|
||
| fn runtime_dir() -> PathBuf { | ||
| static BUILD_RUNTIME: Once = Once::new(); | ||
| BUILD_RUNTIME.call_once(|| { | ||
| let cargo = std::env::var_os("CARGO").unwrap_or_else(|| "cargo".into()); | ||
| let mut command = Command::new(cargo); | ||
| command.current_dir(workspace_root()).arg("build"); | ||
| if !cfg!(debug_assertions) { | ||
| command.arg("--release"); | ||
| } | ||
| let build = command | ||
| .args(["-p", "perry-runtime-static"]) | ||
| .output() | ||
| .expect("build static runtime archive"); | ||
| assert!( | ||
| build.status.success(), | ||
| "static runtime build failed\nstdout:\n{}\nstderr:\n{}", | ||
| String::from_utf8_lossy(&build.stdout), | ||
| String::from_utf8_lossy(&build.stderr) | ||
| ); | ||
| }); | ||
|
|
||
| perry_bin() | ||
| .parent() | ||
| .expect("Perry binary directory") | ||
| .to_path_buf() | ||
| } | ||
|
|
||
| fn run_fixture(binary: &Path, force_evacuation: bool) -> Output { | ||
| let mut command = Command::new(binary); | ||
| if force_evacuation { | ||
| command | ||
| .env("PERRY_GC_FORCE_EVACUATE", "1") | ||
| .env("PERRY_GC_VERIFY_EVACUATION", "1"); | ||
| } else { | ||
| command | ||
| .env_remove("PERRY_GC_FORCE_EVACUATE") | ||
| .env_remove("PERRY_GC_VERIFY_EVACUATION"); | ||
| } | ||
| command.output().expect("run method-literal fixture") | ||
| } | ||
|
|
||
| #[test] | ||
| fn direct_method_literal_preserves_semantics_and_iife_controls() { | ||
| let dir = tempfile::tempdir().expect("tempdir"); | ||
| let entry = dir.path().join("main.ts"); | ||
| let binary = dir.path().join("main_bin"); | ||
| std::fs::write( | ||
| &entry, | ||
| r#" | ||
| let order = ""; | ||
| function evaluated(label: string, value: number): number { | ||
| order += label; | ||
| const churn: any[] = []; | ||
| for (let i = 0; i < 256; i++) churn.push({ i }); | ||
| return value; | ||
| } | ||
|
|
||
| const outer = { offset: 7 }; | ||
| const fast: any = { | ||
| first: evaluated("a", 1), | ||
| captured(x: number) { return outer.offset + x; }, | ||
| dynamicThis(x: number) { return this.first + x; }, | ||
| last: evaluated("b", 3), | ||
| }; | ||
|
|
||
| const base: any = { read() { return 10; } }; | ||
| const withSuper: any = { read() { return super.read() + 1; } }; | ||
| Object.setPrototypeOf(withSuper, base); | ||
|
|
||
| const computedKey = "computed"; | ||
| const computed: any = { [computedKey]() { return 9; } }; | ||
|
|
||
| let getterCalls = 0; | ||
| const accessor: any = { | ||
| get value() { getterCalls++; return 11; }, | ||
| }; | ||
|
|
||
| const spread: any = { ...{ x: 1 }, method() { return 2; } }; | ||
|
|
||
| console.log( | ||
| order + ":" + Object.keys(fast).join(",") + ":" + fast.captured(5) + ":" + | ||
| fast.dynamicThis(5) + ":" + fast.captured.name + ":" + fast.dynamicThis.name + | ||
| ":" + withSuper.read() + ":" + computed.computed() + ":" + accessor.value + | ||
| ":" + getterCalls + ":" + (spread.x + spread.method()), | ||
| ); | ||
| "#, | ||
| ) | ||
| .expect("write method-literal fixture"); | ||
|
|
||
| let compile = Command::new(perry_bin()) | ||
| .current_dir(dir.path()) | ||
| .arg("compile") | ||
| .arg(&entry) | ||
| .arg("-o") | ||
| .arg(&binary) | ||
| .arg("--no-cache") | ||
| .arg("--no-auto-optimize") | ||
| .env("PERRY_RUNTIME_DIR", runtime_dir()) | ||
| .output() | ||
| .expect("compile method-literal fixture"); | ||
| assert!( | ||
| compile.status.success(), | ||
| "compile failed\nstdout:\n{}\nstderr:\n{}", | ||
| String::from_utf8_lossy(&compile.stdout), | ||
| String::from_utf8_lossy(&compile.stderr) | ||
| ); | ||
|
|
||
| for force_evacuation in [false, true] { | ||
| let run = run_fixture(&binary, force_evacuation); | ||
| assert!( | ||
| run.status.success(), | ||
| "fixture failed (force_evacuation={force_evacuation})\nstdout:\n{}\nstderr:\n{}", | ||
| String::from_utf8_lossy(&run.stdout), | ||
| String::from_utf8_lossy(&run.stderr) | ||
| ); | ||
| assert_eq!( | ||
| String::from_utf8_lossy(&run.stdout), | ||
| "ab:first,captured,dynamicThis,last:12:6:captured:dynamicThis:11:9:11:1:3\n" | ||
| ); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.