9148c358d0
Topic_Agent/Topic_Blog/Topics/Topics_Biz/Topics_Meeting/Topics_Rag의 마크다운 지식 문서를 Topic_General/Topic_Programming/Topic_Graphic/Topic_Business 4개 카테고리로 재분류. - 중복 제거: frontmatter의 status:duplicate/merged + duplicate_of/redirect_to 필드로 자기 자신을 중복으로 선언한 리다이렉트 stub 1032개 제거, 완전 동일 내용 파일 472개 제거, 동일 파일명·다른 내용 충돌 시 더 큰(완전한) 버전만 유지(162개 제거) — 총 1639개 중복 제거. - 분류: 폴더 단위로 명확한 항목(AI_and_ML/Coding/Architecture 등 → Programming, Comfyui/Visual_Effects → Graphic, Topics_Biz/Topics_Meeting/사업 등 → Business, Poetic_Blog_Writing/창의성/Game_Design 등 → General)은 폴더 우선순위로, 나머지 혼재 폴더(Topic_Agent/Topic_Blog/Topics 루트/Thinking & Reasoning/Other/UI_UX_Assets)는 title/tags 키워드 스코어링으로 파일 단위 분류(불명확한 경우 General로 폴백). 원본 폴더명은 "From_*" 서브폴더로 보존해 추적 가능성 유지. - 최종 배치: Programming 2784 / General 1608 / Graphic 285 / Business 249 = 4926개 문서. - 에이전트 운영 상태(.astra/.agent/.obsidian/sessions/memory/_company/docs/lessons/_shared/src)는 지식 콘텐츠가 아니므로 재분류 대상에서 제외하고 원위치 유지. - Topics/Topic_email(상위 보호 폴더 Topic_email과 파일명 100% 중복) 삭제 — 보호 폴더 자체는 미변경. - 완전히 비게 된 Topic_Agent/Topic_Blog/Topics_Biz/Topics_Rag 폴더 제거.
191 lines
5.2 KiB
Markdown
191 lines
5.2 KiB
Markdown
---
|
|
id: wiki-2026-0508-buffer-allocation
|
|
title: Buffer Allocation
|
|
category: 10_Wiki/Topics
|
|
status: verified
|
|
canonical_id: self
|
|
aliases: [ArrayBuffer Allocation, Typed Array Pool]
|
|
duplicate_of: none
|
|
source_trust_level: A
|
|
confidence_score: 0.9
|
|
verification_status: applied
|
|
tags: [performance, memory, buffer, typed-array, gc]
|
|
raw_sources: []
|
|
last_reinforced: 2026-05-10
|
|
github_commit: pending
|
|
tech_stack:
|
|
language: JavaScript/TypeScript
|
|
framework: Browser/Node/WebGPU
|
|
---
|
|
|
|
# Buffer Allocation
|
|
|
|
## 매 한 줄
|
|
> **"매 buffer 매 reuse 매 GC 의 X"**. Buffer allocation은 binary data buffer (ArrayBuffer / Typed Array)의 effective lifecycle 관리 — naive `new Uint8Array(N)` per operation은 GC churn을 유발해 frame budget을 깬다. 2026 WebGPU / WebCodecs / canvas heavy app 의 must-skill.
|
|
|
|
## 매 핵심
|
|
|
|
### 매 cost source
|
|
- **Allocation**: V8/SpiderMonkey backing-store malloc + zero-fill.
|
|
- **GC**: large allocation → Old-gen → expensive sweep.
|
|
- **Cache miss**: 매 fresh memory 의 cold cache.
|
|
- **Fragmentation**: many sizes → heap 의 fragmented.
|
|
|
|
### 매 strategy
|
|
- **Pool / freelist**: 매 size class 매 reuse.
|
|
- **Subarray view**: single big buffer + slice views.
|
|
- **Pre-allocation**: peak size 부터 allocate.
|
|
- **SharedArrayBuffer**: cross-thread, no copy.
|
|
|
|
### 매 응용
|
|
1. Audio / video frame buffer.
|
|
2. WebGL / WebGPU vertex / index buffer.
|
|
3. WebSocket binary message parser.
|
|
|
|
## 💻 패턴
|
|
|
|
### Simple buffer pool
|
|
```ts
|
|
class BufferPool {
|
|
private pool: Uint8Array[] = [];
|
|
constructor(private size: number, private max = 32) {}
|
|
|
|
acquire(): Uint8Array {
|
|
return this.pool.pop() ?? new Uint8Array(this.size);
|
|
}
|
|
|
|
release(buf: Uint8Array) {
|
|
if (this.pool.length < this.max) {
|
|
buf.fill(0);
|
|
this.pool.push(buf);
|
|
}
|
|
}
|
|
}
|
|
|
|
const pool = new BufferPool(4096);
|
|
const b = pool.acquire();
|
|
// use ...
|
|
pool.release(b);
|
|
```
|
|
|
|
### Size-class pool
|
|
```ts
|
|
class SizeClassPool {
|
|
private classes = new Map<number, Uint8Array[]>();
|
|
|
|
private classOf(n: number): number {
|
|
return Math.pow(2, Math.ceil(Math.log2(Math.max(n, 16))));
|
|
}
|
|
|
|
acquire(n: number): Uint8Array {
|
|
const cls = this.classOf(n);
|
|
const list = this.classes.get(cls);
|
|
return (list?.pop() ?? new Uint8Array(cls)).subarray(0, n);
|
|
}
|
|
|
|
release(buf: Uint8Array) {
|
|
const cls = buf.buffer.byteLength;
|
|
if (!this.classes.has(cls)) this.classes.set(cls, []);
|
|
this.classes.get(cls)!.push(new Uint8Array(buf.buffer));
|
|
}
|
|
}
|
|
```
|
|
|
|
### Single backing buffer + views
|
|
```ts
|
|
const big = new ArrayBuffer(1024 * 1024); // 1MB
|
|
const headers = new Uint32Array(big, 0, 256); // 1KB header
|
|
const body = new Uint8Array(big, 1024, 1024 * 1023); // remainder
|
|
```
|
|
|
|
### WebSocket binary parsing — no copy
|
|
```ts
|
|
ws.binaryType = 'arraybuffer';
|
|
ws.onmessage = (e: MessageEvent<ArrayBuffer>) => {
|
|
const dv = new DataView(e.data);
|
|
const type = dv.getUint8(0);
|
|
const length = dv.getUint32(1, true);
|
|
const payload = new Uint8Array(e.data, 5, length);
|
|
handle(type, payload);
|
|
};
|
|
```
|
|
|
|
### SharedArrayBuffer ring buffer (worker comms)
|
|
```ts
|
|
// main
|
|
const sab = new SharedArrayBuffer(1024);
|
|
const view = new Int32Array(sab);
|
|
worker.postMessage(sab);
|
|
|
|
// worker — Atomics 매 lock-free synchronization
|
|
self.onmessage = (e) => {
|
|
const view = new Int32Array(e.data);
|
|
Atomics.store(view, 0, 42);
|
|
Atomics.notify(view, 0, 1);
|
|
};
|
|
```
|
|
|
|
### Reusable canvas pixel buffer
|
|
```ts
|
|
class FrameRecycler {
|
|
private buffers: ImageData[] = [];
|
|
acquire(w: number, h: number) {
|
|
return this.buffers.find(b => b.width === w && b.height === h)
|
|
?? new ImageData(w, h);
|
|
}
|
|
release(b: ImageData) {
|
|
if (this.buffers.length < 4) this.buffers.push(b);
|
|
}
|
|
}
|
|
```
|
|
|
|
### Detecting allocation hot spots
|
|
```ts
|
|
// 매 dev-only — 매 wrap allocator 매 trace
|
|
const _orig = Uint8Array;
|
|
let count = 0;
|
|
(globalThis as any).Uint8Array = function (...args: any[]) {
|
|
count++;
|
|
if (count % 1000 === 0) console.warn('Uint8Array allocations:', count);
|
|
return new _orig(...args);
|
|
};
|
|
```
|
|
|
|
## 매 결정 기준
|
|
| 상황 | Approach |
|
|
|---|---|
|
|
| Per-frame temp buffer | pool with size class |
|
|
| Streaming binary protocol | preallocated parser buffer |
|
|
| Cross-thread share | SharedArrayBuffer + Atomics |
|
|
| GPU upload | persistent GPU buffer + map/unmap |
|
|
| Rare large alloc | direct allocate |
|
|
|
|
**기본값**: measure GC pressure first; pool only when allocator shows up in profile.
|
|
|
|
## 🔗 Graph
|
|
- 부모: [[Memory Management]] · [[Performance]]
|
|
- 변형: [[Object Pool]]
|
|
- 응용: [[WebGPU]]
|
|
- Adjacent: [[SharedArrayBuffer]] · [[TypedArray]] · [[Garbage Collection]]
|
|
|
|
## 🤖 LLM 활용
|
|
**언제**: pool implementation scaffold, view layout calculation, ring buffer design.
|
|
**언제 X**: 매 GC actual measurement — Chrome Memory profiler.
|
|
|
|
## ❌ 안티패턴
|
|
- **Premature pool**: 매 micro-opt — measure first.
|
|
- **Pool 무제한**: 매 leak — max size cap.
|
|
- **Subarray after detach**: transferred buffer — invalid.
|
|
- **Concurrent writes without Atomics**: SharedArrayBuffer 매 race.
|
|
- **Forget to zero-fill on release**: 매 stale data leak across owners.
|
|
|
|
## 🧪 검증 / 중복
|
|
- Verified (V8 blog, MDN ArrayBuffer, WebGPU spec).
|
|
- 신뢰도 A.
|
|
|
|
## 🕓 Changelog
|
|
| 날짜 | 변경 |
|
|
|---|---|
|
|
| 2026-05-08 | Phase 1 |
|
|
| 2026-05-10 | Manual cleanup — buffer pool + view layout pattern |
|