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

Implement system and monotonic clocks in Inko, and pass separately as seconds and nanoseconds #749

Open
yorickpeterse opened this issue Aug 29, 2024 · 0 comments
Labels
accepting contributions Issues that are suitable to be worked on by anybody, not just maintainers feature New things to add to Inko, such as a new standard library module runtime Changes related to the Rust-based runtime library std Changes related to the standard library

Comments

@yorickpeterse
Copy link
Collaborator

DateTime and Instant use some Rust functions to get the system time and a monotonic clock time respectively. We should instead use Inko's C FFI and use the underlying C functions directly, allowing us to remove this logic from the Rust runtime library.

In addition, DateTime.from_timestamp expects the timestamp as a single value: either an Int without nano/sub seconds, or a Float that includes them. We should adjust this such that the seconds and nanoseconds are passed as two separate Int values, ensuring there's no loss of precision.

As part of this we might also have to change the implementation of Instant: right now it expresses the monotonic time as a single Int in nanoseconds, giving us a range of 292 years. Since this value is relative to when the program started, rather than some arbitrary point in time (e.g. the time at which the OS started), this isn't likely to overflow. However, if we use C directly we may not have a guarantee as to what the starting point is, and I don't want to implicitly depend on that being "recent enough". This means we might also have to change Instant to store both seconds and nanoseconds separately.

Steps to take

First we'll port over the existing Rust code to the equivalent Inko code:

  1. Call tzset() when the runtime library initializes itself in inko_runtime_new, so we don't need to call this every time
  2. Replace use of inko_time_system with Inko/FFI code, using clock_gettime and initially returning the value as a Float (so basically what we do now)
  3. Replace use of inko_time_system_offset with a similar approach
  4. Replace use of inko_time_monotonic with a similar setup, returning a single Int (as we do now)
  5. Remove these functions and their related logic from the Rust codebase

Then we can refactor DateTime:

  1. Change fn pub static from_timestamp[T: ToFloat](time: ref T, utc_offset: Int) -> DateTime to fn pub static from_timestamp(secs: Int, nanos: Int, utc_offset: Int) -> DateTime and change the implementation accordingly
  2. Change DateTime.sub_second to store its value as an Int (in nanoseconds) instead of a Float
  3. In DateTime.to_float, convert sub_second to the number of seconds as a Float, then add that to the timestamp as a float
  4. Change the functions that get the system time and offset such that they return an Int instead of a Float

Finally, we can do essentially the same for Instant.

Note to potential contributors

While this change is open to contributions, it might be a bit of work especially for first-time contributors. I highly recommend submitting the changes in smaller chunks (e.g. only 1-2 steps at a time) instead of all at once. This makes it easier to review the code and give proper feedback.

@yorickpeterse yorickpeterse added accepting contributions Issues that are suitable to be worked on by anybody, not just maintainers feature New things to add to Inko, such as a new standard library module std Changes related to the standard library runtime Changes related to the Rust-based runtime library labels Aug 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepting contributions Issues that are suitable to be worked on by anybody, not just maintainers feature New things to add to Inko, such as a new standard library module runtime Changes related to the Rust-based runtime library std Changes related to the standard library
Projects
None yet
Development

No branches or pull requests

1 participant