深入V8引擎-Time模块介绍
积跬步,行千里,先从最简单的开始写。
这一篇介绍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 = ;
static constexpr int64_t kMillisecondsPerSecond = ;
static constexpr int64_t kMillisecondsPerDay =
kMillisecondsPerSecond * * * kHoursPerDay;
// ...
};
TimeDelta
这个类提供把各种单位的时间转换为microseconds的方法。
class V8_BASE_EXPORT TimeDelta final {
public:
constexpr TimeDelta() : delta_() {} // 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就暂时不看了,等到时候用上了再做解释。
这一篇先简单介绍一下,后面再深入讲一讲在不同操作系统下的,两类时间戳的具体实现。
最新文章
- 刷题ING...
- IoC实践--用Autofac实现MVC5.0的IoC控制反转方法
- Unity不同平台生成中预处理的注意点
- compiler
- android开发实现静默安装(fota升级)
- JavaScript 学习笔记-- ES6学习(一)介绍以及Babel的使用
- AvalonDock结合MVVM模式的应用
- Hacker(五)----黑客专用通道--->;端口
- android操作ini工具类
- curl调用openstack API总结
- Scala学习笔记:重要语法特性
- 基本排序算法(Java)
- Linux下的快速配置虚拟环境virtualenvwrapper
- 通过lua栈了解lua与c的交互
- HDU 2602 Bone Collectors(背包问题,模版)
- Hanlp在ubuntu中的使用方法介绍
- mysql8.0无法给用户授权或提示You are not allowed to create a user with GRANT的问题
- Python学习笔记之Python的多重继承和MixIn
- 2019年UX设计新趋势
- osg::Vec2 Vec3 Vec4
热门文章
- [UVA 12633] Super Rooks on Chessboard FFT+计数
- qemu仿真执行uboot和barebox
- hiho一下 第四十七周 拓扑排序一 【静态数组链式前向星存储结构实现 + 拓扑跳出 】
- ZOJ3228 Searching the String —— AC自动机 + 可重叠/不可重叠
- SQL Server 2008R2 代理服务-开启
- 右上角鼠标滑过展开收缩动画效果js代码的演示页面
- JS判断数字、中文、小数位数
- Zabbix数据库清理历史数据
- linux命令学习笔记(29):chgrp命令
- 远程调用appium server