Awesome-claude-code optimize-docker-opcache

Optimizes OPcache configuration for PHP Docker containers. Configures memory, file limits, JIT, and validation for production and development.

install
source · Clone the upstream repo
git clone https://github.com/dykyi-roman/awesome-claude-code
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/dykyi-roman/awesome-claude-code "$T" && mkdir -p ~/.claude/skills && cp -r "$T/skills/optimize-docker-opcache" ~/.claude/skills/dykyi-roman-awesome-claude-code-optimize-docker-opcache && rm -rf "$T"
manifest: skills/optimize-docker-opcache/SKILL.md
source content

OPcache Configuration Optimization

Provides production and development OPcache tuning for PHP Docker containers, including JIT and preloading.

OPcache Impact Overview

┌─────────────────────────────────────────────────────────────────┐
│                  OPCACHE PERFORMANCE IMPACT                      │
├─────────────────────────────────────────────────────────────────┤
│  Without OPcache:                                               │
│  Request → Parse → Compile → Execute → Response      ~50ms     │
│  With OPcache:                                                  │
│  Request → Execute (cached bytecode) → Response      ~15ms     │
│  With OPcache + JIT:                                            │
│  Request → Execute (native code) → Response          ~10ms     │
│  With OPcache + JIT + Preload:                                  │
│  Request → Execute (preloaded) → Response             ~5ms     │
└─────────────────────────────────────────────────────────────────┘

Production vs Development Settings

SettingProductionDevelopmentPurpose
opcache.enable11Enable OPcache
opcache.enable_cli10Cache for CLI scripts
opcache.memory_consumption256128Shared memory (MB)
opcache.interned_strings_buffer3216Interned strings (MB)
opcache.max_accelerated_files2000010000Max cached files
opcache.validate_timestamps01Check file changes
opcache.revalidate_freq02Revalidation interval (s)
opcache.jit1255disableJIT mode
opcache.jit_buffer_size128M0JIT buffer

Memory Consumption Calculation

memory_consumption = total_php_files * avg_bytecode_size (20-50 KB per file)

  Small project (500 files):    15MB  → set 64MB
  Medium project (2000 files):  60MB  → set 128MB
  Large project (5000 files):  175MB  → set 256MB
  Enterprise (10000+ files):   350MB  → set 512MB

max_accelerated_files Calculation

PHP rounds up to the next prime from: 223, 463, 983, 1979, 3907, 7963, 16229, 32531, 65407, 130987

Total PHP FilesSet ToPHP Uses
< 2001000983
200-90040003907
900-380080007963
3800-80002000016229
8000-160003253132531

JIT Configuration (PHP 8.0+)

opcache.jit = CRTO (4-digit mode)
  C: CPU optimization (0=off, 1=on)
  R: Register allocation (0=off, 1=local, 2=global)
  T: Trigger (0=first run, 3=hot counter, 5=tracing)
  O: Optimization (0=none, 5=full)
ModeValueUse Case
Tracing (recommended)1255Web applications
Function1205Simpler, less memory
DisableddisableDevelopment, debugging

Preloading (PHP 7.4+)

<?php
// config/preload.php
$directories = [
    '/app/src/Domain/', '/app/src/Application/',
    '/app/vendor/symfony/http-kernel/',
];
foreach ($directories as $dir) {
    if (!is_dir($dir)) continue;
    $it = new RecursiveIteratorIterator(
        new RecursiveDirectoryIterator($dir, FilesystemIterator::SKIP_DOTS)
    );
    foreach ($it as $file) {
        if ($file->getExtension() === 'php') opcache_compile_file($file->getRealPath());
    }
}
CategoryPreload?Reason
Domain entities / Value objectsYesLoaded on every request
Framework coreYesAlways loaded
ControllersMaybeOnly relevant routes
Tests / MigrationsNoNot in production

Monitoring OPcache

MetricHealthyAction Needed
Hit rate> 99%< 95%: check validate_timestamps
Memory free> 20%< 10%: increase memory_consumption
Wasted memory< 5%> 10%: restart PHP-FPM
Cache fullfalsetrue: increase max_accelerated_files

Production OPcache INI

[opcache]
opcache.enable = 1
opcache.enable_cli = 1
opcache.memory_consumption = 256
opcache.interned_strings_buffer = 32
opcache.max_accelerated_files = 20000
opcache.validate_timestamps = 0
opcache.revalidate_freq = 0
opcache.save_comments = 1
opcache.fast_shutdown = 1
opcache.jit = 1255
opcache.jit_buffer_size = 128M
opcache.preload = /app/config/preload.php
opcache.preload_user = www-data
opcache.log_verbosity_level = 1
opcache.error_log = /proc/self/fd/2

Development OPcache INI

[opcache]
opcache.enable = 1
opcache.enable_cli = 0
opcache.memory_consumption = 128
opcache.interned_strings_buffer = 16
opcache.max_accelerated_files = 10000
opcache.validate_timestamps = 1
opcache.revalidate_freq = 2
opcache.save_comments = 1
opcache.jit = disable
opcache.jit_buffer_size = 0

Dockerfile Integration

ARG APP_ENV=production
COPY docker/php/opcache-${APP_ENV}.ini /usr/local/etc/php/conf.d/opcache.ini
COPY config/preload.php /app/config/preload.php

Before/After Comparison

MetricNo OPcacheOPcacheOPcache+JIT+Preload
First request200ms200ms50ms (preloaded)
Subsequent requests200ms15ms5ms
Memory per worker30MB35MB38MB
Throughput (req/s)50300500
CPU usageHighMediumLow

Generation Instructions

  1. Count PHP files:
    find /app -name "*.php" | wc -l
    (including vendor)
  2. Calculate memory: files * 35KB, round up to nearest power of 2
  3. Set max_accelerated_files: Next prime above file count
  4. Configure JIT: 1255 for production, disable for development
  5. Create preload script: Include domain, application, and framework core
  6. Set validate_timestamps: 0 for immutable containers, 1 for development
  7. Monitor: Check hit rate and memory usage after deployment