PHP7 innovation and performance optimization (1)

Source: Internet
Author: User
Tags scalar

PHP7 innovation and performance optimization (1)

I was lucky enough to participate in the PHP Technology Summit PHPCON in 2015. I heard about the new features and Performance Optimization of PHP 7 from laruence goy. Laruence is the most authoritative PHP expert in China. He shared many valuable things. I organized the PPT and collected relevant information, this article explains the nature of the technology, hoping to help students who are doing PHP development.

PHP has gone through 20 years of history. Until today, PHP 7 has released the RC version. It is said that the official PHP 7 version should be released around November 2015. PHP7 is a large-scale innovation for PHP5. * in the previous series, especially in terms of performance.

PHP is a Web development language widely used around the world. The PHP7 revolution will certainly bring more profound changes to these Web Services. Here we reference a chart in the bird brother PPT. 82% Web site uses PHP as the development language ):

Note: A web site can use multiple languages as its development language)

Note: This article contains many slides from laruence. The pictures are copyrighted by laruence)

Let's take a look at two exciting performance test results:

Benchmark comparison image from PPT ):

PHP7 performance test results, performance pressure test results, time consumption decreased from 2.991 to 1.186, a significant decrease of 60%.

WordPress's QPS pressure test image comes from PPT ):

In the WordPress project, compared with PHP5.6, PHP7 improves the QPS by 2.77 times.

After reading the exciting performance test results, let's get started. PHP 7 has many new features. However, we will focus more on those major changes.

1. New Features and changes

1. Scalar Type and return Type declaration Scalar Type Declarations & Scalar Type Declarations)

A very important feature of the PHP language is the "weak type", which makes PHP programs very easy to write. New users can quickly get started with PHP, but it is also accompanied by some controversy. Supports the definition of variable types, which can be said to be a change in the nature of innovation, PHP began to support the type definition in an optional way. In addition, a switch command declare (strict_type = 1) is introduced. Once this command is enabled, the program in the current file will be forced to follow the strict function parameter passing type and return type.

For example, to add a type definition to an add function, you can write it as follows:

If you use the force type switch command, it can be changed to the following:

If strict_type is not enabled, PHP will try to convert it to the required type. After the strict_type is enabled, PHP will no longer convert the type. If the type does not match, an error will be thrown. This is a good news for students who like "strong" languages.

For more details: PHP7 scalar type declaration RFC [translation]

2. More errors become caught exceptions.

PHP7 implements a global throwable interface. Both the original Exception and some errors implement this interface), and defines the inheritance structure of exceptions in the form of interfaces. As a result, more errors in PHP 7 become caught exceptions and are returned to the developer. If no capture is performed, the Error is returned. If the capture is performed, the Exception can be processed in the program. These captured errors are usually errors that do not cause fatal damage to the program. For example, functions do not exist. PHP7 further facilitates the developer to handle the issue and gives developers more control over the program. By default, errors directly interrupt the program, while PhP 7 provides the ability to capture and process the program, so that the program can continue to run and provide programmers with more flexible options.

For example, to execute a function that we are not sure whether the function exists, PHP5 is compatible with the method of determining function_exist before the function is called, while PHP7 supports Exception capture.

The example in the preceding figure is from the PPT ):

3. ASTAbstract Syntax Tree, abstract Syntax Tree)

AST acts as a middleware role in the PHP compilation process. Instead, it directly spams opcode from the interpreter to make the interpreter parser) and the compiler compliler) decouple to reduce some Hack code, at the same time, it makes the implementation easier to understand and maintain.

PHP5:

PHP7:

More AST information: https://wiki.php.net/rfc/abstract_syntax_tree

4. Native TLSNative Thread local storage, Native Thread local storage)

In the multi-Thread mode, for example, the woker and event modes of Apache on the Web server are multithreading.) The "Thread security" TS and Thread Safe issues must be solved, because threads share the memory space of processes, each thread itself needs to build a private space to save its own private data in a certain way to avoid mutual contamination with other threads. PHP5 maintains a large global array, allocates an independent storage space for each thread, and the thread accesses the Global Data Group through its own key value.

The unique key value in PHP5 needs to be passed to every function that needs to use global variables. PHP7 considers this transfer method unfriendly and has some problems. Therefore, we try to use a global thread-specific variable to save this key value.

Native TLS problems related: https://wiki.php.net/rfc/native-tls

5. other new features

PHP 7 has many new features and changes. We will not discuss them all here.

... ...


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.