Skip to content

Commit b42b7fc

Browse files
time checkpoint
1 parent d269717 commit b42b7fc

File tree

1 file changed

+47
-0
lines changed
  • core/src/Streamly/Internal/Data/Time

1 file changed

+47
-0
lines changed

core/src/Streamly/Internal/Data/Time/Units.hs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,15 @@ newtype NanoSecond64 = NanoSecond64 Int64
173173
, Prim
174174
)
175175

176+
-- XXX timed
177+
178+
timed :: IO a -> IO (NanoSecond64, a)
179+
timed = undefined
180+
181+
-- ghcStats :: IO a -> IO (GHCStats, a)
182+
-- measuredBy :: Diff s => IO s -> IO a -> IO (s, a)
183+
-- timed = measuredBy (getTime Monotonic)
184+
176185
-- | An 'Int64' time representation with a microsecond resolution.
177186
-- It can represent time up to ~292,000 years.
178187
newtype MicroSecond64 = MicroSecond64 Int64
@@ -263,6 +272,8 @@ instance TimeUnit TimeSpec where
263272
toTimeSpec = id
264273
fromTimeSpec = id
265274

275+
-- XXX Remove 64 suffix, regular units should be considered 64 bit.
276+
266277
instance TimeUnit NanoSecond64 where
267278
{-# INLINE toTimeSpec #-}
268279
toTimeSpec (NanoSecond64 t) = TimeSpec s ns
@@ -355,6 +366,34 @@ fromAbsTime (AbsTime t) = fromTimeSpec t
355366
-- Relative time using NaonoSecond64 as the underlying representation
356367
-------------------------------------------------------------------------------
357368

369+
-- XXX Use NanoSecond etc. instead of RelTime. They already denote relative
370+
-- time. Maybe its a good idea to keep RelTime as a wrapper around time units
371+
-- so that we can switch the underlying representation any time. we can use
372+
-- Double or Int64 or Fixed or TimeSpec.
373+
--
374+
-- Can we design it such that we can switch to Double as the underlying
375+
-- representation any time if we want? We can just switch the module to switch
376+
-- the impl.
377+
--
378+
-- We can use AbsTime and RelTime as generic types so that we have the ability
379+
-- to switch the underlying repr.
380+
--
381+
-- Use "Time" for AbsTime relative to Posix epoch, basically the system
382+
-- time. For Time, use a 64-bit value or 64+64? A fixed epoch + relative time.
383+
-- For relative times in a stream we can use rollingMap (-). As long as the
384+
-- epoch is fixed we only need to diff the reltime which should be efficient.
385+
--
386+
-- We can do the same to paths as well. As long as the root is fixed we can
387+
-- diff only the relative components.
388+
--
389+
-- Also type Time = PosixTime
390+
-- newtype PosixTime = AbsTime Posix days ns
391+
-- newtype UTCTime = AbsTime UTC days ns
392+
-- newtype RelTime = AbsTime Rel days ns
393+
--
394+
-- The max value of ns won't be limited to 10^9 so we can keep the epoch fixed
395+
-- and only manipulate ns.
396+
--
358397
-- We use a separate type to represent relative time for safety and speed.
359398
-- RelTime has a Num instance, absolute time doesn't. Relative times are
360399
-- usually shorter and for our purposes an Int64 nanoseconds can hold close to
@@ -434,10 +473,12 @@ fromRelTime (RelTime t) = fromTimeSpec t
434473
{-# RULES "toRelTime/fromRelTime" forall a. fromRelTime (toRelTime a) = a #-}
435474

436475
-- XXX rename to diffAbsTimes?
476+
-- SemigroupR?
437477
{-# INLINE diffAbsTime #-}
438478
diffAbsTime :: AbsTime -> AbsTime -> RelTime
439479
diffAbsTime (AbsTime t1) (AbsTime t2) = RelTime (t1 - t2)
440480

481+
-- SemigroupR?
441482
{-# INLINE addToAbsTime #-}
442483
addToAbsTime :: AbsTime -> RelTime -> AbsTime
443484
addToAbsTime (AbsTime t1) (RelTime t2) = AbsTime $ t1 + t2
@@ -473,6 +514,12 @@ showNanoSecond64 time@(NanoSecond64 ns)
473514
| t >= 1e1 = printf "%.2f %s" t u
474515
| otherwise = printf "%.3f %s" t u
475516

517+
-- The unit Second may be implicit. We can then use modifiers to convert it
518+
-- e.g. Nano 1 for 1 nanosec, Micro 1 for 1 microsec. These can work in general
519+
-- for any unit.
520+
--
521+
-- We can also use Minute x for 60x, and Hour x for 3600x etc.
522+
--
476523
-- In general we should be able to show the time in a specified unit, if we
477524
-- omit the unit we can show it in an automatically chosen one.
478525
{-

0 commit comments

Comments
 (0)