SlideShare a Scribd company logo
1 of 38
Download to read offline
MAGENTO 2
LAYOUT AND CODE COMPILATION
FOR PERFORMANCE
 
by Ivan Chepurnyi
WHAT? COMPILATION?
COMPLEX ALGORITHMSSIMPLE
WHAT MAKES THEM COMPLEX?
REPEATED DATA PROCESSING
// ... some xml/json/yaml file initialization
foreach ($loadedData as $item) {
$this->process($item);
}
NESTED LOOPS
foreach ($data as $item) {
$row = [];
foreach ($columns as $column) {
$row[] = $column->export($item);
}
$csv->write($row);
}
COMPLEX DEPENDENCY TREE
class ClassOne
{
public function __construct(ClassTwo $dependency) {}
}
class ClassTwo
{
public function __construct(ClassThree $dependency) {}
}
class ClassThree
{
public function __construct(ClassFour $dependencyOne, ClassFive $dependen
}
// ..
HOW CAN WE SOLVE IT?
REPEATED DATA PROCESSING
Translate your XML/JSON/YAML file into executable PHP
code and include it when you need processed structure
NESTED LOOPS
Pre-compile second loop and execute it within the main one
COMPLEX DEPENDENCY TREE
Resolve dependencies and compile resolution into
executable code
BUT COMPILATION LOOKS UGLY...
You need to create PHP code within PHP code
You need to write it to external file
You need to include that file inside of your code
I WAS LOOKING FOR A LIBRARY
Didn't find one... So I wrote it myself.
ECOMDEVCOMPILER
Created to wrap writing PHP code within PHP
Automatically stores compiled code
Automatically validates source and re-compiles code
when needed
Provides easy to use API to create parsers, builders and
executors
INSTALLATION
1. Available as a composer dependency
2. Instantiate it directly or via DI container.
composer require "ecomdev/compiler"
SOME EXAMPLES
COMPILE XML INTO PHP
XML FILE
<objects>
<item id="object_one" type="object" />
<item id="object_two" type="object" />
<item id="object_three" type="object" />
<type id="object" class="SomeClassName"/>
</objects>
PARSER
use EcomDevCompilerStatementBuilder;
class Parser implements EcomDevCompilerParserInterface
{
// .. constructor with builder as dependency
public function parse($value)
{
$xml = simplexml_load_string($value);
$info = $this->readXml($xml);
return $this->getPhpCode($info, $this->builder);
}
// .. other methods
}
PARSE XML DATA
private function readXml($xml)
{
$info = [];
foreach ($xml->children() as $node) {
if ($node->getName() === 'type') {
$info['types'][(string)$node->id] = (string)$node->class;
} elseif ($node->getName() === 'object') {
$info['objects'][(string)$node->id] = (string)$node->type;
}
}
return $info;
}
CREATE PHP CODE
private function getPhpCode($info, $builder) {
$compiledArray = [];
foreach ($info['objects'] as $objectId => $type) {
$compiledArray[$objectId] = $builder->instance($info['types'][
}
return $builder->container(
$builder->returnValue($compiledArray)
);
}
COMPILED PHP FILE
return [
'object_one' => new SomeClassName(),
'object_two' => new SomeClassName(),
'object_three' => new SomeClassName()
];
NESTED LOOP SIMPLIFYING
YOUR CONSTRUCTOR
public function __construct(
EcomDevCompilerBuilder $builder,
EcomDevCompilerCompiler $compiler)
{
$this->builder = $builder;
$this->compiler = $compiler;
}
EXPORT METHOD
public function export($data, $columns)
{
$statements = $this->compileColumns($columns, $this->builder);
$source = new EcomDevCompilerSourceStaticData(
'your_id', 'your_checksum', $statements
);
$reference = $this->compiler->compile($source);
$closure = $this->compiler->interpret($reference);
foreach ($data as $item) {
$row = $closure($item, $columns);
}
}
COMPILATION METHOD
public function compileColumns($columns, $builder)
{
$item = $builder->variable('item');
$compiledArray = [];
foreach ($columns as $id => $column) {
$compiledArray[] = $builder->chainVariable('columns')[$id]
->export($item);
}
$closure = $builder->closure(
[$item, $builder->variable('columns')],
$builder->container([$builder->returnValue($compiledArray)])
);
return $builder->container([$builder->returnValue($closure)]);
}
RESULT
return function ($item, $columns) {
return [
$columns['id1']->export($item),
$columns['id2']->export($item),
$columns['id3']->export($item),
// ...
];
};
MAIN COMPONENTS
CompilerInterface - Compiler instance
StorageInterface - Stores compiled files
SourceInterface - Provider of data (File, String,
StaticData)
ParserInterface - Parser of data
ObjectBuilderInterface - Bound builder for included files
AND SOME SWEET STUFF...
EXPORTABLE OBJECTS
class SomeClass implements EcomDevCompilerExportableInterface
{
public function __construct($foo, $bar) { /* */ }
public function export() {
return [
'foo' => $this->foo,
'bar' => $this->bar
];
}
}
Will be automatically compiled into:
new SomeClass('fooValue', 'barValue');
MAGENTO 2.0
LAYOUT COMPILATION IS A MUST
WHY? BECAUSE OF ITS ALGORITHM
LAYOUT CACHING
Every handle that is added to the
MagentoFrameworkViewResult changes the cache key
for the whole generated structure.
LAYOUT GENERATION
Scheduled structure is generated from XML object at all
times
SOLUTION
1. Make every handle a compiled php code
2. Include compiled handles at loading phase
GOOD NEWS
I am already working on it
Will be release in April 2016
GITHUB
COMPILER LIBRARY FOR M2
https://github.com/EcomDev/compiler
LAYOUT COMPILER FOR M1
https://github.com/EcomDev/EcomDev_LayoutCompiler
LAYOUT COMPILER FOR M2
Coming soon
Q&A
@IvanChepurnyi
ivan@ecomdev.org

More Related Content

What's hot

The IoC Hydra - Dutch PHP Conference 2016
The IoC Hydra - Dutch PHP Conference 2016The IoC Hydra - Dutch PHP Conference 2016
The IoC Hydra - Dutch PHP Conference 2016Kacper Gunia
 
Forget about index.php and build you applications around HTTP!
Forget about index.php and build you applications around HTTP!Forget about index.php and build you applications around HTTP!
Forget about index.php and build you applications around HTTP!Kacper Gunia
 
AngularJS Directives
AngularJS DirectivesAngularJS Directives
AngularJS DirectivesEyal Vardi
 
AngulrJS Overview
AngulrJS OverviewAngulrJS Overview
AngulrJS OverviewEyal Vardi
 
Rich domain model with symfony 2.5 and doctrine 2.5
Rich domain model with symfony 2.5 and doctrine 2.5Rich domain model with symfony 2.5 and doctrine 2.5
Rich domain model with symfony 2.5 and doctrine 2.5Leonardo Proietti
 
Min-Maxing Software Costs - Laracon EU 2015
Min-Maxing Software Costs - Laracon EU 2015Min-Maxing Software Costs - Laracon EU 2015
Min-Maxing Software Costs - Laracon EU 2015Konstantin Kudryashov
 
Doctrine For Beginners
Doctrine For BeginnersDoctrine For Beginners
Doctrine For BeginnersJonathan Wage
 
Advanced php testing in action
Advanced php testing in actionAdvanced php testing in action
Advanced php testing in actionJace Ju
 
Rich Model And Layered Architecture in SF2 Application
Rich Model And Layered Architecture in SF2 ApplicationRich Model And Layered Architecture in SF2 Application
Rich Model And Layered Architecture in SF2 ApplicationKirill Chebunin
 
Symfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technologySymfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technologyDaniel Knell
 
Sprout core and performance
Sprout core and performanceSprout core and performance
Sprout core and performanceYehuda Katz
 
Beyond symfony 1.2 (Symfony Camp 2008)
Beyond symfony 1.2 (Symfony Camp 2008)Beyond symfony 1.2 (Symfony Camp 2008)
Beyond symfony 1.2 (Symfony Camp 2008)Fabien Potencier
 
IndexedDB - Querying and Performance
IndexedDB - Querying and PerformanceIndexedDB - Querying and Performance
IndexedDB - Querying and PerformanceParashuram N
 
Dutch PHP Conference - PHPSpec 2 - The only Design Tool you need
Dutch PHP Conference - PHPSpec 2 - The only Design Tool you needDutch PHP Conference - PHPSpec 2 - The only Design Tool you need
Dutch PHP Conference - PHPSpec 2 - The only Design Tool you needKacper Gunia
 
Decoupling the Ulabox.com monolith. From CRUD to DDD
Decoupling the Ulabox.com monolith. From CRUD to DDDDecoupling the Ulabox.com monolith. From CRUD to DDD
Decoupling the Ulabox.com monolith. From CRUD to DDDAleix Vergés
 
Design Patterns in PHP5
Design Patterns in PHP5 Design Patterns in PHP5
Design Patterns in PHP5 Wildan Maulana
 
Forget about Index.php and build you applications around HTTP - PHPers Cracow
Forget about Index.php and build you applications around HTTP - PHPers CracowForget about Index.php and build you applications around HTTP - PHPers Cracow
Forget about Index.php and build you applications around HTTP - PHPers CracowKacper Gunia
 

What's hot (20)

The IoC Hydra - Dutch PHP Conference 2016
The IoC Hydra - Dutch PHP Conference 2016The IoC Hydra - Dutch PHP Conference 2016
The IoC Hydra - Dutch PHP Conference 2016
 
Forget about index.php and build you applications around HTTP!
Forget about index.php and build you applications around HTTP!Forget about index.php and build you applications around HTTP!
Forget about index.php and build you applications around HTTP!
 
AngularJS Directives
AngularJS DirectivesAngularJS Directives
AngularJS Directives
 
AngulrJS Overview
AngulrJS OverviewAngulrJS Overview
AngulrJS Overview
 
The IoC Hydra
The IoC HydraThe IoC Hydra
The IoC Hydra
 
Rich domain model with symfony 2.5 and doctrine 2.5
Rich domain model with symfony 2.5 and doctrine 2.5Rich domain model with symfony 2.5 and doctrine 2.5
Rich domain model with symfony 2.5 and doctrine 2.5
 
Min-Maxing Software Costs - Laracon EU 2015
Min-Maxing Software Costs - Laracon EU 2015Min-Maxing Software Costs - Laracon EU 2015
Min-Maxing Software Costs - Laracon EU 2015
 
PHP MVC
PHP MVCPHP MVC
PHP MVC
 
jQuery in 15 minutes
jQuery in 15 minutesjQuery in 15 minutes
jQuery in 15 minutes
 
Doctrine For Beginners
Doctrine For BeginnersDoctrine For Beginners
Doctrine For Beginners
 
Advanced php testing in action
Advanced php testing in actionAdvanced php testing in action
Advanced php testing in action
 
Rich Model And Layered Architecture in SF2 Application
Rich Model And Layered Architecture in SF2 ApplicationRich Model And Layered Architecture in SF2 Application
Rich Model And Layered Architecture in SF2 Application
 
Symfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technologySymfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technology
 
Sprout core and performance
Sprout core and performanceSprout core and performance
Sprout core and performance
 
Beyond symfony 1.2 (Symfony Camp 2008)
Beyond symfony 1.2 (Symfony Camp 2008)Beyond symfony 1.2 (Symfony Camp 2008)
Beyond symfony 1.2 (Symfony Camp 2008)
 
IndexedDB - Querying and Performance
IndexedDB - Querying and PerformanceIndexedDB - Querying and Performance
IndexedDB - Querying and Performance
 
Dutch PHP Conference - PHPSpec 2 - The only Design Tool you need
Dutch PHP Conference - PHPSpec 2 - The only Design Tool you needDutch PHP Conference - PHPSpec 2 - The only Design Tool you need
Dutch PHP Conference - PHPSpec 2 - The only Design Tool you need
 
Decoupling the Ulabox.com monolith. From CRUD to DDD
Decoupling the Ulabox.com monolith. From CRUD to DDDDecoupling the Ulabox.com monolith. From CRUD to DDD
Decoupling the Ulabox.com monolith. From CRUD to DDD
 
Design Patterns in PHP5
Design Patterns in PHP5 Design Patterns in PHP5
Design Patterns in PHP5
 
Forget about Index.php and build you applications around HTTP - PHPers Cracow
Forget about Index.php and build you applications around HTTP - PHPers CracowForget about Index.php and build you applications around HTTP - PHPers Cracow
Forget about Index.php and build you applications around HTTP - PHPers Cracow
 

Viewers also liked

Real use cases of performance optimization in magento 2
Real use cases of performance optimization in magento 2Real use cases of performance optimization in magento 2
Real use cases of performance optimization in magento 2Max Pronko
 
Oleh Kobchenko - Configure Magento 2 to get maximum performance
Oleh Kobchenko - Configure Magento 2 to get maximum performanceOleh Kobchenko - Configure Magento 2 to get maximum performance
Oleh Kobchenko - Configure Magento 2 to get maximum performanceMeet Magento Italy
 
Phpworld.2015 scaling magento
Phpworld.2015 scaling magentoPhpworld.2015 scaling magento
Phpworld.2015 scaling magentoMathew Beane
 
Making Magento flying like a rocket! (A set of valuable tips for developers)
Making Magento flying like a rocket! (A set of valuable tips for developers)Making Magento flying like a rocket! (A set of valuable tips for developers)
Making Magento flying like a rocket! (A set of valuable tips for developers)Ivan Chepurnyi
 
Magento 2.0: Prepare yourself for a new way of module development
Magento 2.0: Prepare yourself for a new way of module developmentMagento 2.0: Prepare yourself for a new way of module development
Magento 2.0: Prepare yourself for a new way of module developmentIvan Chepurnyi
 
Hidden Secrets of Magento Price Rules
Hidden Secrets of Magento Price RulesHidden Secrets of Magento Price Rules
Hidden Secrets of Magento Price RulesIvan Chepurnyi
 
Varnish Cache and its usage in the real world!
Varnish Cache and its usage in the real world!Varnish Cache and its usage in the real world!
Varnish Cache and its usage in the real world!Ivan Chepurnyi
 

Viewers also liked (8)

Real use cases of performance optimization in magento 2
Real use cases of performance optimization in magento 2Real use cases of performance optimization in magento 2
Real use cases of performance optimization in magento 2
 
Oleh Kobchenko - Configure Magento 2 to get maximum performance
Oleh Kobchenko - Configure Magento 2 to get maximum performanceOleh Kobchenko - Configure Magento 2 to get maximum performance
Oleh Kobchenko - Configure Magento 2 to get maximum performance
 
Phpworld.2015 scaling magento
Phpworld.2015 scaling magentoPhpworld.2015 scaling magento
Phpworld.2015 scaling magento
 
Making Magento flying like a rocket! (A set of valuable tips for developers)
Making Magento flying like a rocket! (A set of valuable tips for developers)Making Magento flying like a rocket! (A set of valuable tips for developers)
Making Magento flying like a rocket! (A set of valuable tips for developers)
 
Magento 2.0: Prepare yourself for a new way of module development
Magento 2.0: Prepare yourself for a new way of module developmentMagento 2.0: Prepare yourself for a new way of module development
Magento 2.0: Prepare yourself for a new way of module development
 
Hidden Secrets of Magento Price Rules
Hidden Secrets of Magento Price RulesHidden Secrets of Magento Price Rules
Hidden Secrets of Magento Price Rules
 
Varnish Cache and its usage in the real world!
Varnish Cache and its usage in the real world!Varnish Cache and its usage in the real world!
Varnish Cache and its usage in the real world!
 
Magento Indexes
Magento IndexesMagento Indexes
Magento Indexes
 

Similar to Meet Magento Sweden - Magento 2 Layout and Code Compilation for Performance

Why is crud a bad idea - focus on real scenarios
Why is crud a bad idea - focus on real scenariosWhy is crud a bad idea - focus on real scenarios
Why is crud a bad idea - focus on real scenariosDivante
 
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy ApplicationsAdding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy ApplicationsSam Hennessy
 
Design Patterns avec PHP 5.3, Symfony et Pimple
Design Patterns avec PHP 5.3, Symfony et PimpleDesign Patterns avec PHP 5.3, Symfony et Pimple
Design Patterns avec PHP 5.3, Symfony et PimpleHugo Hamon
 
Ioc container | Hannes Van De Vreken | CODEiD
Ioc container | Hannes Van De Vreken | CODEiDIoc container | Hannes Van De Vreken | CODEiD
Ioc container | Hannes Van De Vreken | CODEiDCODEiD PHP Community
 
The History of PHPersistence
The History of PHPersistenceThe History of PHPersistence
The History of PHPersistenceHugo Hamon
 
PHPCon 2016: PHP7 by Witek Adamus / XSolve
PHPCon 2016: PHP7 by Witek Adamus / XSolvePHPCon 2016: PHP7 by Witek Adamus / XSolve
PHPCon 2016: PHP7 by Witek Adamus / XSolveXSolve
 
The Origin of Lithium
The Origin of LithiumThe Origin of Lithium
The Origin of LithiumNate Abele
 
JavaScript for PHP developers
JavaScript for PHP developersJavaScript for PHP developers
JavaScript for PHP developersStoyan Stefanov
 
How Kris Writes Symfony Apps
How Kris Writes Symfony AppsHow Kris Writes Symfony Apps
How Kris Writes Symfony AppsKris Wallsmith
 
Symfony2, creare bundle e valore per il cliente
Symfony2, creare bundle e valore per il clienteSymfony2, creare bundle e valore per il cliente
Symfony2, creare bundle e valore per il clienteLeonardo Proietti
 
WordPress REST API hacking
WordPress REST API hackingWordPress REST API hacking
WordPress REST API hackingJeroen van Dijk
 
Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02Seri Moth
 
Lithium: The Framework for People Who Hate Frameworks
Lithium: The Framework for People Who Hate FrameworksLithium: The Framework for People Who Hate Frameworks
Lithium: The Framework for People Who Hate FrameworksNate Abele
 
関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐい関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐいHisateru Tanaka
 

Similar to Meet Magento Sweden - Magento 2 Layout and Code Compilation for Performance (20)

Unittests für Dummies
Unittests für DummiesUnittests für Dummies
Unittests für Dummies
 
Why is crud a bad idea - focus on real scenarios
Why is crud a bad idea - focus on real scenariosWhy is crud a bad idea - focus on real scenarios
Why is crud a bad idea - focus on real scenarios
 
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy ApplicationsAdding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
 
Design Patterns avec PHP 5.3, Symfony et Pimple
Design Patterns avec PHP 5.3, Symfony et PimpleDesign Patterns avec PHP 5.3, Symfony et Pimple
Design Patterns avec PHP 5.3, Symfony et Pimple
 
Ioc container | Hannes Van De Vreken | CODEiD
Ioc container | Hannes Van De Vreken | CODEiDIoc container | Hannes Van De Vreken | CODEiD
Ioc container | Hannes Van De Vreken | CODEiD
 
The History of PHPersistence
The History of PHPersistenceThe History of PHPersistence
The History of PHPersistence
 
Intermediate PHP
Intermediate PHPIntermediate PHP
Intermediate PHP
 
PHPCon 2016: PHP7 by Witek Adamus / XSolve
PHPCon 2016: PHP7 by Witek Adamus / XSolvePHPCon 2016: PHP7 by Witek Adamus / XSolve
PHPCon 2016: PHP7 by Witek Adamus / XSolve
 
The Origin of Lithium
The Origin of LithiumThe Origin of Lithium
The Origin of Lithium
 
JavaScript for PHP developers
JavaScript for PHP developersJavaScript for PHP developers
JavaScript for PHP developers
 
How Kris Writes Symfony Apps
How Kris Writes Symfony AppsHow Kris Writes Symfony Apps
How Kris Writes Symfony Apps
 
Symfony2, creare bundle e valore per il cliente
Symfony2, creare bundle e valore per il clienteSymfony2, creare bundle e valore per il cliente
Symfony2, creare bundle e valore per il cliente
 
SOLID PRINCIPLES
SOLID PRINCIPLESSOLID PRINCIPLES
SOLID PRINCIPLES
 
Quebec pdo
Quebec pdoQuebec pdo
Quebec pdo
 
WordPress REST API hacking
WordPress REST API hackingWordPress REST API hacking
WordPress REST API hacking
 
Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02
 
Lithium: The Framework for People Who Hate Frameworks
Lithium: The Framework for People Who Hate FrameworksLithium: The Framework for People Who Hate Frameworks
Lithium: The Framework for People Who Hate Frameworks
 
Doctrine and NoSQL
Doctrine and NoSQLDoctrine and NoSQL
Doctrine and NoSQL
 
Oops in php
Oops in phpOops in php
Oops in php
 
関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐい関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐい
 

Recently uploaded

Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Principled Technologies
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 

Recently uploaded (20)

Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 

Meet Magento Sweden - Magento 2 Layout and Code Compilation for Performance

  • 1. MAGENTO 2 LAYOUT AND CODE COMPILATION FOR PERFORMANCE   by Ivan Chepurnyi
  • 4. WHAT MAKES THEM COMPLEX?
  • 5. REPEATED DATA PROCESSING // ... some xml/json/yaml file initialization foreach ($loadedData as $item) { $this->process($item); }
  • 6. NESTED LOOPS foreach ($data as $item) { $row = []; foreach ($columns as $column) { $row[] = $column->export($item); } $csv->write($row); }
  • 7. COMPLEX DEPENDENCY TREE class ClassOne { public function __construct(ClassTwo $dependency) {} } class ClassTwo { public function __construct(ClassThree $dependency) {} } class ClassThree { public function __construct(ClassFour $dependencyOne, ClassFive $dependen } // ..
  • 8. HOW CAN WE SOLVE IT?
  • 9. REPEATED DATA PROCESSING Translate your XML/JSON/YAML file into executable PHP code and include it when you need processed structure
  • 10. NESTED LOOPS Pre-compile second loop and execute it within the main one
  • 11. COMPLEX DEPENDENCY TREE Resolve dependencies and compile resolution into executable code
  • 12. BUT COMPILATION LOOKS UGLY... You need to create PHP code within PHP code You need to write it to external file You need to include that file inside of your code
  • 13. I WAS LOOKING FOR A LIBRARY Didn't find one... So I wrote it myself.
  • 14. ECOMDEVCOMPILER Created to wrap writing PHP code within PHP Automatically stores compiled code Automatically validates source and re-compiles code when needed Provides easy to use API to create parsers, builders and executors
  • 15. INSTALLATION 1. Available as a composer dependency 2. Instantiate it directly or via DI container. composer require "ecomdev/compiler"
  • 18. XML FILE <objects> <item id="object_one" type="object" /> <item id="object_two" type="object" /> <item id="object_three" type="object" /> <type id="object" class="SomeClassName"/> </objects>
  • 19. PARSER use EcomDevCompilerStatementBuilder; class Parser implements EcomDevCompilerParserInterface { // .. constructor with builder as dependency public function parse($value) { $xml = simplexml_load_string($value); $info = $this->readXml($xml); return $this->getPhpCode($info, $this->builder); } // .. other methods }
  • 20. PARSE XML DATA private function readXml($xml) { $info = []; foreach ($xml->children() as $node) { if ($node->getName() === 'type') { $info['types'][(string)$node->id] = (string)$node->class; } elseif ($node->getName() === 'object') { $info['objects'][(string)$node->id] = (string)$node->type; } } return $info; }
  • 21. CREATE PHP CODE private function getPhpCode($info, $builder) { $compiledArray = []; foreach ($info['objects'] as $objectId => $type) { $compiledArray[$objectId] = $builder->instance($info['types'][ } return $builder->container( $builder->returnValue($compiledArray) ); }
  • 22. COMPILED PHP FILE return [ 'object_one' => new SomeClassName(), 'object_two' => new SomeClassName(), 'object_three' => new SomeClassName() ];
  • 24. YOUR CONSTRUCTOR public function __construct( EcomDevCompilerBuilder $builder, EcomDevCompilerCompiler $compiler) { $this->builder = $builder; $this->compiler = $compiler; }
  • 25. EXPORT METHOD public function export($data, $columns) { $statements = $this->compileColumns($columns, $this->builder); $source = new EcomDevCompilerSourceStaticData( 'your_id', 'your_checksum', $statements ); $reference = $this->compiler->compile($source); $closure = $this->compiler->interpret($reference); foreach ($data as $item) { $row = $closure($item, $columns); } }
  • 26. COMPILATION METHOD public function compileColumns($columns, $builder) { $item = $builder->variable('item'); $compiledArray = []; foreach ($columns as $id => $column) { $compiledArray[] = $builder->chainVariable('columns')[$id] ->export($item); } $closure = $builder->closure( [$item, $builder->variable('columns')], $builder->container([$builder->returnValue($compiledArray)]) ); return $builder->container([$builder->returnValue($closure)]); }
  • 27. RESULT return function ($item, $columns) { return [ $columns['id1']->export($item), $columns['id2']->export($item), $columns['id3']->export($item), // ... ]; };
  • 28. MAIN COMPONENTS CompilerInterface - Compiler instance StorageInterface - Stores compiled files SourceInterface - Provider of data (File, String, StaticData) ParserInterface - Parser of data ObjectBuilderInterface - Bound builder for included files
  • 29. AND SOME SWEET STUFF...
  • 30. EXPORTABLE OBJECTS class SomeClass implements EcomDevCompilerExportableInterface { public function __construct($foo, $bar) { /* */ } public function export() { return [ 'foo' => $this->foo, 'bar' => $this->bar ]; } } Will be automatically compiled into: new SomeClass('fooValue', 'barValue');
  • 32. WHY? BECAUSE OF ITS ALGORITHM
  • 33. LAYOUT CACHING Every handle that is added to the MagentoFrameworkViewResult changes the cache key for the whole generated structure.
  • 34. LAYOUT GENERATION Scheduled structure is generated from XML object at all times
  • 35. SOLUTION 1. Make every handle a compiled php code 2. Include compiled handles at loading phase
  • 36. GOOD NEWS I am already working on it Will be release in April 2016
  • 37. GITHUB COMPILER LIBRARY FOR M2 https://github.com/EcomDev/compiler LAYOUT COMPILER FOR M1 https://github.com/EcomDev/EcomDev_LayoutCompiler LAYOUT COMPILER FOR M2 Coming soon