php-memory-optimisation

php-memory-optimisation is a cursor rule for Cursor from ivangrynenko/cursorrules. It costs 0 tokens per session (878 once invoked), scanned A, original, MIT.

A set of PHP checks for reducing peak memory use. It flags operations that load large database results or files into memory and suggests streaming or incremental alternatives.

In plain words
What is it for?
Use it when reviewing PHP code that processes large query results, files, ranges, or arrays.
Why use it?
It helps avoid excessive memory consumption that can slow down or crash PHP applications. The checks focus on common costly operations such as fetchAll(), repeated file reads, and array merging in loops.

Cursor rule for Cursor

Written for Cursor: installed under .cursor/.

Good fit Use it when reviewing PHP code that processes large query results, files, ranges, or arrays.

Compare 6 cursor rules from other repositories ↓
Install with agentmods
npx agentmods add rules/ivangrynenko/cursorrules/php-memory-optimisation
Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

Clone the repo
git clone --depth 1 https://github.com/ivangrynenko/cursorrules

Made for: Cursor.

Wrote this? Show the measurements

A badge with what this costs and how it scanned, read live from this page, so it follows the numbers instead of freezing them. Markdown for a README, HTML for a documentation site or a project page.

agentmods badge for php-memory-optimisation

README.md
[![agentmods](https://agentmods.dev/badge/rules/ivangrynenko/cursorrules/php-memory-optimisation/github.svg)](https://agentmods.dev/rules/ivangrynenko/cursorrules/php-memory-optimisation)
Your own site
<a href="https://agentmods.dev/rules/ivangrynenko/cursorrules/php-memory-optimisation"><img src="https://agentmods.dev/badge/rules/ivangrynenko/cursorrules/php-memory-optimisation/github.svg" alt="Measured on agentmods" height="20"></a>

Or the 80×15 button, for a site that already has a row of RSS and ATOM ones. Only the verdict fits; the numbers stay here.

agentmods 80×15 button for php-memory-optimisation

Your own site · 80×15
<a href="https://agentmods.dev/rules/ivangrynenko/cursorrules/php-memory-optimisation"><img src="https://agentmods.dev/badge/rules/ivangrynenko/cursorrules/php-memory-optimisation.svg" alt="Reviewed on agentmods" width="80" height="20"></a>
Per session 0 Nothing until a file matches its globs; then the whole rule loads.
When invoked 878 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 1 finding. A grade says what 26 rules found in the file — not that it is safe.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5.1 $0.00000 $0.00878
Opus 5.5 $0.00000 $0.00351
Sonnet 5.5 $0.00000 $0.00176
Haiku 4.5 $0.00000 $0.00088

Measured 1mo ago against content hash df596c1fd54a, method: parsed. Prices are Anthropic first-party input rates as of 2026-10-07, from the pricing page.

Security

Grade A, and why

php-memory-optimisation scanned grade A with 1 finding against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 1mo ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Makes network callslowCapability

Not a fault in itself. Listed so you know the mod talks to something, and to what.

- **Stream database results:** Prefer `$stmt->fetch(PDO::FETCH_ASSOC)` in a `while` loop or use generators instead of `fetchAll()`.
.cursor/rules/php-memory-optimisation.mdc · 86 lines

What it actually says

PHP Memory Optimisation Standards

Guidance and automated checks to reduce peak memory usage in PHP applications. Based on widely accepted practices and the article "PHP Memory Optimization Tips" by Khouloud Haddad.

actions:

  • type: enforce conditions:

    Avoid loading entire DB result sets into memory.

    • pattern: "->fetchAll\(" message: "Avoid fetchAll() on large result sets; iterate with fetch() in a loop or wrap with a generator (yield)."

    • pattern: "\bmysqli_fetch_all\(" message: "Avoid mysqli_fetch_all() for large queries; prefer streaming fetch (e.g., mysqli_fetch_assoc in a loop)."

    Avoid repeated full-file loads inside loops.

    • pattern: "foreach\s*\([^)]\)\s\{[^}]*file_get_contents\(" message: "Avoid file_get_contents() inside loops; stream with SplFileObject or read once and reuse."

    Avoid array_merge in tight loops as it copies arrays.

    • pattern: "foreach\s*\([^)]\)\s\{[^}]=\sarray_merge\(" message: "Avoid array_merge() inside loops; append elements directly or preallocate arrays."

    Use caution with range() on large ranges (allocates full array).

    • pattern: "\brange\s*\(" message: "range() allocates full arrays; for large ranges consider generators (yield) to avoid high memory."
  • type: suggest message: | PHP memory optimisation recommendations:

    • Stream database results: Prefer $stmt->fetch(PDO::FETCH_ASSOC) in a while loop or use generators instead of fetchAll().
    • Use generators (yield): Iterate large datasets without allocating full arrays.
    • Stream files: Use SplFileObject or chunked reads instead of file_get_contents() for large files.
    • Minimise array copying: Avoid array_merge() in loops; push items directly or pre-size with known capacity (e.g., SplFixedArray).
    • Free memory explicitly: unset($var) after large data is no longer needed; consider gc_collect_cycles() for long-running scripts.
    • Profile memory: Use memory_get_usage() and tools like Xdebug/Blackfire to spot peaks.
    • OPcache: Ensure OPcache is enabled and sized appropriately in production.
  • type: validate conditions:

    Detect full-file reads that likely could be streamed.

    • pattern: "\bfile\s*\(" message: "file() reads entire files into memory; prefer SplFileObject for line-by-line streaming."

metadata: priority: high version: 1.0

actions:

  • type: suggest message: | OPcache recommendations (php.ini):

    • Set opcache.enable=1 and opcache.enable_cli=1 for CLI scripts that process large datasets.
    • Size memory pool appropriately, e.g., opcache.memory_consumption=128 (adjust to your project).
    • Consider opcache.interned_strings_buffer and opcache.max_accelerated_files for larger codebases.
  • type: enforce conditions:

    • pattern: "(?mi)^opcache\.enable\s*=\s*0" message: "Enable OPcache in production (set opcache.enable=1) to reduce memory and CPU overhead."

metadata: priority: medium version: 1.0

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 1mo ago First seen · 86 lines · 878 tokens per session scan A df596c1fd54a

Subscribe to this mod's changes

php-memory-optimisation is a cursor rule published in the GitHub repository ivangrynenko/cursorrules (88 stars, last pushed 11mo ago), licensed MIT. It costs nothing until one of its globs matches a file; then it loads 878 tokens. A static security scan graded it A with 1 finding (makes network calls). No closer match exists in the catalogue, so it is treated as the original; first seen 2026-09-03.