V8 is a high-performance JavaScript engine for Google Open source

Source: Internet
Author: User
Tags access properties class definition svm

V8 is a high-performance JavaScript engine for Google Open source, implemented in C + + and used in Google's open source browser, Chrome.
  
Why is V8 very fast, and what kind of solution makes V8 reach this speed? It is an interesting thing to find the secret.
  
Object-oriented, design patterns and performance
  
Some C + + developers have some strange ideas. They believe that using object-oriented and design patterns can degrade program performance. But V8 proved that the idea was wrong. The implementation of V8 uses many design patterns, but is still very efficient.
  
The following is a list of the two modes used in V8:
  
Factory mode
  
When the JavaScript engine executes a script, the engine creates an instance of each variable, function, or array encountered. Jsobject is the parent object of all these objects.
  
All classes that inherit from Jsobject are listed below:
  
V8 implements a factory class to create these objects, and the factory::newjsobject in that class are used to create these objects.
  
All methods that use the class/method are listed below.
  
The class in the V8 engine does not directly use this factory class, but instead adds another layer of encapsulation that calls the factory class through the heap class.
  
Visitor mode:
  
This explains the Observer pattern on Wikipedia:
  
The observer design pattern is a way of separating the algorithm from the object that the algorithm deals with. This separation allows new operations to be added to an existing object structure without modifying the structure itself. This is a way to follow the open/closed guidelines.
  
Similar to the factory model, the visitor pattern adds a wrapper layer to the implementation. This allows the code to be more readable and maintainable.
  
Many classes in the V8 source code implement the visitor pattern.
  
Even if V8 developers have to optimize execution efficiency, they don't care about the encapsulation layer that is added to the code. Using design patterns and adding some C + + mechanisms adds some encapsulation, so it does have an impact on efficiency. But this has a small impact on efficiency, and more on design decisions used by the application.
  
Design decisions for execution efficiency in V8
  
1. Hidden classes and quick property access.
  
JavaScript is a dynamic programming language: You can add or remove objects when the object is running. This means that it is easy to change the properties of an object.
  
The parent classes of jsfunction and Jsvalue are jsobject,jsfunction used to represent a JavaScript function, and jsvalue is used to represent a JavaScript value. However, there is no class that inherits from Jsobject and is used to represent a class such as function or value. Many JavaScript engines use dictionary-type data structures to store the familiarity of these objects, and accessing each property requires the dynamic lookup and resolution of the property's location in memory.
  
This approach causes JavaScript to be slower in accessing the properties of an object variable than in Java or Smalltalk. In these languages, the location of the real-column variable assignment is fixed, that is, the compiler, based on the layout in the object's class definition, adds a fixed offset position to the object's position in memory. So accessing these properties is only a read or store on memory, and this operation usually requires only one instruction.
  
V8 uses the hidden class concept to reduce the time spent accessing JavaScript properties. Instead of using dynamic queries to access properties, V8 creates hidden classes behind the scenes.
  
2. Dynamic Production machine code
  
During the first execution, V8 compiled the JavaScript source directly into machine code with no intermediate bytecode and no interpreter. Property access is handled by inline cache code, and V8 execution may have other machine directives that modify these cached code.
  
3. Efficient garbage collector
  
During execution, V8 will regain the memory of the discarded object, which is garbage collection. To ensure faster object classification, shorter garbage collection pauses, and no memory fragmentation. V8 uses a pause, generational, and precise garbage collector. This means that V8 has used:
#include <stdio.h>
#include <time.h>
#include <opencv2/opencv.hpp>
#include <opencv/cv.h>
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/ml/ml.hpp>
#include <io.h>

using namespace Std;
using namespace CV;

void GetFiles (string path, vector<string>& files);
void Getbubble (mat& trainingimages, vector<int>& traininglabels);
void Getnobubble (mat& trainingimages, vector<int>& traininglabels);

int main ()
{
Get Training data
Mat classes;
Mat Trainingdata;
Mat trainingimages;
Vector<int> Traininglabels;
Getbubble (Trainingimages, traininglabels);
Getnobubble (Trainingimages, traininglabels);
Mat (trainingimages). CopyTo (Trainingdata);
Trainingdata.convertto (Trainingdata, CV_32FC1);
Mat (traininglabels). CopyTo (classes);
Configuring the SVM Trainer parameters
Cvsvmparams Svm_params;
Svm_params.svm_type = cvsvm::c_svc;
Svm_params.kernel_type = Cvsvm::linear;
Svm_params.degree = 0;
Svm_params.gamma = 1;
SVM_PARAMS.COEF0 = 0;
Svm_params. C = 1;
svm_params.nu = 0;
SVM_PARAMS.P = 0;
Svm_params.term_crit = Cvtermcriteria (cv_termcrit_iter, 1000, 0.01);
Training
CVSVM SVM;
Svm.train (Trainingdata, classes, Mat (www.huachengjpt.com), Mat (), svm_params);
Save the Model
Svm.save ("Svm.xml");
cout<< "Training good!!! "<<endl;
GetChar ();
return 0;
}
void GetFiles (string path, vector<string>& files)
{
Long hfile = 0;
struct _finddata_t www.8555388.cn/fileinfo;
string P;
if (hfile = _findfirst (p.assign (Path). Append (www.yibaoyule1.com "\\*"). C_str (), &fileinfo))! =-1)
{
Do
{
if ((Fileinfo.attrib & _a_subdir))
Stops the execution of the program during the garbage collection cycle.
  
In most garbage loops, only part of the object heap is processed. This minimizes the impact of pauses on the application.
  
Records the location of all objects and pointers in memory, avoiding memory leaks caused by identifying objects as pointers.
  
Conclusion:
  
It is a false idea to use an object-oriented or design pattern for efficiency reasons. This only gets a few milliseconds of optimization, but loses the readability and maintainability of the code.

V8 is a high-performance JavaScript engine for Google Open source

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.