Skip to content

Optimization Proposal: Compile-time Resolution of Default Arguments #17449

@tahaghafuri

Description

@tahaghafuri

Description

Description

Currently, when PHP functions have default arguments containing PHP code expressions (like __FILE__.$a), these expressions are stored as AST nodes and evaluated at runtime via the ZEND_RECV_INIT opcode. This approach has performance implications since it requires runtime evaluation of the AST for each function call where the default argument is used.

Current Behavior

function example($param = __FILE__ . '/suffix') {
// Function body
}

This currently generates a ZEND_RECV_INIT opcode that stores the expression __FILE__ . '/suffix' as an AST node for runtime evaluation.

Proposed Behavior

The same code would be compiled into bytecode operations, potentially like:

  1. Check if argument was provided
  2. If not, execute the compiled bytecode for the default value computation
  3. Jump to function body

Benefits

  1. Performance Improvement: Eliminates the overhead of storing and evaluating AST nodes at runtime
  2. Memory Optimization: Reduces memory usage by storing compiled bytecode instead of AST structures
  3. Execution Efficiency: Allows for potential compile-time optimizations of default value expressions

Technical Considerations

  • The new opcode would need to handle both simple and complex expressions appropriately
  • Special handling may be required for dynamic values like __FILE__
  • Backward compatibility should be maintained for edge cases

Similar Precedents

This optimization approach is similar to how PHP already handles static variable initialization through:

  • ZEND_BIND_STATIC
  • ZEND_BIND_INIT_STATIC_OR_JMP

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions