Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>V Language Compiler Discussion</title>
- <style>
- body {
- font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif;
- line-height: 1.6;
- color: #24292e;
- max-width: 900px;
- margin: 0 auto;
- padding: 20px;
- background-color: #ffffff;
- }
- h1, h2, h3, h4 {
- color: #24292e;
- margin-top: 1.5em;
- margin-bottom: 0.8em;
- }
- h1 {
- border-bottom: 1px solid #e1e4e8;
- padding-bottom: 0.3em;
- }
- h2 {
- border-bottom: 1px solid #eaecef;
- padding-bottom: 0.3em;
- }
- pre {
- background-color: #f6f8fa;
- border-radius: 6px;
- padding: 16px;
- overflow: auto;
- font-size: 85%;
- font-family: SFMono-Regular, Consolas, 'Liberation Mono', Menlo, monospace;
- }
- code {
- font-family: SFMono-Regular, Consolas, 'Liberation Mono', Menlo, monospace;
- font-size: 85%;
- background-color: rgba(27,31,35,0.05);
- padding: 0.2em 0.4em;
- border-radius: 6px;
- }
- pre code {
- background: none;
- padding: 0;
- }
- table {
- border-collapse: collapse;
- width: 100%;
- margin: 1em 0;
- }
- th, td {
- border: 1px solid #dfe2e5;
- padding: 8px 12px;
- text-align: left;
- }
- th {
- background-color: #f6f8fa;
- font-weight: 600;
- }
- tr:nth-child(even) {
- background-color: #f6f8fa;
- }
- blockquote {
- margin: 0;
- padding: 0 1em;
- color: #6a737d;
- border-left: 0.25em solid #dfe2e5;
- }
- .timestamp {
- color: #6a737d;
- font-size: 0.9em;
- margin-top: 2em;
- text-align: right;
- font-style: italic;
- }
- </style>
- </head>
- <body>
- <h1>V Language Compiler: Output, Performance, and Comparison with GCC</h1>
- <h2>What is the output of the Vlang compiler? ASM or C?</h2>
- <p>The <strong>V programming language</strong> (Vlang) compiler can produce <strong>multiple types of output</strong>, depending on the compilation mode and target backend:</p>
- <h3>1. C code (default in early versions)</h3>
- <ul>
- <li>Historically, V <strong>transpiled to C</strong> as an intermediate step.</li>
- <li>The generated C code was then compiled using a C compiler (like <code>gcc</code> or <code>clang</code>) to produce a native binary.</li>
- <li>This is still supported and can be explicitly requested with:
- <pre><code>v -o program.c program.v</code></pre>
- </li>
- </ul>
- <h3>2. Native machine code (via direct compilation or x64 backend)</h3>
- <ul>
- <li>Starting around <strong>V 0.2+</strong>, the V compiler introduced a <strong>native backend</strong> that can <strong>emit x86_64 assembly directly</strong> (without going through C).</li>
- <li>This backend can generate <strong>assembly (.s)</strong> or <strong>directly produce an executable</strong>.</li>
- <li>On supported platforms (mainly x86_64 Linux/macOS/Windows), V now <strong>prefers its native backend by default</strong>, bypassing C entirely for faster compilation.</li>
- <li>You can force native compilation with:
- <pre><code>v -native program.v</code></pre>
- </li>
- </ul>
- <h3>3. LLVM IR (experimental)</h3>
- <ul>
- <li>There is also <strong>experimental support for an LLVM backend</strong>, which would emit LLVM IR, but this is not the default and is still under development.</li>
- </ul>
- <h3>Summary</h3>
- <table>
- <thead>
- <tr>
- <th>Backend</th>
- <th>Output</th>
- <th>Default?</th>
- <th>Status</th>
- </tr>
- </thead>
- <tbody>
- <tr>
- <td>C backend</td>
- <td>C source code</td>
- <td>No (legacy)</td>
- <td>Fully supported</td>
- </tr>
- <tr>
- <td>Native backend</td>
- <td>x86_64 assembly → executable</td>
- <td><strong>Yes</strong> (on x64)</td>
- <td>Primary path</td>
- </tr>
- <tr>
- <td>LLVM backend</td>
- <td>LLVM IR</td>
- <td>No</td>
- <td>Experimental</td>
- </tr>
- </tbody>
- </table>
- <blockquote>
- <p>✅ <strong>As of V 0.4+ (2023–2025)</strong>, the <strong>default output is a native executable</strong>, generated via V’s <strong>own x64 code generator</strong> (which internally produces and assembles x86_64 assembly). The C backend remains available for portability (e.g., compiling to architectures not yet supported by the native backend, like ARM).</p>
- </blockquote>
- <p>You can check what your V compiler is doing by using:
- <pre><code>v -show-coutput program.v # shows generated C (if using C backend)
- v -show-asm program.v # shows generated assembly (if using native backend)</code></pre>
- </p>
- <h2>Is the speed of V 0.4 the same as V 0.2?</h2>
- <p>Great question! The <strong>speed of V 0.4 vs. V 0.2</strong> can differ in <strong>two distinct dimensions</strong>, and it’s important to clarify which one you mean:</p>
- <h3>1. Compilation Speed (how fast <code>v</code> compiles your code)</h3>
- <ul>
- <li><strong>V 0.4 is significantly faster</strong> at compiling than V 0.2.</li>
- <li>The V compiler was already designed for speed, but between 0.2 and 0.4:
- <ul>
- <li>The <strong>native x64 backend</strong> matured and became the default (no more C intermediate step on x86_64), eliminating the overhead of calling <code>gcc</code>/<code>clang</code> and parsing/translating to C.</li>
- <li>Improvements in parsing, hashing, and module caching reduced redundant work.</li>
- <li>The compiler became <strong>self-hosted</strong> (written in V and bootstrapped), allowing tighter optimization.</li>
- </ul>
- </li>
- <li><strong>Result</strong>: In V 0.4, hello-world compiles in <strong>~1–15 ms</strong> on modern hardware, and large projects compile <strong>~2–5× faster</strong> than in V 0.2 (especially when the C backend was used).</li>
- </ul>
- <h3>2. Runtime Performance (how fast the generated program runs)</h3>
- <ul>
- <li>This is more nuanced.</li>
- <li><strong>V 0.2</strong> relied heavily on the <strong>C backend</strong>, so runtime performance depended on the quality of the generated C and the optimizations of <code>gcc</code>/<code>clang</code>.</li>
- <li><strong>V 0.4</strong> uses a <strong>handwritten native x64 code generator</strong>, which:
- <ul>
- <li>Produces <strong>simple, correct, but not highly optimized</strong> assembly by default.</li>
- <li><strong>Does not yet include advanced optimizations</strong> like instruction scheduling, vectorization, or sophisticated register allocation found in LLVM/GCC.</li>
- <li>However, it avoids C runtime overhead and uses a lean runtime model (no GC by default, bounds checking can be disabled).</li>
- </ul>
- </li>
- <li><strong>Net effect</strong>:
- <ul>
- <li>For <strong>simple, low-level code</strong>, V 0.4 <strong>native output is often comparable</strong> to C (within ~5–15%).</li>
- <li>For <strong>compute-intensive or complex code</strong>, V 0.2 (via <code>-prod</code> + <code>clang -O2</code>) could sometimes outperform V 0.4’s native backend—because <code>clang</code> applies aggressive optimizations that V’s native backend doesn’t yet implement.</li>
- <li>But: V 0.4 <strong>can still use the C backend</strong> (<code>v -c</code>), so you can get <strong>identical (or better) runtime performance</strong> to V 0.2 if needed.</li>
- </ul>
- </li>
- </ul>
- <blockquote>
- <p>🔧 Tip: To maximize runtime speed in V 0.4, use:
- <pre><code>v -prod -c program.v # uses C backend + aggressive optimizations</code></pre>
- This often matches or exceeds C performance.
- </p>
- </blockquote>
- <h3>Summary</h3>
- <table>
- <thead>
- <tr>
- <th>Aspect</th>
- <th>V 0.2</th>
- <th>V 0.4</th>
- <th>Verdict</th>
- </tr>
- </thead>
- <tbody>
- <tr>
- <td><strong>Compilation speed</strong></td>
- <td>Fast (via C)</td>
- <td><strong>Much faster</strong> (native backend by default)</td>
- <td>✅ <strong>V 0.4 wins</strong></td>
- </tr>
- <tr>
- <td><strong>Runtime speed</strong></td>
- <td>Good (thanks to GCC/Clang)</td>
- <td>Good (native), <strong>or better</strong> (with <code>-c -prod</code>)</td>
- <td>↔️ <strong>Comparable or better in V 0.4</strong></td>
- </tr>
- </tbody>
- </table>
- <h2>V 0.4 compared to <code>gcc -O2</code>?</h2>
- <p>Great follow-up! Comparing <strong>V 0.4</strong> (with its default native backend) to <strong><code>gcc -O2</code></strong> (a mature, highly optimized C compiler) reveals important trade-offs in <strong>runtime performance</strong>, <strong>binary size</strong>, and <strong>optimization maturity</strong>.</p>
- <blockquote>
- <p>🔍 <strong>Short Answer</strong>:<br>
- <strong>For most real-world programs, <code>gcc -O2</code> still produces faster code than V 0.4’s native backend.</strong><br>
- However, <strong>V 0.4 using its C backend (<code>v -c -prod</code>) can match or nearly match <code>gcc -O2</code> performance</strong>, because it <em>uses</em> <code>gcc -O2</code> (or <code>clang -O2</code>) under the hood.
- </p>
- </blockquote>
- <h3>1. V 0.4 (native backend) vs. <code>gcc -O2</code></h3>
- <ul>
- <li><strong>V’s native backend</strong> (default on x86_64) generates x86_64 assembly directly.
- <ul>
- <li>✅ <em>Pros</em>: Extremely fast compilation; no external deps; minimal runtime.</li>
- <li>❌ <em>Cons</em>: <strong>Limited optimizations</strong> — basic register allocation, no loop unrolling, no vectorization (SIMD), no advanced constant propagation, no inter-procedural analysis.</li>
- </ul>
- </li>
- <li><strong><code>gcc -O2</code></strong>:
- <ul>
- <li>Applies <strong>decades of compiler research</strong>: instruction scheduling, dead code elimination, function inlining, auto-vectorization (sometimes), profile-guided optimization readiness, etc.</li>
- </ul>
- </li>
- <li><strong>Performance gap</strong>:
- <ul>
- <li>On <strong>numeric kernels</strong> (e.g., matrix multiply, FFT, Mandelbrot): <code>gcc -O2</code> is often <strong>1.5× to 3× faster</strong>.</li>
- <li>On <strong>I/O-bound or simple logic</strong> (e.g., parsing, file copying): difference is <strong>negligible (<10%)</strong>.</li>
- <li>On <strong>memory-heavy code</strong>: V may suffer due to less optimal layout or lack of alias analysis.</li>
- </ul>
- </li>
- </ul>
- <blockquote>
- <p>📌 Example: In the <a href="https://github.com/vlang/v/tree/master/benchmarks">V benchmarks</a>, the native backend is typically <strong>10–40% slower</strong> than <code>gcc -O2</code> on CPU-intensive tasks.</p>
- </blockquote>
- <h3>2. V 0.4 (C backend + <code>-prod</code>) vs. <code>gcc -O2</code></h3>
- <p>When you compile with:
- <pre><code>v -c -prod program.v</code></pre>
- V:
- <ul>
- <li>Generates clean, human-readable C code.</li>
- <li>Invokes <code>gcc -O2</code> (or <code>clang -O2</code>) with <strong>aggressive flags</strong> (<code>-ffast-math</code>, <code>-march=native</code>, etc. in <code>-prod</code> mode).</li>
- <li>Disables safety checks (bounds, overflow, etc.).</li>
- </ul>
- </p>
- <blockquote>
- <p>✅ <strong>Result</strong>: The generated binary is <strong>often within 1–5% of hand-written C compiled with <code>gcc -O2</code></strong>—sometimes even <strong>faster</strong>, because V avoids heap allocations where C might not.</p>
- </blockquote>
- <blockquote>
- <p>📌 Real-world note: Projects like <strong>V’s <code>vpm</code> (package manager)</strong> or <strong><code>vweb</code></strong> show near-identical performance between hand-tuned C and V (via C backend).</p>
- </blockquote>
- <h3>3. Binary Size & Memory Use</h3>
- <ul>
- <li><strong>V native</strong>: Very small binaries (often <100 KB for hello-world), no libc dependency in freestanding mode.</li>
- <li><strong>V via C + gcc -O2</strong>: Slightly larger due to C runtime, but still lean.</li>
- <li><strong>Memory safety</strong>: V enables bounds checks by default (slight overhead); disable with <code>-prod</code> or <code>-skip-unused-checks</code>.</li>
- </ul>
- <h3>Summary Table</h3>
- <table>
- <thead>
- <tr>
- <th>Configuration</th>
- <th>Runtime Speed vs. <code>gcc -O2</code></th>
- <th>Compilation Speed</th>
- <th>Notes</th>
- </tr>
- </thead>
- <tbody>
- <tr>
- <td><strong>V 0.4 (native backend)</strong></td>
- <td><strong>~60–90%</strong> of <code>gcc -O2</code></td>
- <td>⚡ Extremely fast</td>
- <td>Best for dev builds, scripts</td>
- </tr>
- <tr>
- <td><strong>V 0.4 (<code>-c -prod</code>)</strong></td>
- <td><strong>~95–102%</strong> of <code>gcc -O2</code></td>
- <td>Fast (~1–2× slower than native)</td>
- <td>Best for release builds</td>
- </tr>
- <tr>
- <td><strong>Hand-written C + <code>gcc -O2</code></strong></td>
- <td>Baseline (100%)</td>
- <td>Slower than V</td>
- <td>Gold standard for performance</td>
- </tr>
- </tbody>
- </table>
- <h3>Outlook</h3>
- <p>The V team is actively improving the native backend:</p>
- <ul>
- <li>Adding <strong>SSA-based IR</strong> (planned)</li>
- <li>Implementing <strong>scalar replacement, LICM, basic vectorization</strong></li>
- <li>But <strong>full parity with GCC/LLVM</strong> is years away — and may never be the goal, since V can <strong>leverage existing C toolchains</strong> when needed.</li>
- </ul>
- <h3>Recommendation</h3>
- <ul>
- <li>Use <strong><code>v -c -prod</code></strong> for production binaries where performance matters.</li>
- <li>Use <strong>native backend</strong> for development (blazing-fast iteration).</li>
- <li>Don’t compare V’s <em>default</em> output to <code>gcc -O2</code> — compare <strong><code>v -c -prod</code></strong> instead.</li>
- </ul>
- <div class="timestamp">
- Exported on Friday, December 12, 2025
- </div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment