View Source cure_pipe_optimizer (cure v0.7.0)

Optimizations specifically for pipe operator chains.

This module provides compile-time optimizations for pipe operator usage, focusing on eliminating redundant wrapping/unwrapping and inlining simple pipe chains when type information proves they cannot fail.

Summary

Functions

Check if a pipe chain can be inlined to direct function calls.

Determine if a pipe chain is provably error-free.

Statistics about pipe operator optimizations applied.

Optimize a pipe operator chain based on type information.

Functions

can_inline_pipe_chain(PipeChain, TypeEnv)

Check if a pipe chain can be inlined to direct function calls.

A pipe chain can be inlined if:

  1. All functions in the chain are pure (no side effects)
  2. Type information proves no errors can occur
  3. The chain is short enough (to avoid code bloat)

Arguments

  • PipeChain - Pipe operator chain expression
  • TypeEnv - Type environment for analysis

Returns

  • true - Can be inlined
  • false - Should keep monadic pipe

format_stats/1

is_error_free_pipe(PipeChain, TypeEnv)

Determine if a pipe chain is provably error-free.

A pipe chain is error-free if type analysis proves that:

  1. No function in the chain can return Error
  2. All type constraints are satisfied
  3. No runtime exceptions can occur

Arguments

  • PipeChain - Pipe operator chain expression
  • TypeEnv - Type environment for type checking

Returns

  • true - Provably error-free
  • false - May produce errors

new_stats()

Statistics about pipe operator optimizations applied.

optimize_pipe_chain/2

Optimize a pipe operator chain based on type information.

This function analyzes a pipe chain and applies optimizations:

  1. Eliminate redundant Ok wrapping/unwrapping
  2. Inline error-free chains to direct function calls
  3. Specialize monomorphic pipe operations

Arguments

  • PipeExpr - Binary operation expression with '|>' operator
  • TypeEnv - Type environment for type inference

Returns

  • {ok, OptimizedExpr, Stats} - Optimized expression with statistics
  • {unchanged, PipeExpr, Stats} - No optimization applied with statistics

update_stats/2