0. ๐ ์ ์์คํ /์ฃ์ง์ โZig & WebAssemblyโ์ธ๊ฐ์?
**Zig(v0.17.1)**๋ C/C++์ ๋์ฒด์ฌ์ด๋ฉด์ ๋์์ ๊ฐ๋ ฅํ ํฌ๋ก์ค ์ปดํ์ผ๋ฌ์ ๋ช
์์ Allocator ๋ชจ๋ธ์ ์ ๊ณตํฉ๋๋ค. ์จ๊ฒจ์ง ํ ํ ๋น์ด๋ ์์ธ(Exception)๊ฐ ์์ด ์์ญ KB ๋จ์์ ์ด๊ฒฝ๋ WebAssembly(.wasm) ๋ชจ๋์ ๋ง๋ค์ด ๋ธ๋ผ์ฐ์ , Cloudflare Workers, Wasmtime ์ฃ์ง ๋ฐํ์์ ์ฆ์ ๋ฐฐํฌํ ์ ์์ต๋๋ค.
1. ํ์ ์ฌ์ ์๊ฑด & ์์คํ ์๊ตฌ์ฌ์ (Prerequisites & System Requirements)
๐ป ์์คํ ์ค์น ๋ฐ ์คํ ์๊ตฌ์ฌ์ (System Requirements)
| ํญ๋ชฉ | ์ต์ ์ฌ์ (Minimum) | ๊ถ์ฅ ์ฌ์ (Recommended) | ์ฐธ๊ณ ๋ฐ ๋น๊ณ |
|---|---|---|---|
| ์ด์์ฒด์ (OS) | Windows 10/11, macOS 13+, Linux | Ubuntu 24.04 LTS / macOS Apple Silicon | ๋จ์ผ ์์ถ ํด์ ์ค์น |
| CPU | 2์ฝ์ด ์ด์ (x86_64 / ARM64) | 4์ฝ์ด ์ด์ | ์ฆ๋ถ ๋น๋ ๋ฐ WASM ์ต์ ํ |
| ๋ฉ๋ชจ๋ฆฌ (RAM) | 2 GB | 4 GB ~ 8 GB | Zig ์ปดํ์ผ๋ฌ ์บ์ + Wasmtime |
| ๋์คํฌ (Storage) | 1 GB ์ฌ์ ๊ณต๊ฐ | 5 GB+ | Zig ํด์ฒด์ธ + ํ ์คํธ ์ํฐํฉํธ |
| ๋ฐํ์ / ํด์ฒด์ธ | Zig 0.17.1, Node.js 22+ | Wasmtime 28+, ZLS (Zig Language Server) | zig version์ผ๋ก ํ์ธ |
# macOS / Linux (Homebrew) ๋๋ ๊ณต์ ๋ฐ์ด๋๋ฆฌ ์ค์น
brew install zig wasmtime
# ๋ฒ์ ํ์ธ
zig version # 0.17.1
wasmtime --version
[1๊ฐ] Comptime ๋ฉํํ๋ก๊ทธ๋๋ฐ & ์ ๋ก ์ค๋ฒํค๋ ์ถ์ํ
Zig์๋ C ์ ์ฒ๋ฆฌ๊ธฐ(#define)๋ ๋ณ๋์ ๋งคํฌ๋ก ๋ฌธ๋ฒ ๋์ **์ปดํ์ผ ํ์์ ์ผ๋ฐ Zig ์ฝ๋๋ฅผ ์คํํ๋ comptime**์ด ์์ต๋๋ค.
// src/comptime_demo.zig
const std = @import("std");
// ์ปดํ์ผ ํ์์ ํ์
(type)์ ๋ฐ์ ์ ๋ค๋ฆญ ๋ง ๋ฒํผ๋ฅผ ์์ฑํ๋ ํจ์
fn RingBuffer(comptime T: type, comptime capacity: usize) type {
return struct {
items: [capacity]T = undefined,
head: usize = 0,
count: usize = 0,
const Self = @This();
pub fn push(self: *Self, value: T) void {
const idx = (self.head + self.count) % capacity;
if (self.count == capacity) {
self.head = (self.head + 1) % capacity;
} else {
self.count += 1;
}
self.items[idx] = value;
}
};
}
pub fn main() void {
var buf = RingBuffer(u32, 4){};
buf.push(10);
buf.push(20);
std.debug.print("count={d}, first={d}\n", .{ buf.count, buf.items[buf.head] });
}
zig run src/comptime_demo.zig
# ์์ ์ถ๋ ฅ: count=2, first=10
[2๊ฐ] Allocator ํจํด๊ณผ ๋ฉ๋ชจ๋ฆฌ ๋์ ์๋ ํ์ง (DebugAllocator)
Zig ํ์ค ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ ์ ์ญ malloc์ ๋ชฐ๋ ํธ์ถํ์ง ์๊ณ , ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ฐ๋ ํจ์๊ฐ ๋ฐ๋์ std.mem.Allocator๋ฅผ ์ธ์๋ก ๋ฐ์ต๋๋ค.
// src/allocator_demo.zig
const std = @import("std");
pub fn main() !void {
// ๊ฐ๋ฐ/ํ
์คํธ์ฉ ๋์ ํ์ง Allocator
var gpa = std.heap.DebugAllocator(.{}){};
defer {
const check = gpa.deinit();
if (check == .leak) @panic("๋ฉ๋ชจ๋ฆฌ ๋์๊ฐ ๊ฐ์ง๋์์ต๋๋ค!");
}
const allocator = gpa.allocator();
// ๋์ ๋ฒํผ ํ ๋น ๋ฐ defer๋ก ์ค์ฝํ ์ข
๋ฃ ์ ํ์คํ ํด์
const buf = try allocator.alloc(u8, 64);
defer allocator.free(buf);
const msg = try std.fmt.bufPrint(buf, "Zig {s} Allocator OK", .{"0.17"});
std.debug.print("{s}\n", .{msg});
}
zig run src/allocator_demo.zig
# ์์ ์ถ๋ ฅ: Zig 0.17 Allocator OK (๋์ 0๊ฑด์ผ๋ก ์ ์ ์ข
๋ฃ)
[3๊ฐ] WASI & ๋ธ๋ผ์ฐ์ ์ฉ ์ด๊ฒฝ๋ WASM ๋ชจ๋ ์ปดํ์ผ
1) wasm32-freestanding (๋ธ๋ผ์ฐ์ / Node.js / ์ฃ์ง ์์ปค์ฉ)
// src/edge_hash.zig
export fn add_and_hash(a: u32, b: u32) u32 {
var x: u32 = a +% b;
x ^= x >> 16;
x *%= 0x7feb352d;
x ^= x >> 15;
return x;
}
# ReleaseSmall ์ต์ ํ๋ก ์ KB ๋จ์ .wasm ์์ฑ
zig build-exe src/edge_hash.zig \
-target wasm32-freestanding \
-O ReleaseSmall \
-fno-entry \
--export=add_and_hash
2) Node.js์์ Zig WASM ๋ชจ๋ ์ฆ์ ์คํ
// run_wasm.mjs
import fs from 'node:fs';
const bytes = fs.readFileSync('./edge_hash.wasm');
const { instance } = await WebAssembly.instantiate(bytes);
console.log('Zig WASM Hash:', instance.exports.add_and_hash(100, 200));
3) wasm32-wasi (Wasmtime ์๋ฒ/CLI ๋ฐํ์์ฉ)
zig build-exe src/comptime_demo.zig -target wasm32-wasi -O ReleaseFast
wasmtime comptime_demo.wasm
[4๊ฐ] C/Go/Rust ์ฐ๋์ ์ํ C ABI ํฌ๋ก์ค ๋น๋ (build.zig)
Zig๋ ๋ณ๋์ ํฌ๋ก์ค ์ปดํ์ผ ํด์ฒด์ธ ์์ด --target ํ๋๊ทธ๋ง์ผ๋ก Linux/macOS/Windows์ฉ C ABI ๊ณต์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ(.so, .dylib, .dll)์ ํค๋๋ฅผ ์์ฑํ ์ ์์ต๋๋ค.
# Linux x86_64์ฉ C ABI ๊ณต์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ํฌ๋ก์ค ๋น๋
zig build-lib src/edge_hash.zig -dynamic -target x86_64-linux-gnu -O ReleaseFast
ํ๋ก๋์ ์ฒดํฌ๋ฆฌ์คํธ
- ์ฃ์ง/WASM ๋ชจ๋์
-O ReleaseSmall๋ก ๋ฐ์ด๋๋ฆฌ ํฌ๊ธฐ๋ฅผ ์ต์ํํด ์ฝ๋ ์คํํธ๋ฅผ ์ค์ด์ธ์. - ํ
์คํธ ์ฝ๋(
zig test)์๋ ํญ์std.testing.allocator๋ฅผ ์ ๋ฌํด ๋จ์ ํ ์คํธ๋ง๋ค ๋ฉ๋ชจ๋ฆฌ ๋์๋ฅผ ์๋ ๊ฒ์ถํ์ธ์.
์๊ฒฌ ๋ฐ ์ง๋ฌธ
0์์ง ๋ฑ๋ก๋ ์๊ฒฌ์ด ์์ต๋๋ค. ์ฒซ ๋ฒ์งธ ๋๊ธ์ ๋จ๊ฒจ๋ณด์ธ์!
๋๊ธ ์์
๋๊ธ ์ญ์