Skip to content

Instantly share code, notes, and snippets.

@boxabirds
Created January 25, 2025 03:03
Show Gist options
  • Save boxabirds/b92fec28c58e6c2cc9513f16c2bbeb91 to your computer and use it in GitHub Desktop.
Save boxabirds/b92fec28c58e6c2cc9513f16c2bbeb91 to your computer and use it in GitHub Desktop.
node_modules on Mac RAM disk

Deepseek r1 says this

Key Clarifications

  1. APFS Global Kernel Lock (vfs-layer lock):

    • Not fully bypassed: Even with a RAM disk formatted as APFS, macOS's global I/O lock still technically exists. However, because RAM disk operations are orders of magnitude faster than disk I/O, contention for this lock is drastically reduced. This makes the lock effectively irrelevant for most workflows, as operations complete before significant contention occurs.
    • Not the same as Linux: macOS lacks a true tmpfs equivalent, so while a RAM disk helps, it’s not as seamless as Linux’s in-memory filesystem.
  2. RAM Disk Format:

    • macOS RAM disks can be formatted as HFS+ or APFS. While APFS is the default, formatting as HFS+ might slightly reduce lock contention (though benchmarks vary). Either way, the speed of RAM mitigates lock-related bottlenecks.
  3. Real-World Impact:

    • Tools like pnpm or npm/yarn that perform many small file operations benefit most, as macOS’s disk I/O struggles with metadata-heavy workloads. RAM disks eliminate physical disk latency, making these operations near-instant.

Practical Adjustments

1. Use HFS+ for the RAM Disk (Optional)

Some users report better performance with HFS+ for metadata-heavy tasks. To test:

diskutil partitionDisk $DISK_ID GPT HFS+ "$DISKNAME" 0

2. Automount with Data Persistence (If Needed)

Use a script to:

  • Recreate the RAM disk on reboot.
  • Restore critical data (e.g., pnpm-store) from a backup on disk.
    Example script:
# Create RAM disk
SIZE=8192  # 8GB
DISKNAME=RamDisk
DISK_ID=$(hdiutil attach -nomount ram://$((SIZE * 2048)) | xargs)
diskutil eraseDisk HFS+ "$DISKNAME" $DISK_ID

# Restore pnpm-store from backup (if exists)
[ -d ~/pnpm-store-backup ] && rsync -a ~/pnpm-store-backup/ /Volumes/RamDisk/pnpm-store/

3. Monitor Memory Pressure

  • Allocate only what your system can spare (e.g., 4–8GB for a 16GB RAM machine).
  • Use sudo memory_pressure or Activity Monitor to check for memory strain.

Alternatives to RAM Disks

  1. OverlayFS with Docker:
    Mount project directories as in-memory overlay layers in Docker containers. Example:

    docker run -v /path/to/project:/app -v /dev/shm:/app/node_modules ...

    This isolates I/O from the host’s filesystem.

  2. Linux VM (Last Resort):
    Tools like UTM or Parallels can run Linux with near-native performance on Apple Silicon. Use this if macOS’s I/O limitations are unbearable.


Conclusion

RAM disks do improve performance for macOS development workflows, but they’re not magic:

  • Pro: Mitigates APFS lock contention by reducing I/O latency to near-zero.
  • ⚠️ Con: Doesn’t eliminate the lock—metadata operations are still serialized, but too fast to matter.
  • 🚨 Critical: Always back up your RAM disk contents (e.g., cron jobs to sync pnpm-store to disk hourly).

For most users, this setup is worth the effort. However, if your workflow involves massive monorepos or requires Linux-like filesystem behavior, a VM or dual-booting Linux may still be the better long-term solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment