Skip to content
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

optimize(core): move internal js src off heap #9713

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions core/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,43 @@ impl JsRuntime {
}
}

/// Same as execute() except intended for internal runtime JS code
/// exposed to v8 as external strings to reduce heap-usage (~1MB) per-isolate
pub fn execute_static(
&mut self,
js_filename: &'static str,
js_source: &'static str,
) -> Result<(), AnyError> {
let context = self.global_context();

let scope = &mut v8::HandleScope::with_context(self.v8_isolate(), context);

let source =
v8::String::new_external_onebyte_static(scope, js_source).unwrap();
let name =
v8::String::new_external_onebyte_static(scope, js_filename).unwrap();
let origin = bindings::script_origin(scope, name);

let tc_scope = &mut v8::TryCatch::new(scope);

let script = match v8::Script::compile(tc_scope, source, Some(&origin)) {
Some(script) => script,
None => {
let exception = tc_scope.exception().unwrap();
return exception_to_err_result(tc_scope, exception, false);
}
};

match script.run(tc_scope) {
Some(_) => Ok(()),
None => {
assert!(tc_scope.has_caught());
let exception = tc_scope.exception().unwrap();
exception_to_err_result(tc_scope, exception, false)
}
}
}

/// Takes a snapshot. The isolate should have been created with will_snapshot
/// set to true.
///
Expand Down
2 changes: 1 addition & 1 deletion op_crates/crypto/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub fn init(isolate: &mut JsRuntime) {
include_str!("01_crypto.js"),
)];
for (url, source_code) in files {
isolate.execute(url, source_code).unwrap();
isolate.execute_static(url, source_code).unwrap();
}
}

Expand Down
4 changes: 2 additions & 2 deletions op_crates/fetch/26_fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@
requiredArguments("FormData.set", arguments.length, 2);
name = String(name);

// If there are any entries in the context objects entry list whose name
// If there are any entries in the context object's entry list whose name
// is name, replace the first such entry with entry and remove the others
let found = false;
let i = 0;
Expand All @@ -350,7 +350,7 @@
i++;
}

// Otherwise, append entry to the context objects entry list.
// Otherwise, append entry to the context object's entry list.
if (!found) {
this[dataSymbol].push([name, parseFormDataValue(value, filename)]);
}
Expand Down
2 changes: 1 addition & 1 deletion op_crates/fetch/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ pub fn init(isolate: &mut JsRuntime) {
),
];
for (url, source_code) in files {
isolate.execute(url, source_code).unwrap();
isolate.execute_static(url, source_code).unwrap();
}
}

Expand Down
2 changes: 1 addition & 1 deletion op_crates/web/02_event.js
Original file line number Diff line number Diff line change
Expand Up @@ -916,7 +916,7 @@
// If signal is not null and its aborted flag is set, then return.
return;
} else {
// If listeners signal is not null, then add the following abort
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This character non-ascii?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really subtle but this is a right single quotation mark (0x2019).
A quick s/’/'/g and we should be good.

// If listener's signal is not null, then add the following abort
// abort steps to it: Remove an event listener.
signal.addEventListener("abort", () => {
this.removeEventListener(type, callback, options);
Expand Down
16 changes: 8 additions & 8 deletions op_crates/web/21_filereader.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@

fr.aborting = false;

// 1. If frs state is "loading", throw an InvalidStateError DOMException.
// 1. If fr's state is "loading", throw an InvalidStateError DOMException.
if (fr.readyState === FileReader.LOADING) {
throw new DOMException(
"Invalid FileReader state.",
"InvalidStateError",
);
}
// 2. Set frs state to "loading".
// 2. Set fr's state to "loading".
fr.readyState = FileReader.LOADING;
// 3. Set frs result to null.
// 3. Set fr's result to null.
fr.result = null;
// 4. Set frs error to null.
// 4. Set fr's error to null.
fr.error = null;

// 5. Let stream be the result of calling get stream on blob.
Expand Down Expand Up @@ -79,9 +79,9 @@
return;
}

// 1. Set frs state to "done".
// 1. Set fr's state to "done".
fr.readyState = FileReader.DONE;
// 2. Let result be the result of package data given bytes, type, blobs type, and encodingName.
// 2. Let result be the result of package data given bytes, type, blob's type, and encodingName.
const size = chunks.reduce((p, i) => p + i.byteLength, 0);
const bytes = new Uint8Array(size);
let offs = 0;
Expand Down Expand Up @@ -115,7 +115,7 @@
fr.dispatchEvent(ev);
}

// 5. If frs state is not "loading", fire a progress event called loadend at the fr.
// 5. If fr's state is not "loading", fire a progress event called loadend at the fr.
//Note: Event handler for the load or error events could have started another load, if that happens the loadend event for this load is not fired.
if (fr.readyState !== FileReader.LOADING) {
const ev = new ProgressEvent("loadend", {
Expand Down Expand Up @@ -143,7 +143,7 @@
fr.dispatchEvent(ev);
}

//If frs state is not "loading", fire a progress event called loadend at fr.
//If fr's state is not "loading", fire a progress event called loadend at fr.
//Note: Event handler for the error event could have started another load, if that happens the loadend event for this load is not fired.
if (fr.readyState !== FileReader.LOADING) {
const ev = new ProgressEvent("loadend", {});
Expand Down
2 changes: 1 addition & 1 deletion op_crates/web/lib.deno_web.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ declare class EventTarget {
*
* When set to true, options's passive indicates that the callback will not
* cancel the event by invoking preventDefault(). This is used to enable
* performance optimizations described in § 2.8 Observing event listeners.
* performance optimizations described in section 2.8 Observing event listeners.
*
* When set to true, options's once indicates that the callback will only be
* invoked once after which the event listener will be removed.
Expand Down
2 changes: 1 addition & 1 deletion op_crates/web/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub fn init(isolate: &mut JsRuntime) {
),
];
for (url, source_code) in files {
isolate.execute(url, source_code).unwrap();
isolate.execute_static(url, source_code).unwrap();
}
}

Expand Down
2 changes: 1 addition & 1 deletion op_crates/webgpu/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ pub fn init(isolate: &mut deno_core::JsRuntime) {
),
];
for (url, source_code) in files {
isolate.execute(url, source_code).unwrap();
isolate.execute_static(url, source_code).unwrap();
}
}

Expand Down
2 changes: 1 addition & 1 deletion op_crates/websocket/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ pub async fn op_ws_next_event(
/// Load and execute the javascript code.
pub fn init(isolate: &mut JsRuntime) {
isolate
.execute(
.execute_static(
"deno:op_crates/websocket/01_websocket.js",
include_str!("01_websocket.js"),
)
Expand Down
29 changes: 15 additions & 14 deletions runtime/js/02_console.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,21 @@
return ArrayBuffer.isView(x) && !(x instanceof DataView);
}

const tableChars = {
middleMiddle: "─",
rowMiddle: "┼",
topRight: "┐",
topLeft: "┌",
leftMiddle: "├",
topMiddle: "┬",
bottomRight: "┘",
bottomLeft: "└",
bottomMiddle: "┴",
rightMiddle: "┤",
left: "│ ",
right: " │",
middle: " │ ",
// Strings are escaped so all JS source is ASCII
var tableChars = {
bottomLeft: "\u2514",
bottomMiddle: "\u2534",
bottomRight: "\u2518",
left: "\u2502 ",
leftMiddle: "\u251c",
middle: " \u2502 ",
middleMiddle: "\u2500",
right: " \u2502",
rightMiddle: "\u2524",
rowMiddle: "\u253c",
topLeft: "\u250c",
topMiddle: "\u252c",
topRight: "\u2510",
};

function isFullWidthCodePoint(code) {
Expand Down