-
Notifications
You must be signed in to change notification settings - Fork 240
enable conditionnal loading for polyfill #847
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
Conversation
Is the reason this is working is that V8 first checks to see if Put differently, i understand why this code works the 2nd time if fetch is called twice. But I don't understand why it works the first time. |
It seems v8 first checks for native implementation and then looks at the pure JS value.
When returning "no", v8 continues to search for the value, falling back to pure JS. Since we inject a polyfill, we define the property in pure JS, so it works... I know this is fragile, if the order change, it won't work anymore. But it was really easier than trying to inject the value manually after loading the polyfill. |
src/browser/polyfill/polyfill.zig
Outdated
pub fn missing(self: *Loader, name: []const u8, js_context: *Env.JsContext) bool { | ||
if (!self.done.fetch and std.mem.eql(u8, name, "fetch")) { | ||
// load the polyfill once. | ||
self.done.fetch = true; |
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.
I will add a comment, but it's important to set the polyfill done immediately.
Indeed, if your polyfill calls the property during its execution, the missing handler will be called too, resulting to an infinite recursive load...
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.
These are going to be called a lot, there could be value in optimizing the string comparison to be a single int equality check. Something like this:
switch (name.len) {
5 => switch (@as(u40, @bitCast(domain[0..5].*))) {
asUint(u40, "fetch") => if (!self.done.fetch) {
self.done.fetch = true;
return load(fetch_src);
},
else => {},
},
else => {},
};
return false;
fn asUint(comptime T: type, comptime string: []const u8) T {
return @bitCast(string[0..string.len].*);
}
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.
Not certain for this case, but it seems the optimized code generation is better for the std.mem.eql, see: https://godbolt.org/z/8a6M5vc6d
Unless we have measurements I would prefer simple code for the users and the compiler.
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.
I changed the code, do you see a good way to optimize this version?
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.
@sjorsdonkers I don't think your godbolt code is right. Your code never calls square
; I think the function isn't even included in the output. The fact that "fetch" doesn't appear in the assembly is a pretty good sign that something isn't quite right.
Try with:
pub fn main() void {
const allocator = std.heap.page_allocator;
const args = std.process.argsAlloc(allocator) catch return;
defer std.process.argsFree(allocator, args);
if (square(args[0])) {
return;
}
if (other_square(args[0])) {
return;
}
}
(Removing the std.debug.print and the error return type makes the generated assembly a little less noisy).
Personally, I find this hard to follow, but the switch seems to only require 1-2 compares (which is what I'd expect), vs the 1-4 for std.mem.eql
. In both cases, you only get 1 compare on length inequality, and for std.mem.eql you only get 2 jumps if the ptrs are the same (which they never would be for us). After that, it seems to compare in 32 bit chunks (so 2 extra compares for the 40-bit word "fetch").
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.
@karlseguin The code doesn't call square
as it is generally better to compare the 2 versions in isolation. So to see the other results the function call in the code should be modified.
I agree, it is hard to follow, it is not obvious which implementation will be faster, also considering we don't know how well branch prediction works in either case or how the surrounding code changes things.
Hence why I would recommend avoiding code complexity/micro-optimizations unless we can measure a difference.
src/browser/polyfill/polyfill.zig
Outdated
pub fn missing(self: *Loader, name: []const u8, js_context: *Env.JsContext) bool { | ||
if (!self.done.fetch and std.mem.eql(u8, name, "fetch")) { | ||
// load the polyfill once. | ||
self.done.fetch = true; |
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.
These are going to be called a lot, there could be value in optimizing the string comparison to be a single int equality check. Something like this:
switch (name.len) {
5 => switch (@as(u40, @bitCast(domain[0..5].*))) {
asUint(u40, "fetch") => if (!self.done.fetch) {
self.done.fetch = true;
return load(fetch_src);
},
else => {},
},
else => {},
};
return false;
fn asUint(comptime T: type, comptime string: []const u8) T {
return @bitCast(string[0..string.len].*);
}
src/cdp/cdp.zig
Outdated
@@ -569,7 +569,7 @@ const IsolatedWorld = struct { | |||
// Currently we have only 1 page/frame and thus also only 1 state in the isolate world. | |||
pub fn createContext(self: *IsolatedWorld, page: *Page) !void { | |||
if (self.executor.js_context != null) return error.Only1IsolatedContextSupported; | |||
_ = try self.executor.createJsContext(&page.window, page, {}, false); | |||
_ = try self.executor.createJsContext(&page.window, page, {}, false, null); |
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.
I guess the BrowserContext will need its own loader to track the isolate world polyfills?
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.
Good point!
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.
done ✔️
About our discussion regarding returning "true" after having the polyfill loaded, it doesn't work, I get the following error:
|
Add a debug global unknown property
Because it will be displayed only if the property is non-native. So if your property is set in pureJS, you will still have the log...
f590982
to
2cdc9e9
Compare
No description provided.