-
Notifications
You must be signed in to change notification settings - Fork 288
Performance now #636
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
Merged
Merged
Performance now #636
Changes from all commits
Commits
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
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,87 @@ | ||
| // Copyright (C) 2023-2025 Lightpanda (Selecy SAS) | ||
| // | ||
| // Francis Bouvier <[email protected]> | ||
| // Pierre Tachoire <[email protected]> | ||
| // | ||
| // This program is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU Affero General Public License as | ||
| // published by the Free Software Foundation, either version 3 of the | ||
| // License, or (at your option) any later version. | ||
| // | ||
| // This program is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU Affero General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU Affero General Public License | ||
| // along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
|
||
| const std = @import("std"); | ||
|
|
||
| const parser = @import("../netsurf.zig"); | ||
| const EventTarget = @import("../dom/event_target.zig").EventTarget; | ||
|
|
||
| // https://developer.mozilla.org/en-US/docs/Web/API/Performance | ||
| pub const Performance = struct { | ||
| pub const prototype = *EventTarget; | ||
|
|
||
| // Extend libdom event target for pure zig struct. | ||
| base: parser.EventTargetTBase = parser.EventTargetTBase{}, | ||
|
|
||
| time_origin: std.time.Timer, | ||
| // if (Window.crossOriginIsolated) -> Resolution in isolated contexts: 5 microseconds | ||
| // else -> Resolution in non-isolated contexts: 100 microseconds | ||
| const ms_resolution = 100; | ||
|
|
||
| fn limitedResolutionMs(nanoseconds: u64) f64 { | ||
| const elapsed_at_resolution = ((nanoseconds / std.time.ns_per_us) + ms_resolution / 2) / ms_resolution * ms_resolution; | ||
| const elapsed = @as(f64, @floatFromInt(elapsed_at_resolution)); | ||
| return elapsed / @as(f64, std.time.us_per_ms); | ||
| } | ||
|
|
||
| pub fn get_timeOrigin(self: *const Performance) f64 { | ||
| const is_posix = switch (@import("builtin").os.tag) { // From std.time.zig L125 | ||
| .windows, .uefi, .wasi => false, | ||
| else => true, | ||
| }; | ||
| const zero = std.time.Instant{ .timestamp = if (!is_posix) 0 else .{ .sec = 0, .nsec = 0 } }; | ||
| const started = self.time_origin.started.since(zero); | ||
| return limitedResolutionMs(started); | ||
| } | ||
|
|
||
| pub fn _now(self: *Performance) f64 { | ||
| return limitedResolutionMs(self.time_origin.read()); | ||
| } | ||
| }; | ||
|
|
||
| const testing = @import("./../../testing.zig"); | ||
|
|
||
| test "Performance: get_timeOrigin" { | ||
| var perf = Performance{ .time_origin = try std.time.Timer.start() }; | ||
| const time_origin = perf.get_timeOrigin(); | ||
| try testing.expect(time_origin >= 0); | ||
|
|
||
| // Check resolution | ||
| try testing.expectDelta(@rem(time_origin * std.time.us_per_ms, 100.0), 0.0, 0.1); | ||
| } | ||
|
|
||
| test "Performance: now" { | ||
| var perf = Performance{ .time_origin = try std.time.Timer.start() }; | ||
|
|
||
| // Monotonically increasing | ||
| var now = perf._now(); | ||
| while (now <= 0) { // Loop for now to not be 0 | ||
| try testing.expectEqual(now, 0); | ||
|
Collaborator
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. Parameters are swapped, it's
Contributor
Author
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'll take this change into the next PR |
||
| now = perf._now(); | ||
| } | ||
| // Check resolution | ||
| try testing.expectDelta(@rem(now * std.time.us_per_ms, 100.0), 0.0, 0.1); | ||
|
|
||
| var after = perf._now(); | ||
| while (after <= now) { // Loop untill after > now | ||
| try testing.expectEqual(after, now); | ||
| after = perf._now(); | ||
| } | ||
| // Check resolution | ||
| try testing.expectDelta(@rem(after * std.time.us_per_ms, 100.0), 0.0, 0.1); | ||
| } | ||
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
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.
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.
It's fine, but just FYI, build.zig blocks any builds that aren't on linux or macos. There are a few places, like browser.zig
timestampfunction, which treat anything else as unreachable.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.
Yea, this is ugly. I wanted to just use the is_posix constant in time.zig, but that is not pub for some reason.
Then wanted to just check if
@TypeOf(std.time.Instant.timestamp) == i64but also:Does not seem to work ..