Performance Optimization

Measure first with Devel::NYTProf and Benchmark. Optimize algorithms before micro-optimizing.

Data structures: use hashes for lookups (O(1)) instead of grep over arrays. Avoid copying large arrays/hashes: pass references.

Regex: precompile with qr//, anchor patterns, avoid needless captures ((?:) or /n), avoid catastrophic backtracking, use index() for simple substring checks.

I/O: read line by line for large files. Use sysread with big buffers for binary throughput. Set $| only when needed. Batch database operations (prepared statements, transactions, execute_array).

Loops: hoist invariant work out of loops. Use for over a list rather than C-style indexing. map/grep are fast. Use the Schwartzian Transform (or Orcish Maneuver) for expensive sort keys.

Strings: build with .= or join. Don't concatenate in nested loops when join would do.

Function calls: method and sub calls have overhead. Inline in hot loops if profiling shows it matters. Use state for caches and Memoize for pure functions.

XS modules: use XS-backed modules (JSON::XS/Cpanel::JSON::XS, Text::CSV_XS, List::Util, YAML::XS, DBD drivers). Write critical code in C via XS/FFI (Module 51).

Startup time: lazy-load heavy modules (require inside subs), prefer Moo over Moose for CLI tools, use preforking servers (Starman/Hypnotoad) for web apps.

Parallelism: use Parallel::ForkManager/MCE for CPU-bound batch work, and event loops for I/O-bound work.

Upgrade Perl: newer releases bring performance improvements (e.g., faster sub signatures, regex engine work, builtin functions).