SlideShare una empresa de Scribd logo
1 de 67
Descargar para leer sin conexión
Introduction to phc
                         Current state of phc
     Next for phc - Analysis and Optimization
                       Experiences with PHP




Compiling and Optimizing Scripting Languages

                     Paul Biggar and David Gregg

                 Department of Computer Science and Statistics
                            Trinity College Dublin


                        Google, 18th March, 2009




                                                 Trinity College Dublin   1
Introduction to phc
                            Current state of phc
        Next for phc - Analysis and Optimization
                          Experiences with PHP


Motivation


     User needs web page in 0.5 seconds
         Execution time
         DB access
         Network latency
         Browser rendering

     Easier maintainance

     What if execution was:
         2x as fast?
         10x as fast?



                                                    Trinity College Dublin   2
Introduction to phc
                              Current state of phc
          Next for phc - Analysis and Optimization
                            Experiences with PHP


Outline

      Introduction to phc
  1


      Current state of phc
  2
        Challenges to compilation?
        phc solution: use the C API
        Speedup

      Next for phc - Analysis and Optimization
  3
        Simple Optimizations
        Advanced Optimizations

      Experiences with PHP
  4




                                                      Trinity College Dublin   3
Introduction to phc
                              Current state of phc
          Next for phc - Analysis and Optimization
                            Experiences with PHP


Outline

      Introduction to phc
  1


      Current state of phc
  2
        Challenges to compilation?
        phc solution: use the C API
        Speedup

      Next for phc - Analysis and Optimization
  3
        Simple Optimizations
        Advanced Optimizations

      Experiences with PHP
  4




                                                      Trinity College Dublin   4
Introduction to phc
                             Current state of phc
         Next for phc - Analysis and Optimization
                           Experiences with PHP


phc



      http://phpcompiler.org
      Ahead-of-time compiler for PHP
      Edsko de Vries, John Gilbert, Paul Biggar
      BSD license
      Latest release: 0.2.0.3 - compiles non-OO
      svn trunk: compiles most OO




                                                     Trinity College Dublin   5
Introduction to phc
                            Current state of phc
        Next for phc - Analysis and Optimization
                          Experiences with PHP


Structure of phc




                                                    Trinity College Dublin   6
Introduction to phc
                          Current state of phc
      Next for phc - Analysis and Optimization
                        Experiences with PHP


PHP




                  <?php

                       echo quot;helloquot;, quot;world!quot;;

                  ?>




                                                  Trinity College Dublin   7
Introduction to phc
                          Current state of phc
      Next for phc - Analysis and Optimization
                        Experiences with PHP


AST
                                                   PHP_script




                                                 List<Statement>




                                      Eval_expr (3)             Nop (5)




                                  Method_invocation (3)



                     NULL
                                  METHOD_NAME (3)                         List<Actual_parameter>
                    (Target)



                                          echo                     Actual_parameter (3)            Actual_parameter (3)




                                                                      STRING (3)                      STRING (3)




                                                                            hello                        world!


                                                           Trinity College Dublin                                         8
Introduction to phc
                          Current state of phc
      Next for phc - Analysis and Optimization
                        Experiences with PHP


HIR


                  <?php
                    $x = $a + $b + $c + $d;
                  ?>



                  <?php
                    $TLE0 = ($a + $b);
                    $TLE1 = ($TLE0 + $c);
                    $x = ($TLE1 + $d);
                  ?>


                                                  Trinity College Dublin   9
Introduction to phc
                          Current state of phc
      Next for phc - Analysis and Optimization
                        Experiences with PHP


MIR
                  <?php
                    while ($cond)
                      echo quot;helloquot;, quot;world!quot;;
                  ?>

                  <?php
                  L7:
                    $TLE0 = !$cond;
                    if ($TLE0) goto L3 else goto L6;
                  L6:
                    print(’hello’);
                    print(’world!’);
                    goto L7;
                  L3:
                  ?>
                                                  Trinity College Dublin   10
Introduction to phc
                              Current state of phc
          Next for phc - Analysis and Optimization
                            Experiences with PHP


Plugins



                         http://phpcompiler.org/doc/latest/devmanual.html




                                                      Trinity College Dublin   11
Introduction to phc
                          Current state of phc
      Next for phc - Analysis and Optimization
                        Experiences with PHP


XML
                        <?xml version=quot;1.0quot;?>
                        <AST:PHP_script xmlns:AST=quot;http://www.phpcompiler.org/phc-1.1quot;>
                          <AST:Statement_list>
                            <AST:Eval_expr>
                              <AST:Method_invocation>
                                <AST:Target xsi:nil=quot;truequot; />
                                <AST:METHOD_NAME>
                                  <value>echo</value>
                                </AST:METHOD_NAME>
                                <AST:Actual_parameter_list>
                                  <AST:Actual_parameter>
                                    <bool><!-- is_ref -->false</bool>
                                    <AST:STRING>
                                      <value>hello</value>
                                    </AST:STRING>
                                  </AST:Actual_parameter>
                                  <AST:Actual_parameter>
                                    <bool><!-- is_ref -->false</bool>
                                    <AST:STRING>
                                      <value>world!</value>
                                    </AST:STRING>
                                  </AST:Actual_parameter>
                                </AST:Actual_parameter_list>
                              </AST:Method_invocation>
                            </AST:Eval_expr>
                            <AST:Nop>
                            </AST:Nop>
                          </AST:Statement_list>
                        </AST:PHP_script>
                                              Trinity College Dublin                      12
Introduction to phc
                                                      Challenges to compilation?
                              Current state of phc
                                                      phc solution: use the C API
          Next for phc - Analysis and Optimization
                                                      Speedup
                            Experiences with PHP


Outline

      Introduction to phc
  1


      Current state of phc
  2
        Challenges to compilation?
        phc solution: use the C API
        Speedup

      Next for phc - Analysis and Optimization
  3
        Simple Optimizations
        Advanced Optimizations

      Experiences with PHP
  4




                                                      Trinity College Dublin        13
Introduction to phc
                                                   Challenges to compilation?
                           Current state of phc
                                                   phc solution: use the C API
       Next for phc - Analysis and Optimization
                                                   Speedup
                         Experiences with PHP


SAC 2009


    A Practical Solution for Scripting Language
                     Compilers

        Paul Biggar, Edsko de Vries and David Gregg

                   Department of Computer Science and Statistics
                              Trinity College Dublin


      ACM Symposium on Applied Computing - PL track
                  12th March, 2009


                                                   Trinity College Dublin        14
Introduction to phc
                                                   Challenges to compilation?
                           Current state of phc
                                                   phc solution: use the C API
       Next for phc - Analysis and Optimization
                                                   Speedup
                         Experiences with PHP


Sneak peak




    Problem: Scripting languages present “unique” problems
    (in practice)


    Solution: Re-use as much of the Canonical Reference
    Implementation as possible.




                                                   Trinity College Dublin        15
Introduction to phc
                                                      Challenges to compilation?
                              Current state of phc
                                                      phc solution: use the C API
          Next for phc - Analysis and Optimization
                                                      Speedup
                            Experiences with PHP


Outline

      Introduction to phc
  1


      Current state of phc
  2
        Challenges to compilation?
        phc solution: use the C API
        Speedup

      Next for phc - Analysis and Optimization
  3
        Simple Optimizations
        Advanced Optimizations

      Experiences with PHP
  4




                                                      Trinity College Dublin        16
Introduction to phc
                                                    Challenges to compilation?
                            Current state of phc
                                                    phc solution: use the C API
        Next for phc - Analysis and Optimization
                                                    Speedup
                          Experiences with PHP


Undefined


     The PHP group claim that they have the final say in
     the specification of PHP. This group’s specification is
     an implementation, and there is no prose specification
     or agreed validation suite. There are alternate
     implementations [...] that claim to be compatible (they
     don’t say what this means) with some version of PHP.

 D. M. Jones. Forms of language specification: Examples from
 commonly used computer languages. ISO/IEC
 JTC1/SC22/OWG/N0121, February 2008.



                                                    Trinity College Dublin        17
Introduction to phc
                                                     Challenges to compilation?
                             Current state of phc
                                                     phc solution: use the C API
         Next for phc - Analysis and Optimization
                                                     Speedup
                           Experiences with PHP


Batteries included




  Jeff Atwood, Coding Horror, May 20th, 2008
  http://www.codinghorror.com/blog/archives/001119.html
                                                     Trinity College Dublin        18
Introduction to phc
                                                     Challenges to compilation?
                             Current state of phc
                                                     phc solution: use the C API
         Next for phc - Analysis and Optimization
                                                     Speedup
                           Experiences with PHP


Change between releases


  <?php
    var_dump (0x9fa0ff0b);
  ?>


  PHP 5.2.1 (32-bit)
  int(2147483647)

  PHP 5.2.3 (32-bit)
  float(2678128395)


                                                     Trinity College Dublin        19
Introduction to phc
                                                   Challenges to compilation?
                           Current state of phc
                                                   phc solution: use the C API
       Next for phc - Analysis and Optimization
                                                   Speedup
                         Experiences with PHP


Run-time code generation

  <?php
    eval ($argv[1]);
  ?>



  <?php
    include (quot;mylib.phpquot;);
    ...
    include (quot;plugin.phpquot;);
    ...
  ?>

                                                   Trinity College Dublin        20
Introduction to phc
                                                      Challenges to compilation?
                              Current state of phc
                                                      phc solution: use the C API
          Next for phc - Analysis and Optimization
                                                      Speedup
                            Experiences with PHP


Outline

      Introduction to phc
  1


      Current state of phc
  2
        Challenges to compilation?
        phc solution: use the C API
        Speedup

      Next for phc - Analysis and Optimization
  3
        Simple Optimizations
        Advanced Optimizations

      Experiences with PHP
  4




                                                      Trinity College Dublin        21
Introduction to phc
                                                   Challenges to compilation?
                           Current state of phc
                                                   phc solution: use the C API
       Next for phc - Analysis and Optimization
                                                   Speedup
                         Experiences with PHP


Use C API




                                                   Trinity College Dublin        22
Introduction to phc
                                                      Challenges to compilation?
                             Current state of phc
                                                      phc solution: use the C API
         Next for phc - Analysis and Optimization
                                                      Speedup
                           Experiences with PHP


More detail


                     PHP                             zval
                     Python                          PyObject
                     Ruby                            VALUE
                     Lua                             TValue
  H. Muhammad and R. Ierusalimschy. C APIs in extension and
  extensible languages. Journal of Universal Computer Science,
  13(6):839–853, 2007.


                                                      Trinity College Dublin        23
Introduction to phc
                                                     Challenges to compilation?
                             Current state of phc
                                                     phc solution: use the C API
         Next for phc - Analysis and Optimization
                                                     Speedup
                           Experiences with PHP


Simple listings: $i = 0



  // $i = 0;
  {
    zval* p_i;
    php_hash_find (LOCAL_ST, quot;iquot;, 5863374, p_i);
    php_destruct (p_i);
    php_allocate (p_i);
    ZVAL_LONG (*p_i, 0);
  }




                                                     Trinity College Dublin        24
Introduction to phc
                                                         Challenges to compilation?
                                 Current state of phc
                                                         phc solution: use the C API
             Next for phc - Analysis and Optimization
                                                         Speedup
                               Experiences with PHP


Example: $i = 0
  // $i = 0;
  {
    if (local_i == NULL)
    {
      local_i = EG (uninitialized_zval_ptr);
      local_i->refcount++;
    }
    zval **p_lhs = &local_i;

      zval *value;
      if ((*p_lhs)->is_ref)
      {
        // Always overwrite the current value
        value = *p_lhs;
        zval_dtor (value);
      }
      else
      {
        ALLOC_INIT_ZVAL (value);
        zval_ptr_dtor (p_lhs);
        *p_lhs = value;
      }

      ZVAL_LONG (value, 0);
  }
                                                         Trinity College Dublin        25
Introduction to phc
                                                                        Challenges to compilation?
                                        Current state of phc
                                                                        phc solution: use the C API
                    Next for phc - Analysis and Optimization
                                                                        Speedup
                                      Experiences with PHP


Example: $i = $j
  // $i = $j;
  {
    if (local_i == NULL)
    {
      local_i = EG (uninitialized_zval_ptr);
      local_i->refcount++;
    }
    zval **p_lhs = &local_i;

      zval *rhs;
      if (local_j == NULL)
        rhs = EG (uninitialized_zval_ptr);
      else
        rhs = local_j;

      if (*p_lhs != rhs)
      {
        if ((*p_lhs)->is_ref)
        {
          // First, call the destructor to remove any data structures
          // associated with lhs that will now be overwritten
          zval_dtor (*p_lhs);
          // Overwrite LHS
          (*p_lhs)->value = rhs->value;
          (*p_lhs)->type = rhs->type;
          zval_copy_ctor (*p_lhs);
        }
        else
        {
          zval_ptr_dtor (p_lhs);
          if (rhs->is_ref)
          {
            // Take a copy of RHS for LHS
            *p_lhs = zvp_clone_ex (rhs);
          }
          else
          {
            // Share a copy
            rhs->refcount++;
            *p_lhs = rhs;
          }
        }
      }
                                                                        Trinity College Dublin        26
  }
Introduction to phc
                                                                                                                    Challenges to compilation?
                                                                                            Current state of phc
                                                                                                                    phc solution: use the C API
                                                                        Next for phc - Analysis and Optimization
                                                                                                                    Speedup
                                                                                          Experiences with PHP


Example: printf ($f)
  static zend_fcall_info printf_fci;
  static zend_fcall_info_cache printf_fcic = { 0, NULL, NULL, NULL };

  // printf($f);
  {
    if (!printf_fcic->initialized)
    {
      zval fn;
      INIT_PZVAL (&fn);
      ZVAL_STRING (&fn, quot;printfquot;, 0);
      int result = zend_fcall_info_init (&fn, &printf_fci, &printf_fcic TSRMLS_CC);
      if (result != SUCCESS)
      {
        phc_setup_error (1, quot;listings_source.phpquot;, 8, NULL TSRMLS_CC);
        php_error_docref (NULL TSRMLS_CC, E_ERROR,
            quot;Call to undefined function %s()quot;, function_name);
      }
    }

      zend_function *signature = printf_fcic.function_handler;
      zend_arg_info *arg_info = signature->common.arg_info; // optional

      int by_ref[1];
      int abr_index = 0;
      // TODO: find names to replace index
      if (arg_info)
      {
        by_ref[abr_index] = arg_info->pass_by_reference;
        arg_info++;
      }
      else
        by_ref[abr_index] = signature->common.pass_rest_by_reference;

      abr_index++;


      // Setup array of arguments
      // TODO: i think arrays of size 0 is an error
      int destruct[1];
      zval *args[1];
      zval **args_ind[1];

      int af_index = 0;
      destruct[af_index] = 0;
      if (by_ref[af_index])
      {
        if (local_f == NULL)
        {
          local_f = EG (uninitialized_zval_ptr);
          local_f->refcount++;
        }
        zval **p_arg = &local_f;

          // We don’t need to restore ->is_ref afterwards,
          // because the called function will reduce the
          // refcount of arg on return, and will reset is_ref to
          // 0 when refcount drops to 1. If the refcount does
          // not drop to 1 when the function returns, but we did
          // set is_ref to 1 here, that means that is_ref must
          // already have been 1 to start with (since if it had
          // not, that means that the variable would have been
          // in a copy-on-write set, and would have been
          // seperated above).
          (*p_arg)->is_ref = 1;

          args_ind[af_index] = p_arg;

        assert (!in_copy_on_write (*args_ind[af_index]));
        args[af_index] = *args_ind[af_index];
      }
      else
      {
        zval *arg;
        if (local_f == NULL)
          arg = EG (uninitialized_zval_ptr);
        else
          arg = local_f;

          args[af_index] = fetch_var_arg (arg, &destruct[af_index]);
          if (arg->is_ref)
          {
            // We dont separate since we don’t own one of ARG’s references.
            arg = zvp_clone_ex (arg);
            destruct[af_index] = 1;

            // It seems we get incorrect refcounts without this.
            // TODO This decreases the refcount to zero, which seems wrong,
            // but gives the right answer. We should look at how zend does
            // this.

            arg->refcount--;
          }
          args[af_index] = arg;

          args_ind[af_index] = &args[af_index];
      }
      af_index++;


      phc_setup_error (1, quot;listings_source.phpquot;, 8, NULL TSRMLS_CC);

      // save existing parameters, in case of recursion
      int param_count_save = printf_fci.param_count;
      zval ***params_save = printf_fci.params;
      zval **retval_save = printf_fci.retval_ptr_ptr;

      zval *rhs = NULL;

      // set up params
      printf_fci.params = args_ind;
      printf_fci.param_count = 1;
      printf_fci.retval_ptr_ptr = &rhs;

      // call the function
      int success = zend_call_function (&printf_fci, &printf_fcic TSRMLS_CC);
      assert (success == SUCCESS);

      // restore params
      printf_fci.params = params_save;
      printf_fci.param_count = param_count_save;
      printf_fci.retval_ptr_ptr = retval_save;

      // unset the errors
      phc_setup_error (0, NULL, 0, NULL TSRMLS_CC);

      int i;
      for (i = 0; i < 1; i++)
      {
        if (destruct[i])
        {
          assert (destruct[i]);
          zval_ptr_dtor (args_ind[i]);
        }
      }


      // When the Zend engine returns by reference, it allocates a zval into
      // retval_ptr_ptr. To return by reference, the callee writes into the
      // retval_ptr_ptr, freeing the allocated value as it does. (Note, it may
      // not actually return anything). So the zval returned - whether we return
      // it, or it is the allocated zval - has a refcount of 1.

      // The caller is responsible for cleaning that up (note, this is unaffected
      // by whether it is added to some COW set).

      //   For reasons unknown, the Zend API resets the refcount and is_ref fields
      //   of the return value after the function returns (unless the callee is
      //   interpreted). If the function is supposed to return by reference, this
      //   loses the refcount. This only happens when non-interpreted code is
      //   called. We work around it, when compiled code is called, by saving the
      //   refcount into SAVED_REFCOUNT, in the return statement. The downside is
      //   that we may create an error if our code is called by a callback, and
      //   returns by reference, and the callback returns by reference. At least
      //   this is an obscure case.
      if   (signature->common.return_reference
            && signature->type != ZEND_USER_FUNCTION)
      {
          assert (rhs != EG (uninitialized_zval_ptr));
          rhs->is_ref = 1;
          if (saved_refcount != 0)
          {
            rhs->refcount = saved_refcount;
          }
          rhs->refcount++;
      }
      saved_refcount = 0;     // for ’obscure cases’

      zval_ptr_dtor (&rhs);



                                                                                                                    Trinity College Dublin        27
      if (signature->common.return_reference
          && signature->type != ZEND_USER_FUNCTION)
        zval_ptr_dtor (&rhs);
  }
Introduction to phc
                                                     Challenges to compilation?
                             Current state of phc
                                                     phc solution: use the C API
         Next for phc - Analysis and Optimization
                                                     Speedup
                           Experiences with PHP


Applicability

      Everything
          Perl
          PHP
          Ruby
          Tcl – I think




                                                     Trinity College Dublin        28
Introduction to phc
                                                     Challenges to compilation?
                             Current state of phc
                                                     phc solution: use the C API
         Next for phc - Analysis and Optimization
                                                     Speedup
                           Experiences with PHP


Applicability

      Everything
          Perl
          PHP
          Ruby
          Tcl – I think


      Except specification
          Lua
          Python




                                                     Trinity College Dublin        28
Introduction to phc
                                                     Challenges to compilation?
                             Current state of phc
                                                     phc solution: use the C API
         Next for phc - Analysis and Optimization
                                                     Speedup
                           Experiences with PHP


Applicability

      Everything
           Perl
           PHP
           Ruby
           Tcl – I think


      Except specification
           Lua
           Python


      Not at all
           Javascript


                                                     Trinity College Dublin        28
Introduction to phc
                                                      Challenges to compilation?
                              Current state of phc
                                                      phc solution: use the C API
          Next for phc - Analysis and Optimization
                                                      Speedup
                            Experiences with PHP


Outline

      Introduction to phc
  1


      Current state of phc
  2
        Challenges to compilation?
        phc solution: use the C API
        Speedup

      Next for phc - Analysis and Optimization
  3
        Simple Optimizations
        Advanced Optimizations

      Experiences with PHP
  4




                                                      Trinity College Dublin        29
Introduction to phc
                                                    Challenges to compilation?
                            Current state of phc
                                                    phc solution: use the C API
        Next for phc - Analysis and Optimization
                                                    Speedup
                          Experiences with PHP


Original Speed-up




                                           0.1x
                       (10 times slower than the PHP interpreter)




                                                    Trinity College Dublin        30
Introduction to phc
                                                    Challenges to compilation?
                            Current state of phc
                                                    phc solution: use the C API
        Next for phc - Analysis and Optimization
                                                    Speedup
                          Experiences with PHP


The problem with copies

  <?php
    for ($i = 0; $i < $n; $i++)
      $str = $str . quot;helloquot;;
  ?>



  <?php
    for ($i = 0; $i < $n; $i++)
    {
      $T = $str . quot;helloquot;;
      $str = $T;
    }
  ?>
                                                    Trinity College Dublin        31
Introduction to phc
                                                    Challenges to compilation?
                            Current state of phc
                                                    phc solution: use the C API
        Next for phc - Analysis and Optimization
                                                    Speedup
                          Experiences with PHP


Optimization

     Constant folding




<?php                                               <?php
   ...                                                ...
   $T = quot;5quot; + true;                                   $T = 6;
   ...                                                ...
?>                                                  ?>




                                                    Trinity College Dublin        32
Introduction to phc
                                                    Challenges to compilation?
                            Current state of phc
                                                    phc solution: use the C API
        Next for phc - Analysis and Optimization
                                                    Speedup
                          Experiences with PHP


Optimization

     Constant folding
     Constant pooling




  <?php
    $sum = 0;
    for ($i = 0; $i < 10; $i=$i+1)
    {
      $sum .= quot;helloquot;;
    }
  ?>


                                                    Trinity College Dublin        32
Introduction to phc
                                                        Challenges to compilation?
                                Current state of phc
                                                        phc solution: use the C API
            Next for phc - Analysis and Optimization
                                                        Speedup
                              Experiences with PHP


Optimization

        Constant folding
        Constant pooling
        Function caching



  // printf ($f);
  static php_fcall_info printf_info;
  {
    php_fcall_info_init (quot;printfquot;, &printf_info);

      php_hash_find (
        LOCAL_ST, quot;fquot;, 5863275, &printf_info.params);

      php_call_function (&printf_info);
  }

                                                        Trinity College Dublin        32
Introduction to phc
                                                    Challenges to compilation?
                            Current state of phc
                                                    phc solution: use the C API
        Next for phc - Analysis and Optimization
                                                    Speedup
                          Experiences with PHP


Optimization

     Constant folding
     Constant pooling
     Function caching
     Pre-hashing

  // $i = 0;
  {
    zval* p_i;
    php_hash_find (LOCAL_ST, quot;iquot;, 5863374, p_i);
    php_destruct (p_i);
    php_allocate (p_i);
    ZVAL_LONG (*p_i, 0);
  }

                                                    Trinity College Dublin        32
Introduction to phc
                                                    Challenges to compilation?
                            Current state of phc
                                                    phc solution: use the C API
        Next for phc - Analysis and Optimization
                                                    Speedup
                          Experiences with PHP


Optimization

     Constant folding
     Constant pooling
     Function caching
     Pre-hashing
     Symbol-table removal
  // $i = 0;
  {
    php_destruct (local_i);
    php_allocate (local_i);
    ZVAL_LONG (*local_i, 0);
  }



                                                    Trinity College Dublin        32
Introduction to phc
                                                                                                                      Challenges to compilation?
                                                            Current state of phc
                                                                                                                      phc solution: use the C API
                                        Next for phc - Analysis and Optimization
                                                                                                                      Speedup
                                                          Experiences with PHP


Current speed-up
                                   3
  Speedup of compiled benchmark




                                  2.5

                                   2

                                  1.5

                                   1

                                  0.5

                                   0
                                           ackermann
                                                       ary
                                                             ary2
                                                                    ary3
                                                                           fibo
                                                                                  hash1
                                                                                          hash2
                                                                                                  heapsort
                                                                                                             mandel
                                                                                                                       mandel2
                                                                                                                                 matrix
                                                                                                                                          nestedloop
                                                                                                                                                       sieve
                                                                                                                                                               simple
                                                                                                                                                                        simplecall
                                                                                                                                                                                     simpleucall
                                                                                                                                                                                                   simpleudcall
                                                                                                                                                                                                                  strcat
                                                                                                                                                                                                                           mean
                                                                                                                      Trinity College Dublin                                                                                      33
Introduction to phc
                              Current state of phc    Simple Optimizations
          Next for phc - Analysis and Optimization    Advanced Optimizations
                            Experiences with PHP


Outline

      Introduction to phc
  1


      Current state of phc
  2
        Challenges to compilation?
        phc solution: use the C API
        Speedup

      Next for phc - Analysis and Optimization
  3
        Simple Optimizations
        Advanced Optimizations

      Experiences with PHP
  4




                                                      Trinity College Dublin   34
Introduction to phc
                              Current state of phc    Simple Optimizations
          Next for phc - Analysis and Optimization    Advanced Optimizations
                            Experiences with PHP


Outline

      Introduction to phc
  1


      Current state of phc
  2
        Challenges to compilation?
        phc solution: use the C API
        Speedup

      Next for phc - Analysis and Optimization
  3
        Simple Optimizations
        Advanced Optimizations

      Experiences with PHP
  4




                                                      Trinity College Dublin   35
Introduction to phc
                            Current state of phc    Simple Optimizations
        Next for phc - Analysis and Optimization    Advanced Optimizations
                          Experiences with PHP


Intra-procedural optimizations




                                                            Dead-code elimination
                                                            Sparse-conditional
                                                            constant propagation




                                                    Trinity College Dublin          36
Introduction to phc
                              Current state of phc    Simple Optimizations
          Next for phc - Analysis and Optimization    Advanced Optimizations
                            Experiences with PHP


Type-inference


  <?php

    function a ($x, $y)
    {
      $str = $x . $y;
      ...
      return $str;
    }

  ?>



                                                      Trinity College Dublin   37
Introduction to phc
                               Current state of phc    Simple Optimizations
           Next for phc - Analysis and Optimization    Advanced Optimizations
                             Experiences with PHP


User-space handlers

     __toString
     __get
     __set
     __isset
     __unset
     __sleep
     __wake
     __call
     __callStatic
     ...

                                                       Trinity College Dublin   38
Introduction to phc
                               Current state of phc    Simple Optimizations
           Next for phc - Analysis and Optimization    Advanced Optimizations
                             Experiences with PHP


C API handlers


     read_property
     read_dimension
     get
     set
     cast_object
     has_property
     unset_property
     ...



                                                       Trinity College Dublin   39
Introduction to phc
                            Current state of phc    Simple Optimizations
        Next for phc - Analysis and Optimization    Advanced Optimizations
                          Experiences with PHP


Unknown types propagate



     local symbol table
     global symbol table
     return values
     reference parameters
     callee parameters




                                                    Trinity College Dublin   40
Introduction to phc
                              Current state of phc    Simple Optimizations
          Next for phc - Analysis and Optimization    Advanced Optimizations
                            Experiences with PHP


Outline

      Introduction to phc
  1


      Current state of phc
  2
        Challenges to compilation?
        phc solution: use the C API
        Speedup

      Next for phc - Analysis and Optimization
  3
        Simple Optimizations
        Advanced Optimizations

      Experiences with PHP
  4




                                                      Trinity College Dublin   41
Introduction to phc
                            Current state of phc    Simple Optimizations
        Next for phc - Analysis and Optimization    Advanced Optimizations
                          Experiences with PHP


Analysis design


     Must model types precisely
         (Possibly unnamed) fields, arrays, variables and method
         calls




                                                    Trinity College Dublin   42
Introduction to phc
                            Current state of phc    Simple Optimizations
        Next for phc - Analysis and Optimization    Advanced Optimizations
                          Experiences with PHP


Analysis design


     Must model types precisely
         (Possibly unnamed) fields, arrays, variables and method
         calls

     Uses and definitions incomplete
         Can’t use def-use chains
         Can’t use SSA




                                                    Trinity College Dublin   42
Introduction to phc
                            Current state of phc    Simple Optimizations
        Next for phc - Analysis and Optimization    Advanced Optimizations
                          Experiences with PHP


Analysis design


     Must model types precisely
         (Possibly unnamed) fields, arrays, variables and method
         calls

     Uses and definitions incomplete
         Can’t use def-use chains
         Can’t use SSA

     Imprecise callgraph




                                                    Trinity College Dublin   42
Introduction to phc
                            Current state of phc    Simple Optimizations
        Next for phc - Analysis and Optimization    Advanced Optimizations
                          Experiences with PHP


Algorithm

     Abstract Execution / Interpretation




                                                    Trinity College Dublin   43
Introduction to phc
                            Current state of phc    Simple Optimizations
        Next for phc - Analysis and Optimization    Advanced Optimizations
                          Experiences with PHP


Algorithm

     Abstract Execution / Interpretation

     Points-to analysis
         *-sensitive




                                                    Trinity College Dublin   43
Introduction to phc
                              Current state of phc    Simple Optimizations
          Next for phc - Analysis and Optimization    Advanced Optimizations
                            Experiences with PHP


Algorithm

      Abstract Execution / Interpretation

      Points-to analysis
           *-sensitive

      Constant-propagation
           Precision
           Array-indices/field names
           Implicit conversions


  A. Pioli. Conditional pointer aliasing and constant propagation.
  Master’s thesis, SUNY at New Paltz, 1999.

                                                      Trinity College Dublin   43
Introduction to phc
                            Current state of phc    Simple Optimizations
        Next for phc - Analysis and Optimization    Advanced Optimizations
                          Experiences with PHP


Algorithm

     Abstract Execution / Interpretation

     Points-to analysis
         *-sensitive

     Constant-propagation
         Precision
         Array-indices/field names
         Implicit conversions

     Type-inference
         Virtual calls
         Function annotations

                                                    Trinity College Dublin   43
Introduction to phc
                            Current state of phc    Simple Optimizations
        Next for phc - Analysis and Optimization    Advanced Optimizations
                          Experiences with PHP


Complex cases



     Hashtables
     Implicit conversions
     Variable-variables
     $GLOBALS
     Static includes
     $SESSION
     Compiler temporaries




                                                    Trinity College Dublin   44
Introduction to phc
                            Current state of phc    Simple Optimizations
        Next for phc - Analysis and Optimization    Advanced Optimizations
                          Experiences with PHP


Interesting thoughts


     Strip off first loop iteration




                                                    Trinity College Dublin   45
Introduction to phc
                            Current state of phc    Simple Optimizations
        Next for phc - Analysis and Optimization    Advanced Optimizations
                          Experiences with PHP


Interesting thoughts


     Strip off first loop iteration
     JITs or Gal/Franz Tracing?




                                                    Trinity College Dublin   45
Introduction to phc
                             Current state of phc    Simple Optimizations
         Next for phc - Analysis and Optimization    Advanced Optimizations
                           Experiences with PHP


Interesting thoughts


      Strip off first loop iteration
      JITs or Gal/Franz Tracing?
      Use string transducer analysis

  Sound and Precise Analysis of Web Applications
  for Injection Vulnerabilities
  Gary Wassermann, Zhendong Su, PLDI’07.

  Static approximation of dynamically generated Web pages
  Yasuhiko Minamide, WWW 2005


                                                     Trinity College Dublin   45
Introduction to phc
                              Current state of phc
          Next for phc - Analysis and Optimization
                            Experiences with PHP


Outline

      Introduction to phc
  1


      Current state of phc
  2
        Challenges to compilation?
        phc solution: use the C API
        Speedup

      Next for phc - Analysis and Optimization
  3
        Simple Optimizations
        Advanced Optimizations

      Experiences with PHP
  4




                                                      Trinity College Dublin   46
Introduction to phc
                            Current state of phc
        Next for phc - Analysis and Optimization
                          Experiences with PHP


Opinions and conjecture



  Opinions and conjecture




                                                    Trinity College Dublin   47
Introduction to phc
                            Current state of phc
        Next for phc - Analysis and Optimization
                          Experiences with PHP


Opinions and conjecture



  Opinions and conjecture
     Language Problems




                                                    Trinity College Dublin   47
Introduction to phc
                            Current state of phc
        Next for phc - Analysis and Optimization
                          Experiences with PHP


Opinions and conjecture



  Opinions and conjecture
     Language Problems

     Implementation problems




                                                    Trinity College Dublin   47
Introduction to phc
                            Current state of phc
        Next for phc - Analysis and Optimization
                          Experiences with PHP


Opinions and conjecture



  Opinions and conjecture
     Language Problems

     Implementation problems

     Community Problems




                                                    Trinity College Dublin   47
Introduction to phc
                              Current state of phc
          Next for phc - Analysis and Optimization
                            Experiences with PHP


Opinions and conjecture



  Fixes
      Remove coupling between libraries and interpreter
      Better community interactions:
           Pre-commit reviews
           Mailing list moderation
           Per-area maintainers
      Love of the language leads to more tools




                                                      Trinity College Dublin   48
Introduction to phc
                           Current state of phc
       Next for phc - Analysis and Optimization
                         Experiences with PHP


Summary


    Re-use existing run-time for language
    Better yet: standardize libraries (and language?), including
    FFI

    Analysis needs to be precise, and whole-program
    Pessimistic assumptions spread

    Language, implementation and community need to be
    fixed
        All related?



                                                   Trinity College Dublin   49
Introduction to phc
                             Current state of phc
         Next for phc - Analysis and Optimization
                           Experiences with PHP


Thanks


  phc needs contributors
    contribute:
    http://phpcompiler.org/contribute.html
    mailing list: phc-general@phpcompiler.org

    slides: http://www.cs.tcd.ie/~pbiggar/
    contact: paul.biggar@gmail.com




                                                     Trinity College Dublin   50
Introduction to phc
                            Current state of phc
        Next for phc - Analysis and Optimization
                          Experiences with PHP


Complex cases



     Hashtables
     Implicit conversions
     Variable-variables
     $GLOBALS
     Static includes
     $SESSION
     Compiler temporaries




                                                    Trinity College Dublin   51

Más contenido relacionado

La actualidad más candente

Python Intro For Managers
Python Intro For ManagersPython Intro For Managers
Python Intro For Managers
Atul Shridhar
 

La actualidad más candente (17)

How to reverse engineer Android applications
How to reverse engineer Android applicationsHow to reverse engineer Android applications
How to reverse engineer Android applications
 
Machine Learning on Code - SF meetup
Machine Learning on Code - SF meetupMachine Learning on Code - SF meetup
Machine Learning on Code - SF meetup
 
Guidelines php 8 gig
Guidelines php 8 gigGuidelines php 8 gig
Guidelines php 8 gig
 
Metaprogramming in Ruby
Metaprogramming in RubyMetaprogramming in Ruby
Metaprogramming in Ruby
 
Recording Finer-Grained Software Evolution with IDE: An Annotation-Based Appr...
Recording Finer-Grained Software Evolution with IDE: An Annotation-Based Appr...Recording Finer-Grained Software Evolution with IDE: An Annotation-Based Appr...
Recording Finer-Grained Software Evolution with IDE: An Annotation-Based Appr...
 
Pimp My Java LavaJUG
Pimp My Java LavaJUGPimp My Java LavaJUG
Pimp My Java LavaJUG
 
Python Intro For Managers
Python Intro For ManagersPython Intro For Managers
Python Intro For Managers
 
C tutorial
C tutorialC tutorial
C tutorial
 
Generating Assertion Code from OCL: A Transformational Approach Based on Simi...
Generating Assertion Code from OCL: A Transformational Approach Based on Simi...Generating Assertion Code from OCL: A Transformational Approach Based on Simi...
Generating Assertion Code from OCL: A Transformational Approach Based on Simi...
 
Aumentando a eficiência do Web Container usando chamadas Assíncronas
Aumentando a eficiência do Web Container usando chamadas Assíncronas Aumentando a eficiência do Web Container usando chamadas Assíncronas
Aumentando a eficiência do Web Container usando chamadas Assíncronas
 
Java Library Evolution Puzzlers
Java Library Evolution PuzzlersJava Library Evolution Puzzlers
Java Library Evolution Puzzlers
 
Sentence-to-Code Traceability Recovery with Domain Ontologies
Sentence-to-Code Traceability Recovery with Domain OntologiesSentence-to-Code Traceability Recovery with Domain Ontologies
Sentence-to-Code Traceability Recovery with Domain Ontologies
 
Pgq
PgqPgq
Pgq
 
Python Bindings Overview
Python Bindings OverviewPython Bindings Overview
Python Bindings Overview
 
Php Vs Phyton
Php Vs PhytonPhp Vs Phyton
Php Vs Phyton
 
Intermediate python
Intermediate pythonIntermediate python
Intermediate python
 
Extending and scripting PDT
Extending and scripting PDTExtending and scripting PDT
Extending and scripting PDT
 

Destacado (6)

Smartphoneseminar orso_sakamoto
Smartphoneseminar orso_sakamotoSmartphoneseminar orso_sakamoto
Smartphoneseminar orso_sakamoto
 
La Dolce Vita Rubyista
La Dolce Vita RubyistaLa Dolce Vita Rubyista
La Dolce Vita Rubyista
 
Briele
BrieleBriele
Briele
 
Lifecanbehard
LifecanbehardLifecanbehard
Lifecanbehard
 
Outside-In Development with Cucumber
Outside-In Development with CucumberOutside-In Development with Cucumber
Outside-In Development with Cucumber
 
角度
角度角度
角度
 

Similar a Compiling And Optimizing Scripting Languages

Search Lucene
Search LuceneSearch Lucene
Search Lucene
Jeremy Coates
 
Алексей Петров "PHP at Scale: Knowing enough to be dangerous!"
Алексей Петров "PHP at Scale: Knowing enough to be dangerous!"Алексей Петров "PHP at Scale: Knowing enough to be dangerous!"
Алексей Петров "PHP at Scale: Knowing enough to be dangerous!"
Fwdays
 
Php through the eyes of a hoster phpbnl11
Php through the eyes of a hoster phpbnl11Php through the eyes of a hoster phpbnl11
Php through the eyes of a hoster phpbnl11
Combell NV
 
Ein Stall voller Trüffelschweine - (PHP-)Profiling-Tools im Überblick
Ein Stall voller Trüffelschweine - (PHP-)Profiling-Tools im ÜberblickEin Stall voller Trüffelschweine - (PHP-)Profiling-Tools im Überblick
Ein Stall voller Trüffelschweine - (PHP-)Profiling-Tools im Überblick
renebruns
 

Similar a Compiling And Optimizing Scripting Languages (20)

Prepare for PHP Test Fest 2009
Prepare for PHP Test Fest 2009Prepare for PHP Test Fest 2009
Prepare for PHP Test Fest 2009
 
Automatic PHP 7 Compatibility Checking Using php7cc (and PHPCompatibility)
Automatic PHP 7 Compatibility Checking Using php7cc (and PHPCompatibility)Automatic PHP 7 Compatibility Checking Using php7cc (and PHPCompatibility)
Automatic PHP 7 Compatibility Checking Using php7cc (and PHPCompatibility)
 
Statyczna analiza kodu PHP
Statyczna analiza kodu PHPStatyczna analiza kodu PHP
Statyczna analiza kodu PHP
 
Last 2 Months in PHP - July & August 2016
Last 2 Months in PHP - July & August 2016Last 2 Months in PHP - July & August 2016
Last 2 Months in PHP - July & August 2016
 
Code metrics in PHP
Code metrics in PHPCode metrics in PHP
Code metrics in PHP
 
Search Lucene
Search LuceneSearch Lucene
Search Lucene
 
Kyrylo Cherneha "C++ & Python Interaction in Automotive Industry"
Kyrylo Cherneha "C++ & Python Interaction in Automotive Industry"Kyrylo Cherneha "C++ & Python Interaction in Automotive Industry"
Kyrylo Cherneha "C++ & Python Interaction in Automotive Industry"
 
Алексей Петров "PHP at Scale: Knowing enough to be dangerous!"
Алексей Петров "PHP at Scale: Knowing enough to be dangerous!"Алексей Петров "PHP at Scale: Knowing enough to be dangerous!"
Алексей Петров "PHP at Scale: Knowing enough to be dangerous!"
 
Northeast PHP - High Performance PHP
Northeast PHP - High Performance PHPNortheast PHP - High Performance PHP
Northeast PHP - High Performance PHP
 
Phalcon 2 - PHP Brazil Conference
Phalcon 2 - PHP Brazil ConferencePhalcon 2 - PHP Brazil Conference
Phalcon 2 - PHP Brazil Conference
 
Php through the eyes of a hoster phpbnl11
Php through the eyes of a hoster phpbnl11Php through the eyes of a hoster phpbnl11
Php through the eyes of a hoster phpbnl11
 
Python Loops Tutorial | Python For Loop | While Loop Python | Python Training...
Python Loops Tutorial | Python For Loop | While Loop Python | Python Training...Python Loops Tutorial | Python For Loop | While Loop Python | Python Training...
Python Loops Tutorial | Python For Loop | While Loop Python | Python Training...
 
Php through the eyes of a hoster confoo
Php through the eyes of a hoster confooPhp through the eyes of a hoster confoo
Php through the eyes of a hoster confoo
 
PHP 5.4 - Begin your love affair with traits
PHP 5.4 - Begin your love affair with traitsPHP 5.4 - Begin your love affair with traits
PHP 5.4 - Begin your love affair with traits
 
Ein Stall voller Trüffelschweine - (PHP-)Profiling-Tools im Überblick
Ein Stall voller Trüffelschweine - (PHP-)Profiling-Tools im ÜberblickEin Stall voller Trüffelschweine - (PHP-)Profiling-Tools im Überblick
Ein Stall voller Trüffelschweine - (PHP-)Profiling-Tools im Überblick
 
PHP Ecosystem and Best Practices
PHP Ecosystem and Best PracticesPHP Ecosystem and Best Practices
PHP Ecosystem and Best Practices
 
Living With Legacy Code
Living With Legacy CodeLiving With Legacy Code
Living With Legacy Code
 
The why and how of moving to php 8
The why and how of moving to php 8The why and how of moving to php 8
The why and how of moving to php 8
 
Test Fest 2009
Test Fest 2009Test Fest 2009
Test Fest 2009
 
Cli the other sapi pbc11
Cli the other sapi pbc11Cli the other sapi pbc11
Cli the other sapi pbc11
 

Más de LittleBIGRuby

Wii Ruby All Work And No Play Just Wont Do
Wii Ruby All Work And No Play Just Wont DoWii Ruby All Work And No Play Just Wont Do
Wii Ruby All Work And No Play Just Wont Do
LittleBIGRuby
 
The Building Blocks Of Modularity
The Building Blocks Of ModularityThe Building Blocks Of Modularity
The Building Blocks Of Modularity
LittleBIGRuby
 
Outside In Development With Cucumber
Outside In Development With CucumberOutside In Development With Cucumber
Outside In Development With Cucumber
LittleBIGRuby
 
Practical Puppet Systems Building Systems 1
Practical Puppet Systems Building Systems 1Practical Puppet Systems Building Systems 1
Practical Puppet Systems Building Systems 1
LittleBIGRuby
 

Más de LittleBIGRuby (8)

Wii Ruby All Work And No Play Just Wont Do
Wii Ruby All Work And No Play Just Wont DoWii Ruby All Work And No Play Just Wont Do
Wii Ruby All Work And No Play Just Wont Do
 
The Building Blocks Of Modularity
The Building Blocks Of ModularityThe Building Blocks Of Modularity
The Building Blocks Of Modularity
 
Rack Middleware
Rack MiddlewareRack Middleware
Rack Middleware
 
Outside In Development With Cucumber
Outside In Development With CucumberOutside In Development With Cucumber
Outside In Development With Cucumber
 
Practical Puppet Systems Building Systems 1
Practical Puppet Systems Building Systems 1Practical Puppet Systems Building Systems 1
Practical Puppet Systems Building Systems 1
 
Little Big Ruby
Little Big RubyLittle Big Ruby
Little Big Ruby
 
Ffi
FfiFfi
Ffi
 
Sequel
SequelSequel
Sequel
 

Compiling And Optimizing Scripting Languages

  • 1. Introduction to phc Current state of phc Next for phc - Analysis and Optimization Experiences with PHP Compiling and Optimizing Scripting Languages Paul Biggar and David Gregg Department of Computer Science and Statistics Trinity College Dublin Google, 18th March, 2009 Trinity College Dublin 1
  • 2. Introduction to phc Current state of phc Next for phc - Analysis and Optimization Experiences with PHP Motivation User needs web page in 0.5 seconds Execution time DB access Network latency Browser rendering Easier maintainance What if execution was: 2x as fast? 10x as fast? Trinity College Dublin 2
  • 3. Introduction to phc Current state of phc Next for phc - Analysis and Optimization Experiences with PHP Outline Introduction to phc 1 Current state of phc 2 Challenges to compilation? phc solution: use the C API Speedup Next for phc - Analysis and Optimization 3 Simple Optimizations Advanced Optimizations Experiences with PHP 4 Trinity College Dublin 3
  • 4. Introduction to phc Current state of phc Next for phc - Analysis and Optimization Experiences with PHP Outline Introduction to phc 1 Current state of phc 2 Challenges to compilation? phc solution: use the C API Speedup Next for phc - Analysis and Optimization 3 Simple Optimizations Advanced Optimizations Experiences with PHP 4 Trinity College Dublin 4
  • 5. Introduction to phc Current state of phc Next for phc - Analysis and Optimization Experiences with PHP phc http://phpcompiler.org Ahead-of-time compiler for PHP Edsko de Vries, John Gilbert, Paul Biggar BSD license Latest release: 0.2.0.3 - compiles non-OO svn trunk: compiles most OO Trinity College Dublin 5
  • 6. Introduction to phc Current state of phc Next for phc - Analysis and Optimization Experiences with PHP Structure of phc Trinity College Dublin 6
  • 7. Introduction to phc Current state of phc Next for phc - Analysis and Optimization Experiences with PHP PHP <?php echo quot;helloquot;, quot;world!quot;; ?> Trinity College Dublin 7
  • 8. Introduction to phc Current state of phc Next for phc - Analysis and Optimization Experiences with PHP AST PHP_script List<Statement> Eval_expr (3) Nop (5) Method_invocation (3) NULL METHOD_NAME (3) List<Actual_parameter> (Target) echo Actual_parameter (3) Actual_parameter (3) STRING (3) STRING (3) hello world! Trinity College Dublin 8
  • 9. Introduction to phc Current state of phc Next for phc - Analysis and Optimization Experiences with PHP HIR <?php $x = $a + $b + $c + $d; ?> <?php $TLE0 = ($a + $b); $TLE1 = ($TLE0 + $c); $x = ($TLE1 + $d); ?> Trinity College Dublin 9
  • 10. Introduction to phc Current state of phc Next for phc - Analysis and Optimization Experiences with PHP MIR <?php while ($cond) echo quot;helloquot;, quot;world!quot;; ?> <?php L7: $TLE0 = !$cond; if ($TLE0) goto L3 else goto L6; L6: print(’hello’); print(’world!’); goto L7; L3: ?> Trinity College Dublin 10
  • 11. Introduction to phc Current state of phc Next for phc - Analysis and Optimization Experiences with PHP Plugins http://phpcompiler.org/doc/latest/devmanual.html Trinity College Dublin 11
  • 12. Introduction to phc Current state of phc Next for phc - Analysis and Optimization Experiences with PHP XML <?xml version=quot;1.0quot;?> <AST:PHP_script xmlns:AST=quot;http://www.phpcompiler.org/phc-1.1quot;> <AST:Statement_list> <AST:Eval_expr> <AST:Method_invocation> <AST:Target xsi:nil=quot;truequot; /> <AST:METHOD_NAME> <value>echo</value> </AST:METHOD_NAME> <AST:Actual_parameter_list> <AST:Actual_parameter> <bool><!-- is_ref -->false</bool> <AST:STRING> <value>hello</value> </AST:STRING> </AST:Actual_parameter> <AST:Actual_parameter> <bool><!-- is_ref -->false</bool> <AST:STRING> <value>world!</value> </AST:STRING> </AST:Actual_parameter> </AST:Actual_parameter_list> </AST:Method_invocation> </AST:Eval_expr> <AST:Nop> </AST:Nop> </AST:Statement_list> </AST:PHP_script> Trinity College Dublin 12
  • 13. Introduction to phc Challenges to compilation? Current state of phc phc solution: use the C API Next for phc - Analysis and Optimization Speedup Experiences with PHP Outline Introduction to phc 1 Current state of phc 2 Challenges to compilation? phc solution: use the C API Speedup Next for phc - Analysis and Optimization 3 Simple Optimizations Advanced Optimizations Experiences with PHP 4 Trinity College Dublin 13
  • 14. Introduction to phc Challenges to compilation? Current state of phc phc solution: use the C API Next for phc - Analysis and Optimization Speedup Experiences with PHP SAC 2009 A Practical Solution for Scripting Language Compilers Paul Biggar, Edsko de Vries and David Gregg Department of Computer Science and Statistics Trinity College Dublin ACM Symposium on Applied Computing - PL track 12th March, 2009 Trinity College Dublin 14
  • 15. Introduction to phc Challenges to compilation? Current state of phc phc solution: use the C API Next for phc - Analysis and Optimization Speedup Experiences with PHP Sneak peak Problem: Scripting languages present “unique” problems (in practice) Solution: Re-use as much of the Canonical Reference Implementation as possible. Trinity College Dublin 15
  • 16. Introduction to phc Challenges to compilation? Current state of phc phc solution: use the C API Next for phc - Analysis and Optimization Speedup Experiences with PHP Outline Introduction to phc 1 Current state of phc 2 Challenges to compilation? phc solution: use the C API Speedup Next for phc - Analysis and Optimization 3 Simple Optimizations Advanced Optimizations Experiences with PHP 4 Trinity College Dublin 16
  • 17. Introduction to phc Challenges to compilation? Current state of phc phc solution: use the C API Next for phc - Analysis and Optimization Speedup Experiences with PHP Undefined The PHP group claim that they have the final say in the specification of PHP. This group’s specification is an implementation, and there is no prose specification or agreed validation suite. There are alternate implementations [...] that claim to be compatible (they don’t say what this means) with some version of PHP. D. M. Jones. Forms of language specification: Examples from commonly used computer languages. ISO/IEC JTC1/SC22/OWG/N0121, February 2008. Trinity College Dublin 17
  • 18. Introduction to phc Challenges to compilation? Current state of phc phc solution: use the C API Next for phc - Analysis and Optimization Speedup Experiences with PHP Batteries included Jeff Atwood, Coding Horror, May 20th, 2008 http://www.codinghorror.com/blog/archives/001119.html Trinity College Dublin 18
  • 19. Introduction to phc Challenges to compilation? Current state of phc phc solution: use the C API Next for phc - Analysis and Optimization Speedup Experiences with PHP Change between releases <?php var_dump (0x9fa0ff0b); ?> PHP 5.2.1 (32-bit) int(2147483647) PHP 5.2.3 (32-bit) float(2678128395) Trinity College Dublin 19
  • 20. Introduction to phc Challenges to compilation? Current state of phc phc solution: use the C API Next for phc - Analysis and Optimization Speedup Experiences with PHP Run-time code generation <?php eval ($argv[1]); ?> <?php include (quot;mylib.phpquot;); ... include (quot;plugin.phpquot;); ... ?> Trinity College Dublin 20
  • 21. Introduction to phc Challenges to compilation? Current state of phc phc solution: use the C API Next for phc - Analysis and Optimization Speedup Experiences with PHP Outline Introduction to phc 1 Current state of phc 2 Challenges to compilation? phc solution: use the C API Speedup Next for phc - Analysis and Optimization 3 Simple Optimizations Advanced Optimizations Experiences with PHP 4 Trinity College Dublin 21
  • 22. Introduction to phc Challenges to compilation? Current state of phc phc solution: use the C API Next for phc - Analysis and Optimization Speedup Experiences with PHP Use C API Trinity College Dublin 22
  • 23. Introduction to phc Challenges to compilation? Current state of phc phc solution: use the C API Next for phc - Analysis and Optimization Speedup Experiences with PHP More detail PHP zval Python PyObject Ruby VALUE Lua TValue H. Muhammad and R. Ierusalimschy. C APIs in extension and extensible languages. Journal of Universal Computer Science, 13(6):839–853, 2007. Trinity College Dublin 23
  • 24. Introduction to phc Challenges to compilation? Current state of phc phc solution: use the C API Next for phc - Analysis and Optimization Speedup Experiences with PHP Simple listings: $i = 0 // $i = 0; { zval* p_i; php_hash_find (LOCAL_ST, quot;iquot;, 5863374, p_i); php_destruct (p_i); php_allocate (p_i); ZVAL_LONG (*p_i, 0); } Trinity College Dublin 24
  • 25. Introduction to phc Challenges to compilation? Current state of phc phc solution: use the C API Next for phc - Analysis and Optimization Speedup Experiences with PHP Example: $i = 0 // $i = 0; { if (local_i == NULL) { local_i = EG (uninitialized_zval_ptr); local_i->refcount++; } zval **p_lhs = &local_i; zval *value; if ((*p_lhs)->is_ref) { // Always overwrite the current value value = *p_lhs; zval_dtor (value); } else { ALLOC_INIT_ZVAL (value); zval_ptr_dtor (p_lhs); *p_lhs = value; } ZVAL_LONG (value, 0); } Trinity College Dublin 25
  • 26. Introduction to phc Challenges to compilation? Current state of phc phc solution: use the C API Next for phc - Analysis and Optimization Speedup Experiences with PHP Example: $i = $j // $i = $j; { if (local_i == NULL) { local_i = EG (uninitialized_zval_ptr); local_i->refcount++; } zval **p_lhs = &local_i; zval *rhs; if (local_j == NULL) rhs = EG (uninitialized_zval_ptr); else rhs = local_j; if (*p_lhs != rhs) { if ((*p_lhs)->is_ref) { // First, call the destructor to remove any data structures // associated with lhs that will now be overwritten zval_dtor (*p_lhs); // Overwrite LHS (*p_lhs)->value = rhs->value; (*p_lhs)->type = rhs->type; zval_copy_ctor (*p_lhs); } else { zval_ptr_dtor (p_lhs); if (rhs->is_ref) { // Take a copy of RHS for LHS *p_lhs = zvp_clone_ex (rhs); } else { // Share a copy rhs->refcount++; *p_lhs = rhs; } } } Trinity College Dublin 26 }
  • 27. Introduction to phc Challenges to compilation? Current state of phc phc solution: use the C API Next for phc - Analysis and Optimization Speedup Experiences with PHP Example: printf ($f) static zend_fcall_info printf_fci; static zend_fcall_info_cache printf_fcic = { 0, NULL, NULL, NULL }; // printf($f); { if (!printf_fcic->initialized) { zval fn; INIT_PZVAL (&fn); ZVAL_STRING (&fn, quot;printfquot;, 0); int result = zend_fcall_info_init (&fn, &printf_fci, &printf_fcic TSRMLS_CC); if (result != SUCCESS) { phc_setup_error (1, quot;listings_source.phpquot;, 8, NULL TSRMLS_CC); php_error_docref (NULL TSRMLS_CC, E_ERROR, quot;Call to undefined function %s()quot;, function_name); } } zend_function *signature = printf_fcic.function_handler; zend_arg_info *arg_info = signature->common.arg_info; // optional int by_ref[1]; int abr_index = 0; // TODO: find names to replace index if (arg_info) { by_ref[abr_index] = arg_info->pass_by_reference; arg_info++; } else by_ref[abr_index] = signature->common.pass_rest_by_reference; abr_index++; // Setup array of arguments // TODO: i think arrays of size 0 is an error int destruct[1]; zval *args[1]; zval **args_ind[1]; int af_index = 0; destruct[af_index] = 0; if (by_ref[af_index]) { if (local_f == NULL) { local_f = EG (uninitialized_zval_ptr); local_f->refcount++; } zval **p_arg = &local_f; // We don’t need to restore ->is_ref afterwards, // because the called function will reduce the // refcount of arg on return, and will reset is_ref to // 0 when refcount drops to 1. If the refcount does // not drop to 1 when the function returns, but we did // set is_ref to 1 here, that means that is_ref must // already have been 1 to start with (since if it had // not, that means that the variable would have been // in a copy-on-write set, and would have been // seperated above). (*p_arg)->is_ref = 1; args_ind[af_index] = p_arg; assert (!in_copy_on_write (*args_ind[af_index])); args[af_index] = *args_ind[af_index]; } else { zval *arg; if (local_f == NULL) arg = EG (uninitialized_zval_ptr); else arg = local_f; args[af_index] = fetch_var_arg (arg, &destruct[af_index]); if (arg->is_ref) { // We dont separate since we don’t own one of ARG’s references. arg = zvp_clone_ex (arg); destruct[af_index] = 1; // It seems we get incorrect refcounts without this. // TODO This decreases the refcount to zero, which seems wrong, // but gives the right answer. We should look at how zend does // this. arg->refcount--; } args[af_index] = arg; args_ind[af_index] = &args[af_index]; } af_index++; phc_setup_error (1, quot;listings_source.phpquot;, 8, NULL TSRMLS_CC); // save existing parameters, in case of recursion int param_count_save = printf_fci.param_count; zval ***params_save = printf_fci.params; zval **retval_save = printf_fci.retval_ptr_ptr; zval *rhs = NULL; // set up params printf_fci.params = args_ind; printf_fci.param_count = 1; printf_fci.retval_ptr_ptr = &rhs; // call the function int success = zend_call_function (&printf_fci, &printf_fcic TSRMLS_CC); assert (success == SUCCESS); // restore params printf_fci.params = params_save; printf_fci.param_count = param_count_save; printf_fci.retval_ptr_ptr = retval_save; // unset the errors phc_setup_error (0, NULL, 0, NULL TSRMLS_CC); int i; for (i = 0; i < 1; i++) { if (destruct[i]) { assert (destruct[i]); zval_ptr_dtor (args_ind[i]); } } // When the Zend engine returns by reference, it allocates a zval into // retval_ptr_ptr. To return by reference, the callee writes into the // retval_ptr_ptr, freeing the allocated value as it does. (Note, it may // not actually return anything). So the zval returned - whether we return // it, or it is the allocated zval - has a refcount of 1. // The caller is responsible for cleaning that up (note, this is unaffected // by whether it is added to some COW set). // For reasons unknown, the Zend API resets the refcount and is_ref fields // of the return value after the function returns (unless the callee is // interpreted). If the function is supposed to return by reference, this // loses the refcount. This only happens when non-interpreted code is // called. We work around it, when compiled code is called, by saving the // refcount into SAVED_REFCOUNT, in the return statement. The downside is // that we may create an error if our code is called by a callback, and // returns by reference, and the callback returns by reference. At least // this is an obscure case. if (signature->common.return_reference && signature->type != ZEND_USER_FUNCTION) { assert (rhs != EG (uninitialized_zval_ptr)); rhs->is_ref = 1; if (saved_refcount != 0) { rhs->refcount = saved_refcount; } rhs->refcount++; } saved_refcount = 0; // for ’obscure cases’ zval_ptr_dtor (&rhs); Trinity College Dublin 27 if (signature->common.return_reference && signature->type != ZEND_USER_FUNCTION) zval_ptr_dtor (&rhs); }
  • 28. Introduction to phc Challenges to compilation? Current state of phc phc solution: use the C API Next for phc - Analysis and Optimization Speedup Experiences with PHP Applicability Everything Perl PHP Ruby Tcl – I think Trinity College Dublin 28
  • 29. Introduction to phc Challenges to compilation? Current state of phc phc solution: use the C API Next for phc - Analysis and Optimization Speedup Experiences with PHP Applicability Everything Perl PHP Ruby Tcl – I think Except specification Lua Python Trinity College Dublin 28
  • 30. Introduction to phc Challenges to compilation? Current state of phc phc solution: use the C API Next for phc - Analysis and Optimization Speedup Experiences with PHP Applicability Everything Perl PHP Ruby Tcl – I think Except specification Lua Python Not at all Javascript Trinity College Dublin 28
  • 31. Introduction to phc Challenges to compilation? Current state of phc phc solution: use the C API Next for phc - Analysis and Optimization Speedup Experiences with PHP Outline Introduction to phc 1 Current state of phc 2 Challenges to compilation? phc solution: use the C API Speedup Next for phc - Analysis and Optimization 3 Simple Optimizations Advanced Optimizations Experiences with PHP 4 Trinity College Dublin 29
  • 32. Introduction to phc Challenges to compilation? Current state of phc phc solution: use the C API Next for phc - Analysis and Optimization Speedup Experiences with PHP Original Speed-up 0.1x (10 times slower than the PHP interpreter) Trinity College Dublin 30
  • 33. Introduction to phc Challenges to compilation? Current state of phc phc solution: use the C API Next for phc - Analysis and Optimization Speedup Experiences with PHP The problem with copies <?php for ($i = 0; $i < $n; $i++) $str = $str . quot;helloquot;; ?> <?php for ($i = 0; $i < $n; $i++) { $T = $str . quot;helloquot;; $str = $T; } ?> Trinity College Dublin 31
  • 34. Introduction to phc Challenges to compilation? Current state of phc phc solution: use the C API Next for phc - Analysis and Optimization Speedup Experiences with PHP Optimization Constant folding <?php <?php ... ... $T = quot;5quot; + true; $T = 6; ... ... ?> ?> Trinity College Dublin 32
  • 35. Introduction to phc Challenges to compilation? Current state of phc phc solution: use the C API Next for phc - Analysis and Optimization Speedup Experiences with PHP Optimization Constant folding Constant pooling <?php $sum = 0; for ($i = 0; $i < 10; $i=$i+1) { $sum .= quot;helloquot;; } ?> Trinity College Dublin 32
  • 36. Introduction to phc Challenges to compilation? Current state of phc phc solution: use the C API Next for phc - Analysis and Optimization Speedup Experiences with PHP Optimization Constant folding Constant pooling Function caching // printf ($f); static php_fcall_info printf_info; { php_fcall_info_init (quot;printfquot;, &printf_info); php_hash_find ( LOCAL_ST, quot;fquot;, 5863275, &printf_info.params); php_call_function (&printf_info); } Trinity College Dublin 32
  • 37. Introduction to phc Challenges to compilation? Current state of phc phc solution: use the C API Next for phc - Analysis and Optimization Speedup Experiences with PHP Optimization Constant folding Constant pooling Function caching Pre-hashing // $i = 0; { zval* p_i; php_hash_find (LOCAL_ST, quot;iquot;, 5863374, p_i); php_destruct (p_i); php_allocate (p_i); ZVAL_LONG (*p_i, 0); } Trinity College Dublin 32
  • 38. Introduction to phc Challenges to compilation? Current state of phc phc solution: use the C API Next for phc - Analysis and Optimization Speedup Experiences with PHP Optimization Constant folding Constant pooling Function caching Pre-hashing Symbol-table removal // $i = 0; { php_destruct (local_i); php_allocate (local_i); ZVAL_LONG (*local_i, 0); } Trinity College Dublin 32
  • 39. Introduction to phc Challenges to compilation? Current state of phc phc solution: use the C API Next for phc - Analysis and Optimization Speedup Experiences with PHP Current speed-up 3 Speedup of compiled benchmark 2.5 2 1.5 1 0.5 0 ackermann ary ary2 ary3 fibo hash1 hash2 heapsort mandel mandel2 matrix nestedloop sieve simple simplecall simpleucall simpleudcall strcat mean Trinity College Dublin 33
  • 40. Introduction to phc Current state of phc Simple Optimizations Next for phc - Analysis and Optimization Advanced Optimizations Experiences with PHP Outline Introduction to phc 1 Current state of phc 2 Challenges to compilation? phc solution: use the C API Speedup Next for phc - Analysis and Optimization 3 Simple Optimizations Advanced Optimizations Experiences with PHP 4 Trinity College Dublin 34
  • 41. Introduction to phc Current state of phc Simple Optimizations Next for phc - Analysis and Optimization Advanced Optimizations Experiences with PHP Outline Introduction to phc 1 Current state of phc 2 Challenges to compilation? phc solution: use the C API Speedup Next for phc - Analysis and Optimization 3 Simple Optimizations Advanced Optimizations Experiences with PHP 4 Trinity College Dublin 35
  • 42. Introduction to phc Current state of phc Simple Optimizations Next for phc - Analysis and Optimization Advanced Optimizations Experiences with PHP Intra-procedural optimizations Dead-code elimination Sparse-conditional constant propagation Trinity College Dublin 36
  • 43. Introduction to phc Current state of phc Simple Optimizations Next for phc - Analysis and Optimization Advanced Optimizations Experiences with PHP Type-inference <?php function a ($x, $y) { $str = $x . $y; ... return $str; } ?> Trinity College Dublin 37
  • 44. Introduction to phc Current state of phc Simple Optimizations Next for phc - Analysis and Optimization Advanced Optimizations Experiences with PHP User-space handlers __toString __get __set __isset __unset __sleep __wake __call __callStatic ... Trinity College Dublin 38
  • 45. Introduction to phc Current state of phc Simple Optimizations Next for phc - Analysis and Optimization Advanced Optimizations Experiences with PHP C API handlers read_property read_dimension get set cast_object has_property unset_property ... Trinity College Dublin 39
  • 46. Introduction to phc Current state of phc Simple Optimizations Next for phc - Analysis and Optimization Advanced Optimizations Experiences with PHP Unknown types propagate local symbol table global symbol table return values reference parameters callee parameters Trinity College Dublin 40
  • 47. Introduction to phc Current state of phc Simple Optimizations Next for phc - Analysis and Optimization Advanced Optimizations Experiences with PHP Outline Introduction to phc 1 Current state of phc 2 Challenges to compilation? phc solution: use the C API Speedup Next for phc - Analysis and Optimization 3 Simple Optimizations Advanced Optimizations Experiences with PHP 4 Trinity College Dublin 41
  • 48. Introduction to phc Current state of phc Simple Optimizations Next for phc - Analysis and Optimization Advanced Optimizations Experiences with PHP Analysis design Must model types precisely (Possibly unnamed) fields, arrays, variables and method calls Trinity College Dublin 42
  • 49. Introduction to phc Current state of phc Simple Optimizations Next for phc - Analysis and Optimization Advanced Optimizations Experiences with PHP Analysis design Must model types precisely (Possibly unnamed) fields, arrays, variables and method calls Uses and definitions incomplete Can’t use def-use chains Can’t use SSA Trinity College Dublin 42
  • 50. Introduction to phc Current state of phc Simple Optimizations Next for phc - Analysis and Optimization Advanced Optimizations Experiences with PHP Analysis design Must model types precisely (Possibly unnamed) fields, arrays, variables and method calls Uses and definitions incomplete Can’t use def-use chains Can’t use SSA Imprecise callgraph Trinity College Dublin 42
  • 51. Introduction to phc Current state of phc Simple Optimizations Next for phc - Analysis and Optimization Advanced Optimizations Experiences with PHP Algorithm Abstract Execution / Interpretation Trinity College Dublin 43
  • 52. Introduction to phc Current state of phc Simple Optimizations Next for phc - Analysis and Optimization Advanced Optimizations Experiences with PHP Algorithm Abstract Execution / Interpretation Points-to analysis *-sensitive Trinity College Dublin 43
  • 53. Introduction to phc Current state of phc Simple Optimizations Next for phc - Analysis and Optimization Advanced Optimizations Experiences with PHP Algorithm Abstract Execution / Interpretation Points-to analysis *-sensitive Constant-propagation Precision Array-indices/field names Implicit conversions A. Pioli. Conditional pointer aliasing and constant propagation. Master’s thesis, SUNY at New Paltz, 1999. Trinity College Dublin 43
  • 54. Introduction to phc Current state of phc Simple Optimizations Next for phc - Analysis and Optimization Advanced Optimizations Experiences with PHP Algorithm Abstract Execution / Interpretation Points-to analysis *-sensitive Constant-propagation Precision Array-indices/field names Implicit conversions Type-inference Virtual calls Function annotations Trinity College Dublin 43
  • 55. Introduction to phc Current state of phc Simple Optimizations Next for phc - Analysis and Optimization Advanced Optimizations Experiences with PHP Complex cases Hashtables Implicit conversions Variable-variables $GLOBALS Static includes $SESSION Compiler temporaries Trinity College Dublin 44
  • 56. Introduction to phc Current state of phc Simple Optimizations Next for phc - Analysis and Optimization Advanced Optimizations Experiences with PHP Interesting thoughts Strip off first loop iteration Trinity College Dublin 45
  • 57. Introduction to phc Current state of phc Simple Optimizations Next for phc - Analysis and Optimization Advanced Optimizations Experiences with PHP Interesting thoughts Strip off first loop iteration JITs or Gal/Franz Tracing? Trinity College Dublin 45
  • 58. Introduction to phc Current state of phc Simple Optimizations Next for phc - Analysis and Optimization Advanced Optimizations Experiences with PHP Interesting thoughts Strip off first loop iteration JITs or Gal/Franz Tracing? Use string transducer analysis Sound and Precise Analysis of Web Applications for Injection Vulnerabilities Gary Wassermann, Zhendong Su, PLDI’07. Static approximation of dynamically generated Web pages Yasuhiko Minamide, WWW 2005 Trinity College Dublin 45
  • 59. Introduction to phc Current state of phc Next for phc - Analysis and Optimization Experiences with PHP Outline Introduction to phc 1 Current state of phc 2 Challenges to compilation? phc solution: use the C API Speedup Next for phc - Analysis and Optimization 3 Simple Optimizations Advanced Optimizations Experiences with PHP 4 Trinity College Dublin 46
  • 60. Introduction to phc Current state of phc Next for phc - Analysis and Optimization Experiences with PHP Opinions and conjecture Opinions and conjecture Trinity College Dublin 47
  • 61. Introduction to phc Current state of phc Next for phc - Analysis and Optimization Experiences with PHP Opinions and conjecture Opinions and conjecture Language Problems Trinity College Dublin 47
  • 62. Introduction to phc Current state of phc Next for phc - Analysis and Optimization Experiences with PHP Opinions and conjecture Opinions and conjecture Language Problems Implementation problems Trinity College Dublin 47
  • 63. Introduction to phc Current state of phc Next for phc - Analysis and Optimization Experiences with PHP Opinions and conjecture Opinions and conjecture Language Problems Implementation problems Community Problems Trinity College Dublin 47
  • 64. Introduction to phc Current state of phc Next for phc - Analysis and Optimization Experiences with PHP Opinions and conjecture Fixes Remove coupling between libraries and interpreter Better community interactions: Pre-commit reviews Mailing list moderation Per-area maintainers Love of the language leads to more tools Trinity College Dublin 48
  • 65. Introduction to phc Current state of phc Next for phc - Analysis and Optimization Experiences with PHP Summary Re-use existing run-time for language Better yet: standardize libraries (and language?), including FFI Analysis needs to be precise, and whole-program Pessimistic assumptions spread Language, implementation and community need to be fixed All related? Trinity College Dublin 49
  • 66. Introduction to phc Current state of phc Next for phc - Analysis and Optimization Experiences with PHP Thanks phc needs contributors contribute: http://phpcompiler.org/contribute.html mailing list: phc-general@phpcompiler.org slides: http://www.cs.tcd.ie/~pbiggar/ contact: paul.biggar@gmail.com Trinity College Dublin 50
  • 67. Introduction to phc Current state of phc Next for phc - Analysis and Optimization Experiences with PHP Complex cases Hashtables Implicit conversions Variable-variables $GLOBALS Static includes $SESSION Compiler temporaries Trinity College Dublin 51