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

Use std::time::Duration for time intervals #7616

Open
wants to merge 15 commits into
base: master
Choose a base branch
from

Conversation

higumachan
Copy link

#6075

I replaced the interval declared in f64 that I could find with ruffle_core::RuffleDuration.

Please let me know if you find any omissions in the f64-declared intervals, as they are difficult to find.

Copy link
Contributor

@relrelb relrelb left a comment

Choose a reason for hiding this comment

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

Welome! I think instant::Instant can be used in place of the proposed RuffleDuration. instant is already a dependency of ruffle-core, and currently being used in various places across the codebase. So I'll be glad if you please proceed with instant::Instant. Also, looks like the PR has conflicts against current master branch. Can you please resolve them while at it?

@n0samu
Copy link
Member

n0samu commented Aug 10, 2022

@relrelb Doesn't that conflict with what Mike was suggesting here? #6075 (comment)
Or am I misunderstanding something?

@relrelb
Copy link
Contributor

relrelb commented Aug 12, 2022

Welome! I think instant::Instant can be used in place of the proposed RuffleDuration. instant is already a dependency of ruffle-core, and currently being used in various places across the codebase. So I'll be glad if you please proceed with instant::Instant. Also, looks like the PR has conflicts against current master branch. Can you please resolve them while at it?

Right, I take my words back. I was confusing between instant and duration, which are not the same thing.

@higumachan
Copy link
Author

@relrelb I fixed conflict.

Welome! I think instant::Instant can be used in place of the proposed RuffleDuration. instant is already a dependency of ruffle-core, and currently being used in various places across the codebase. So I'll be glad if you please proceed with instant::Instant. Also, looks like the PR has conflicts against current master branch. Can you please resolve them while at it?

Right, I take my words back. I was confusing between instant and duration, which are not the same thing.

As for this one, the regression test failed due to lack of precision in std::Duration (same definition in instant::Duration), as I wrote in my comment here(#6075 (comment)).

Copy link
Member

@Herschel Herschel left a comment

Choose a reason for hiding this comment

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

Thank you! This looks exactly what I had in mind. There are only a few small naming issues.

sha2 = "0.10.2"
sha2 = "0.10.2"
Copy link
Member

Choose a reason for hiding this comment

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

Unrelated formatting change in the PR, could you remove this change?

Comment on lines 44 to 49
pub fn zero() -> Self {
Self(OrderedFloat::zero())
}
Copy link
Member

Choose a reason for hiding this comment

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

Let's make this pub const ZERO: Self. This matches what we do for Twips.

Comment on lines 52 to 57
pub const fn one_sec() -> Self {
Self(OrderedFloat(1.0))
}
Copy link
Member

Choose a reason for hiding this comment

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

Let's make this pub const ONE_SECOND: Self

Comment on lines 8 to 9
#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Default, Serialize, Deserialize)]
pub struct RuffleDuration(OrderedFloat<f64>);
Copy link
Member

Choose a reason for hiding this comment

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

For ease of typing and to follow Rust naming conventions, let's name this Duration instead of RuffleDuration. Rust prefers to avoid stuttering in names (ruffle_core::RuffleDuration vs. ruffle_core::Duration). We can explicitly specify for methods that want the std version (std::instant::Duration), or use a renaming import (use std::instant::Duration as StdDuration).

Instead of adding ordered_float dependency so that we can derive Ord and Eq, let's instead implement any necessary methods ourselves. For example, Ord isn't necessary if we add a direct min/max method:

    pub fn min(self, other: RuffleDuration) -> RuffleDuration {
        if self.0 < other.0 {
            self
        } else {
            other
        }
    }

then the duration can become:
pub struct Duration(f64);
without the need for ordered_float.

I know we already have lots of dependencies, but I like to avoid adding new ones when possible.

@higumachan
Copy link
Author

@Herschel Thanks for the review.
I fixed the your review points.

@@ -1372,7 +1372,7 @@ fn run_swf(
// with timer execution (timers will see an elapsed time of *at least*
// the requested timer interval).
if frame_time_sleep {
std::thread::sleep(frame_time_duration);
std::thread::sleep(StdDuration::from_nanos(frame_time.as_nanos() as u64));
Copy link
Member

Choose a reason for hiding this comment

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

Could this be frame_time.into()?

@@ -1310,8 +1311,7 @@ fn run_swf(
let base_path = Path::new(swf_path).parent().unwrap();
let mut executor = NullExecutor::new();
let movie = SwfMovie::from_path(swf_path, None)?;
let frame_time = 1000.0 / movie.frame_rate().to_f64();
let frame_time_duration = Duration::from_millis(frame_time as u64);
let frame_time = Duration::from_millis(1000.0 / movie.frame_rate().to_f64());
Copy link
Member

Choose a reason for hiding this comment

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

This can be Duration::from_secs(1.0 / ...); now

@higumachan
Copy link
Author

@Herschel

Sorry, this is late, but I have fixed the review points.

@kmeisthax
Copy link
Member

Hmm... looks like this PR needs some TLC. I'll come back to it later.

@kmeisthax kmeisthax force-pushed the refactor/f64_to_duration branch from 2673859 to 042ee49 Compare November 30, 2022 03:45
@kmeisthax
Copy link
Member

I've pushed an update to this PR that rebases it on a relatively-current master and attempts to fix a few incompatibilities that cropped up from new types adopting Instant and Duration.

I still need to come back to this. There's some stuff in web and desktop that relies on Instant arithmetic that isn't supported with the alternative Duration type.

@iwannabethedev
Copy link
Contributor

Is this PR difficult to update and review due to its size? If it is, I wonder whether it could be split into multiple smaller PRs - like, the first PR would be core/src/duration.rs . And after that is merged, a PR would be opened that takes a part of the changes in this PR, gets it merged, and then rinse and repeat with new, small PRs until all changes in this PR has been merged.

The advantage of having a large PR is, I would guess, that you can then try out the new type in all the places where it might be relevant, and thus uncover problems early on, which is very useful and help and can avoid backtracking late in the process. But, now that there is a large PR, I imagine that you already have that potential advantage of a large PR, and can then proceed with small PRs taken from this large PR.

@iwannabethedev
Copy link
Contributor

@higumachan In case that you do not currently seek to work on this further, would it be alright with you for others to take a stab at getting these changes merged, for instance through multiple smaller PRs that reference this PR?

@danielhjacobs danielhjacobs added the T-refactor Type: Refactor / Cleanup label Sep 17, 2024
@danielhjacobs danielhjacobs added the A-other Area: Not covered by other area labels label Oct 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-other Area: Not covered by other area labels T-refactor Type: Refactor / Cleanup
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants