You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We currently generate a big lump of array data from the AMP specs that we then load into PHP and process.
This is less than ideal for several reasons.
Arrays only know about strings
We have to manage an entire set of constants and always refer to these constants to access a specific index, so as to avoid simple typos introducing bugs.
With classes, this changes so that we can directly refer to properties, without the need for constants. Arrays:
The IDE will help with autocompletion and show the available values, and typos are immediately highlighted.
Arrays cannot have logic
We have a lot of code that tests the existence of an element within the spec array first before reading and using it. All of this logic is tedious and bug-prone, and obfuscates the actual logic.
With classes, we can encapsulate a lot of this boilerplate logic into the classes, freeing the consuming code up to concentrate on its core logic instead.
if ( $attr_spec_rule->is_mandatory ) {
$this->remove_node( $node );
returnfalse;
}
Arrays use a multiple of the memory of classes
For a structure like we have with these specs, arrays will waste a surprising lot of memory.
This is due to the fact that the indexes cannot be optimized for arrays, they are strings, and every single index for every single element will need to be stored separately as a string.
For classes, these are turned into properties, and the name of a property is only stored a single time in the declaration of the class. After that, the usage is highly optimized by PHP.
A quick and simple test shows that for a relatively simple structure, a change from arrays to classes yields memory savings of 62%: https://3v4l.org/rOJr5
This number will of course vary based on what the structure actually is, but it will be significant either way.
For turning the source of truth into PHP, we could stick with the Python parser and add a second PHP step, or we could omit the entire Python dependency and rebuild it in PHP from scratch.
The generated objects should contain the actual data in their declarations, so that we can offload everything into the opcache and autoloader.
This would mean that the parsing from PHP to bytecode would be skip at runtime because of the opcache. Additionally, starting from PHP 7.4, we could set this up so that the opcache portion can be persisted across requests, making the entire spec loading an instant operation without any performance impact.
Feature description
We currently generate a big lump of array data from the AMP specs that we then load into PHP and process.
This is less than ideal for several reasons.
Arrays only know about strings
We have to manage an entire set of constants and always refer to these constants to access a specific index, so as to avoid simple typos introducing bugs.
With classes, this changes so that we can directly refer to properties, without the need for constants.
Arrays:
Classes:
The IDE will help with autocompletion and show the available values, and typos are immediately highlighted.
Arrays cannot have logic
We have a lot of code that tests the existence of an element within the spec array first before reading and using it. All of this logic is tedious and bug-prone, and obfuscates the actual logic.
With classes, we can encapsulate a lot of this boilerplate logic into the classes, freeing the consuming code up to concentrate on its core logic instead.
Arrays:
Classes:
Arrays use a multiple of the memory of classes
For a structure like we have with these specs, arrays will waste a surprising lot of memory.
This is due to the fact that the indexes cannot be optimized for arrays, they are strings, and every single index for every single element will need to be stored separately as a string.
For classes, these are turned into properties, and the name of a property is only stored a single time in the declaration of the class. After that, the usage is highly optimized by PHP.
A quick and simple test shows that for a relatively simple structure, a change from arrays to classes yields memory savings of 62%: https://3v4l.org/rOJr5
This number will of course vary based on what the structure actually is, but it will be significant either way.
This directly relates to #2769
Acceptance criteria
Implementation brief
QA testing instructions
Demo
Changelog entry
The text was updated successfully, but these errors were encountered: