当前位置: 移动技术网 > IT编程>开发语言>C/C++ > 深入V8引擎-Time模块介绍

深入V8引擎-Time模块介绍

2019年05月24日  | 移动技术网IT编程  | 我要评论

吴白鸽,海王祭下载,张小雨大胆人体艺术

  积跬步,行千里,先从最简单的开始写。

  这一篇介绍v8中的时间模块,与libuv粗糙的update_loop_time方法不同,v8有一套独立完整的类负责管理时间。

  该类位于src/base/platform/time.h,是一个辅助模块,首先来看一下继承树。

  整个模块的继承关系比较简单,一般常用的就是time、timeticks类,这里挨个进行介绍吧。

 

timeconstants

  这个类很直接,只是定义一些常量,比如一星期有7天,一天有24小时,一小时有60分钟等等……

class timeconstants {
 public:
  static constexpr int64_t khoursperday = 24;
  static constexpr int64_t kmillisecondspersecond = 1000;
  static constexpr int64_t kmillisecondsperday =
      kmillisecondspersecond * 60 * 60 * khoursperday;
  // ...
};

 

timedelta

  这个类提供把各种单位的时间转换为microseconds的方法。

class v8_base_export timedelta final {
 public:
  constexpr timedelta() : delta_(0) {}

  // converts units of time to timedeltas.
  static constexpr timedelta fromdays(int days) {
    return timedelta(days * timeconstants::kmicrosecondsperday);
  }
  // ...
}

  这里的常数定义来源于上面的timeconstants类。

 

timebase

  这个类没啥好说的,比较特殊的地方就是这是个模版类,提供对给定类型的时间序列化功能。

template <class timeclass>
class timebase : public timeconstants {
 public:
  // ...

  int64_t tointernalvalue() const { return us_; }

  // ...

  static timeclass frominternalvalue(int64_t us) { return timeclass(us); }
 protected:
  explicit constexpr timebase(int64_t us) : us_(us) {}

  // time value in a microsecond timebase.
  int64_t us_;
};

 

time

  time类负责管理javascript中date.now生成的时间戳,用的比较多所以这里就不解释了。

// -----------------------------------------------------------------------------
// time
//
// this class represents an absolute point in time, internally represented as
// microseconds (s/1,000,000) since 00:00:00 utc, january 1, 1970.

class v8_base_export time final : public time_internal::timebase<time> {
  // ...
};

  关于类的介绍,在注释里面都写的很明白了,需要注意的是在不同的操作系统,这些方法的表现天差地别,甚至有些方法仅在指定的操作系统才能生效。

 

timeticks

// -----------------------------------------------------------------------------
// timeticks
//
// this class represents an abstract time that is most of the time incrementing
// for use in measuring time durations. it is internally represented in
// microseconds.  it can not be converted to a human-readable time, but is
// guaranteed not to decrease (if the user changes the computer clock,
// time::now() may actually decrease or jump).  but note that timeticks may
// "stand still", for example if the computer suspended.

class v8_base_export timeticks final : public time_internal::timebase<timeticks> {
  // ...
};

  注释相当的精细。

  timeticks这个类则负责另外一种时间戳,在浅析libuv源码-获取精确时间中有进行过介绍。比如在windows中,有两种计数api,分别返回系统"心跳"一次所花时间与"心跳"次数,由于频繁总是固定不变,所以可以根据每次返回的次数来进行计时。

  这类事件戳比起上的time优势在于可以保证数值一直在增加,并且不会受外界因素影响(机器挂了另算)。所以无论是libuv设置轮询开始时间或处理定时器任务,还是v8在对js代码进行编译计时,都是用的这个。

  最后的threadticks就暂时不看了,等到时候用上了再做解释。

  这一篇先简单介绍一下,后面再深入讲一讲在不同操作系统下的,两类时间戳的具体实现。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网