Skip to content

existential-birds/beagle / plugins/beagle-elixir/skills/elixir-performance-review

elixir-performance-review

Verified

Reviews Elixir code for performance issues including GenServer bottlenecks, memory usage, and concurrency patterns. Use when reviewing high-throughput code or investigating performance issues.

FileKindSize
SKILL.mdskill md3.2 KB
references/concurrency.mdmarkdown2.6 KB
references/ets-patterns.mdmarkdown2.5 KB
references/genserver-bottlenecks.mdmarkdown3.0 KB
references/memory.mdmarkdown2.5 KB

references/memory.md

View on GitHub
# Memory Patterns

## Binary Handling

### Large Binaries Are Reference Counted

Binaries > 64 bytes are stored on shared heap. Copying between processes is cheap (reference copy).

```elixir
# Efficient - only reference copied
send(pid, large_binary)

# But beware of sub-binaries holding reference to large binary
<<header::binary-size(100), _rest::binary>> = large_binary
# header still references entire large_binary!
```

### Force Copy When Needed

```elixir
# Release reference to large binary
header = :binary.copy(<<header::binary-size(100), _::binary>> = large_binary)
```

## Process Heap

### Large State = Large GC

Each process has its own heap. Large state means:
- Longer GC pauses
- More memory per process

```elixir
# BAD - accumulating large state
def handle_cast({:add, item}, state) do
  {:noreply, [item | state.items]}  # Grows forever!
end

# GOOD - bounded state
def handle_cast({:add, item}, state) do
  items = Enum.take([item | state.items], @max_items)
  {:noreply, %{state | items: items}}
end
```

### Use ETS for Large Shared State

```elixir
# BAD - large map in GenServer
defmodule BigCache do
  use GenServer
  def init(_), do: {:ok, %{}}  # Millions of entries here
end

# GOOD - ETS for large state
defmodule BigCache do
  def init do
    :ets.new(:cache, [:set, :public, :named_table])
  end
end
```

## Message Passing

### Avoid Large Message Copies

```elixir
# BAD - copies entire list to each process
Enum.each(workers, fn pid ->
  send(pid, {:process, large_list})
end)

# GOOD - send reference or key
Enum.each(workers, fn pid ->
  send(pid, {:process, :ets.whereis(:data), key})
end)
```

## Streams for Large Data

### Use Streams to Avoid Loading All in Memory

```elixir
# BAD - loads entire file
File.read!("large.csv")
|> String.split("\n")
|> Enum.map(&parse_line/1)

# GOOD - streams line by line
File.stream!("large.csv")
|> Stream.map(&parse_line/1)
|> Enum.to_list()  # Or process incrementally
```

### Database Streams

```elixir
# BAD - loads all records
Repo.all(User)
|> Enum.map(&process/1)

# GOOD - streams from database
User
|> Repo.stream()
|> Stream.map(&process/1)
|> Stream.run()
```

## Detecting Memory Issues

```elixir
# Process memory
Process.info(self(), :memory)

# System memory
:erlang.memory()

# Binary memory specifically
:erlang.memory(:binary)
```

## Review Questions

1. Are large binaries being unnecessarily copied?
2. Is process state bounded or growing unbounded?
3. Are streams used for large data processing?
4. Is shared state in ETS rather than process heap?
receiving-code-reviewobra/superpowersUse when receiving code review feedback, before implementing suggestions, especially if feedback seems unclear or technically questionable - requires technical rigor and verification, not performative agreement or blind implementationStars: 322kUpdated Claude CodeCodexVerifiedInstall
requesting-code-reviewobra/superpowersUse when completing tasks, implementing major features, or before merging to verify work meets requirementsStars: 322kUpdated Claude CodeCodexVerifiedInstall
using-git-worktreesobra/superpowersUse when starting feature work that needs isolation from current workspace or before executing implementation plans - ensures an isolated workspace exists via native tools or git worktree fallbackStars: 322kUpdated Claude CodeCodexVerifiedInstall
verification-before-completionobra/superpowersUse when about to claim work is complete, fixed, or passing, before committing or creating PRs - requires running verification commands and confirming output before making any success claims; evidence before assertions alwaysStars: 322kUpdated Claude CodeCodexVerifiedInstall