---
id: wiki-2026-0508-pointer-compression
title: Pointer Compression
category: 10_Wiki/Topics
status: verified
canonical_id: self
aliases: [V8 pointer compression, compressed pointers, 32-bit pointers in 64-bit heap]
duplicate_of: none
source_trust_level: A
confidence_score: 0.9
verification_status: applied
tags: [v8, javascript-engine, memory, optimization]
raw_sources: []
last_reinforced: 2026-05-10
github_commit: pending
tech_stack:
  language: cpp
  framework: v8
---

# Pointer Compression

## 매 한 줄
> **"매 64-bit 시스템 위에서 heap pointer를 32-bit 로 줄여 절반 공간으로 저장"**. 매 V8(2020), Hermes, JVM(CompressedOops, 2008부터) 의 heap memory ~40% 절감 + 매 cache locality 향상. 매 4GB 안 heap 제한 대신 매 typical web app 에는 충분.

## 매 핵심

### 매 동기
- 매 64-bit OS 의 V8 heap object 의 80% 가 pointer field 차지.
- 매 pointer = 매 8 bytes 인데 실제 정보 entropy ~32 bits (heap < 4GB).
- 매 절반으로 → 매 RAM↓, 매 GC scan 시간 ↓, 매 L1/L2 cache hit ↑.

### 매 V8 scheme (post-2020)
- **Cage**: 매 4GB 정렬된 가상 메모리 reservation.
- **Base register / R13**: 매 cage base 주소 보관 (cached).
- **32-bit slot**: 매 cage 안 offset.
- **Smi tagging**: 매 31-bit integer 의 inline (1 LSB tag).
- **Decompress**: `addr = base + (int64_t)(int32_t)compressed`.

### 매 trade-off
- **+** Heap mem ~30–43% 절감, 매 V8 startup ↓.
- **+** Cache pressure ↓ → 매 throughput 5-10% ↑.
- **−** Per-access decompression cost (매 작음, 보통 +1 ALU).
- **−** Heap 4GB 한계 (매 큰 app 의 큰 process 분할 필요).
- **−** External pointer 는 sandboxed pointer table 로 indirection 추가.

### 매 비슷한 system
- **JVM CompressedOops**: 매 8-byte aligned + shift → 매 32GB heap 까지 32-bit.
- **CHERI capabilities**: 매 다른 방향 — 매 capability + bounds 로 fat pointer.
- **Hermes (RN)**: 매 32-bit pointer + handle table.
- **WebAssembly memory64 vs memory32**: 매 동일 motif.

## 💻 패턴

### Compress / decompress (V8 conceptual)
```cpp
// 매 V8 cage base = 4GB-aligned
inline Address Decompress(uint32_t compressed, Address cage_base) {
    return cage_base + static_cast<int64_t>(static_cast<int32_t>(compressed));
}

inline uint32_t Compress(Address full) {
    return static_cast<uint32_t>(full); // 매 lower 32 bits
}

// 매 Smi (Small Integer) tagging
constexpr int kSmiTagSize = 1;
inline bool IsSmi(uint32_t v) { return (v & 1) == 0; }
inline int32_t SmiValue(uint32_t v) { return static_cast<int32_t>(v) >> 1; }
```

### Tagged slot in heap object
```cpp
class HeapObject {
    uint32_t map_;           // 매 type info, compressed
    uint32_t properties_;    // 매 compressed
    uint32_t elements_;      // 매 compressed
    // 매 instance fields follow, all 4 bytes each
};
// 매 compare: 64-bit pointer system 에서 same object = 24 bytes header
```

### JVM CompressedOops
```bash
# 매 default ON when heap ≤ 32GB on 64-bit JVM
java -XX:+UseCompressedOops -Xmx16g App
# 매 8-byte alignment + 3-bit shift → 매 32-bit reference 가 32GB 까지 cover
java -XX:ObjectAlignmentInBytes=8 ...
```

### Sandboxed external pointers (V8 2024+)
```cpp
// 매 untrusted external ptr → 매 indirection table
class ExternalPointerTable {
    Address entries_[N];
public:
    uint32_t Allocate(Address external) {
        uint32_t handle = next_++;
        entries_[handle] = external;
        return handle; // 매 store handle 32-bit, not raw ptr
    }
    Address Resolve(uint32_t handle) { return entries_[handle]; }
};
```

### Disable pointer compression (V8 build)
```bash
# 매 large heap (>4GB) needed
gn args out.gn/x64.release
# v8_enable_pointer_compression = false
ninja -C out.gn/x64.release v8
```

### Hermes 32-bit handle (React Native)
```cpp
// 매 HermesValue = 64-bit NaN-boxed; pointer 부분은 cage offset 32-bit
struct HermesValue {
    uint64_t raw;
    bool isPointer() const { return (raw & kTagMask) == kPointerTag; }
    HeapPtr getPointer() const { return base_ + (raw & kPayloadMask); }
};
```

## 매 결정 기준
| 상황 | Approach |
|---|---|
| Browser tab / Node ≤ 4GB | V8 pointer compression ON (default) |
| Large server-side V8 (>4GB) | Compile with compression OFF or use isolates |
| JVM ≤ 32GB heap | CompressedOops ON (default) |
| JVM > 32GB | UseCompressedOops auto-OFF |
| Mobile JS (Hermes) | Always 32-bit handle |
| Custom GC engine | 매 cage allocator + tagged 4-byte slot 직접 구현 |

**기본값**: 매 modern engine 의 default ON; 매 disable 은 명백한 large-heap 이유 있을 때만.

## 🔗 Graph
- 부모: [[Garbage Collection]]
- 응용: [[V8]] · [[Hermes]] · [[Chromium]]

## 🤖 LLM 활용
**언제**: 매 V8/JVM/JS-engine 의 heap memory profiling. 매 mobile/tab memory pressure.
**언제 X**: 매 native-only (Rust/C++) 의 단순 64-bit pointer — 매 자체 GC 없으면 의미 적음.

## ❌ 안티패턴
- **>4GB V8 heap with compression ON**: 매 OOM crash. 매 isolate 분할 또는 disable.
- **Misaligned cage**: 매 4GB align 안 되면 decompress 산술 깨짐.
- **External raw pointer in heap**: 매 sandbox bypass + crash on heap relocation. → 매 external pointer table 필수.
- **Assumption that pointer = 8 bytes** in tooling: 매 V8 inspector / heap snapshot tools 가 가정 하면 corrupt read.

## 🧪 검증 / 중복
- Verified (V8 blog "Pointer Compression in V8" 2020, JVM CompressedOops docs, Hermes design doc).
- 신뢰도 A.

## 🕓 Changelog
| 날짜 | 변경 |
|---|---|
| 2026-05-08 | Phase 1 |
| 2026-05-10 | Manual cleanup — V8 cage scheme + CompressedOops + sandboxed external |
