Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

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

20,991 views

Published on

It is a pity, but I have to admit, that Magento 2 has issues in layout generation process. You'll learn during the talk about the benefits of compiling XML structures into PHP code, and even use the compilation of PHP code into PHP code to speed up your modules complex logic.

Published in: Technology
  • Be the first to comment

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

  1. 1. MAGENTO 2 LAYOUT AND CODE COMPILATION FOR PERFORMANCE   by Ivan Chepurnyi
  2. 2. WHAT? COMPILATION?
  3. 3. COMPLEX ALGORITHMSSIMPLE
  4. 4. WHAT MAKES THEM COMPLEX?
  5. 5. REPEATED DATA PROCESSING // ... some xml/json/yaml file initialization foreach ($loadedData as $item) { $this->process($item); }
  6. 6. NESTED LOOPS foreach ($data as $item) { $row = []; foreach ($columns as $column) { $row[] = $column->export($item); } $csv->write($row); }
  7. 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. 8. HOW CAN WE SOLVE IT?
  9. 9. REPEATED DATA PROCESSING Translate your XML/JSON/YAML file into executable PHP code and include it when you need processed structure
  10. 10. NESTED LOOPS Pre-compile second loop and execute it within the main one
  11. 11. COMPLEX DEPENDENCY TREE Resolve dependencies and compile resolution into executable code
  12. 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. 13. I WAS LOOKING FOR A LIBRARY Didn't find one... So I wrote it myself.
  14. 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. 15. INSTALLATION 1. Available as a composer dependency 2. Instantiate it directly or via DI container. composer require "ecomdev/compiler"
  16. 16. SOME EXAMPLES
  17. 17. COMPILE XML INTO PHP
  18. 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. 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. 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. 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. 22. COMPILED PHP FILE return [ 'object_one' => new SomeClassName(), 'object_two' => new SomeClassName(), 'object_three' => new SomeClassName() ];
  23. 23. NESTED LOOP SIMPLIFYING
  24. 24. YOUR CONSTRUCTOR public function __construct( EcomDevCompilerBuilder $builder, EcomDevCompilerCompiler $compiler) { $this->builder = $builder; $this->compiler = $compiler; }
  25. 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. 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. 27. RESULT return function ($item, $columns) { return [ $columns['id1']->export($item), $columns['id2']->export($item), $columns['id3']->export($item), // ... ]; };
  28. 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. 29. AND SOME SWEET STUFF...
  30. 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');
  31. 31. MAGENTO 2.0 LAYOUT COMPILATION IS A MUST
  32. 32. WHY? BECAUSE OF ITS ALGORITHM
  33. 33. LAYOUT CACHING Every handle that is added to the MagentoFrameworkViewResult changes the cache key for the whole generated structure.
  34. 34. LAYOUT GENERATION Scheduled structure is generated from XML object at all times
  35. 35. SOLUTION 1. Make every handle a compiled php code 2. Include compiled handles at loading phase
  36. 36. GOOD NEWS I am already working on it Will be release in April 2016
  37. 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
  38. 38. Q&A @IvanChepurnyi ivan@ecomdev.org

×