深入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就暂时不看了,等到时候用上了再做解释。
这一篇先简单介绍一下,后面再深入讲一讲在不同操作系统下的,两类时间戳的具体实现。
深入V8引擎-Time模块介绍的更多相关文章
- 瘸腿蛤蟆笔记29-cocos2d-x-3.2 Box2d物理引擎dynamics模块介绍
转载标明出处:http://blog.csdn.net/notbaron/article/details/38611335 上篇回想 本篇名言:奋斗.寻觅.发现,而不屈服.[诗人丁尼生] 上篇中,我们 ...
- (译)V8引擎介绍
V8是什么? V8是谷歌在德国研发中心开发的一个JavaScript引擎.开源并且用C++实现.可以用于运行于客户端和服务端的Javascript程序. V8设计的初衷是为了提高浏览器上JavaScr ...
- [翻译] V8引擎的解析
原文:Parsing in V8 explained 本文档介绍了 V8 引擎是如何解析 JavaScript 源代码的,以及我们将改进它的计划. 动机 我们有个解析器和一个更快的预解析器(~2x), ...
- webkit模块介绍
一.Webkit模块 用到的第三方库如下: cairo 一个2D绘图库 casqt Unicode处理用的库,从QT中抽取部分代码形成的 expat 一个XML SAX解析器的库 freety ...
- 精读《V8 引擎 Lazy Parsing》
1. 引言 本周精读的文章是 V8 引擎 Lazy Parsing,看看 V8 引擎为了优化性能,做了怎样的尝试吧! 这篇文章介绍的优化技术叫 preparser,是通过跳过不必要函数编译的方式优化性 ...
- V8引擎——详解
前言 JavaScript绝对是最火的编程语言之一,一直具有很大的用户群,随着在服务端的使用(NodeJs),更是爆发了极强的生命力.编程语言分为编译型语言和解释型语言两类,编译型语言在执行之前要先进 ...
- Chrome V8系列--浅析Chrome V8引擎中的垃圾回收机制和内存泄露优化策略
V8 实现了准确式 GC,GC 算法采用了分代式垃圾回收机制.因此,V8 将内存(堆)分为新生代和老生代两部分. 一.前言 V8的垃圾回收机制:JavaScript使用垃圾回收机制来自动管理内存.垃圾 ...
- v8引擎详解
引用网址: https://blog.csdn.net/swimming_in_it_/article/details/78869549 前言 JavaScript绝对是最火的编程语言之一,一直具有很 ...
- JavaScript深入浅出第4课:V8引擎是如何工作的?
摘要: 性能彪悍的V8引擎. <JavaScript深入浅出>系列: JavaScript深入浅出第1课:箭头函数中的this究竟是什么鬼? JavaScript深入浅出第2课:函数是一等 ...
随机推荐
- spring-boot2代码
App.java package com.kfit; import org.springframework.boot.SpringApplication; import org.springframe ...
- js作用域总结
一.在ES5中,js 的作用域 js作用域,只有全局作用域与函数作用域,没有块级作用域. 1.全局作用域 var a = 10; function aaa() {alert(a) } function ...
- 郝健: Linux内存管理学习笔记-第2节课【转】
本文转载自:https://blog.csdn.net/juS3Ve/article/details/80035753 摘要 slab./proc/slabinfo和slabtop 用户空间mallo ...
- BZOJ 1604 [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居:队列 + multiset + 并查集【曼哈顿距离变形】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1604 题意: 平面直角坐标系中,有n个点(n <= 100000,坐标范围10^9) ...
- SpringMVC拦截器的配置与使用详解
一.SpringMVC拦截器简介 Spring MVC的处理器拦截器类似于Servlet开发中的过滤器Filter,用于对处理器进行预处理和后处理.在springmvc中,定义拦截 ...
- perl 语言学习总结
.#!/usr/bin/perl -w 内建警告信息,Perl发出警告 .字符串 . 连接符 .重复次数 .字符串与数字之间的自动转换 .; + += *= .= not and or xor .pr ...
- 休假回来 更博-MySQL以月为单位的客户综合情况表_20161008
十一休假老家事比较多 未来得及更新 今起依旧更博- 生成一个以用户ID为单位,各月下单天次,各月买了几个产品,各月订单额 ,天次,,天次,,天次,NULL)) AS 9月天次 FROM ( SELEC ...
- 【LeetCode】042 Trapping Rain Water
题目: Given n non-negative integers representing an elevation map where the width of each bar is 1, co ...
- Dubbo配置设计
配置分类 配置格式 配置加载 可编程配置 配置缺省值 配置一致性 配置覆盖 配置继承 配置向后兼容 配置分类 首先,配置的用途是有多种的,大致可以分为: 环境配置,比如:连接数,超时等配置. 描述配置 ...
- RDA项目打包
注意APP的编译搭建: ./aps/Makefile.toolchain //ccoption path的设定 ./aps/rules.mak //统一的编译规则 MAKE -C 1.TOOLS的可执 ...