Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

Cleanup std::chrono usage - #3576

Merged
brunoabinader merged 1 commit into
masterfrom
cleanup-std-chrono
Jan 25, 2016
Merged

brunoabinader merged 1 commit into
masterfrom
cleanup-std-chrono

Conversation

@brunoabinader

Copy link
Copy Markdown
Contributor

We have abstractions for std::chrono in mbgl/util/chrono.hpp for most common usages. This patch series reduces std::chrono cast complexity and usage to include/mbgl/util/chrono.hpp.

👀 @kkaefer @jfirebaugh @tmpsantos

@jfirebaugh

Copy link
Copy Markdown
Contributor

We're using std::chrono::seconds, std::chrono::milliseconds, etc. wrong. The intent of introducing mbgl::Duration was to have one duration type which we use everywhere a duration is required. Adding mbgl::Seconds, mbgl::Milliseconds, etc., and converters such as asSeconds, toSeconds is a mistake.

The only times std::chrono::seconds and so on should be used are:

  • When constructing a constant, e.g. std::chrono::seconds(10). (Or, if you prefer, using std::literals::chrono_literals and 10s instead.)
  • When converting an integer type with implied units from an external data or an external API.

In both of those cases the expression should be immediately assigned to a mbgl::Duration instance, letting the constructor handle conversion.

@brunoabinader

Copy link
Copy Markdown
Contributor Author

The main goal for establishing aliases and helper functions is consistency. We either follow mbgl or std::chrono directly, but having a mix is bad.

The intent of introducing mbgl::Duration was to have one duration type which we use everywhere a duration is required.

Yes, however, as you mentioned, using helpers like std::chrono::{,micro,milli}seconds or std::literals::chrono_literals are still required for data input.

Adding mbgl::Seconds, mbgl::Milliseconds, etc., and converters such as asSeconds, toSeconds is a mistake.

Are you suggesting we should get rid of our helpers and use e.g. std::chrono::seconds, std::chrono::duration_cast directly?

We're using std::chrono::seconds, std::chrono::milliseconds, etc. wrong.

What do you have in mind then for cases where output is required to be in a time unit that is not seconds - e.g. https://github.com/mapbox/mapbox-gl-native/blob/master/platform/default/timer.cpp#L69 ?

@kkaefer

kkaefer commented Jan 18, 2016

Copy link
Copy Markdown
Member

The main goal for establishing aliases and helper functions is consistency. We either follow mbgl or std::chrono directly, but having a mix is bad.

It's a little inconsistent, but given that we just have type aliases, it's not a problem per se.

I think @jfirebaugh wants to say that we should stay with or move back to std::chrono::duration (or a mbgl::Duration alias of it), instead of using std::chrono::milliseconds (or any aliased types) for storing time duration. When we need to output the actual number, we should cast directly at the place where need it. We could helper functions or aliases for this as well.

@jfirebaugh

Copy link
Copy Markdown
Contributor

Right @kkaefer.

  • For points in time, use mbgl::TimePoint.
  • For spans of time, use mbgl::Duration.
  • If you need to convert to and from these types, do it as far towards the outer edge as possible -- immediately after input, immediately before output.

It looks like the main issue here is Response::modified and Response::expires. Modification time and expiration time are both points in time, not durations: they should be mbgl::TimePoints.

Use mbgl::Duration and mbgl::{,Milli}Seconds whenever possible.
@kkaefer

kkaefer commented Jan 25, 2016

Copy link
Copy Markdown
Member

Looks good!

@brunoabinader
brunoabinader merged commit 2f86467 into master Jan 25, 2016
@brunoabinader
brunoabinader deleted the cleanup-std-chrono branch January 25, 2016 15:12
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants