xp-framework / compiler

Compiles future PHP to today's PHP.
19 stars 0 forks source link

Inlining #97

Closed thekid closed 3 years ago

thekid commented 3 years ago

Proof of concept:

<?php namespace lang\ast\syntax\php;

use lang\ast\nodes\Braced;
use lang\ast\syntax\Extension;

class Inline implements Extension {
  private $inline= [];

  public function setup($language, $emitter) {
    $emitter->transform('method', function($codegen, $node) {
      if ($node->annotation('Inline')) {
        $this->inline[$node->name]= $node;
      }
    });
    $emitter->transform('scope', function($codegen, $node) {
      if ('self' === $node->type && $node->member->is('invoke')) {
        if ($method= $this->inline[$node->member->expression->expression] ?? null) {
          if ($statement= $method->body[0] ?? null && $statement instanceof ReturnNode) {
            return new Braced($method->body[0]->expression);
          }
        }
      }
    });
  }
}

Inlining a private method yields a performance difference of 1.560 seconds vs. 1.826 seconds for 10 million invocations, not sure about the real benefit here.

thekid commented 3 years ago

not sure about the real benefit here.

After thinking about this, doesn't seem to have a good case.