कंपाइलर और इंटरप्रेटर · पार्ट 29 / 36
Bytecode और virtual machines
एक्सपर्ट 18 मिनट
Interpreter bytecode में compile क्यों करते हैं, stack-based VM उसे कैसे चलाती है, और CPython के adaptive interpreter (3.11+) ने क्या बदला।
तुम क्या सीखोगे
- तुम समझा सकते हो कि bytecode चलाना tree पर चलने से तेज़ क्यों है।
- तुम stack machine पर एक छोटे program को कदम-दर-कदम trace कर सकते हो।
- तुम CPython के specialising adaptive interpreter और प्रायोगिक JIT का वर्णन कर सकते हो।
इससे पहले: टेक्स्ट से ट्री तक: lexing और parsing
सीधे tree पर ही क्यों न चलें?
Tree-walking interpreter सरल होता है, पर धीमा: हर node memory में कहीं पड़ा एक object है, और हर कदम pointers का पीछा करता है और recursive calls करता है। ज़्यादातर production interpreters पहले AST को bytecode में compile करते हैं, यानी सरल instructions की एक compact, linear सूची, और फिर उसे एक virtual machine (VM) में चलाते हैं। Python (CPython), Ruby, Lua, Java और C# (JVM/.NET bytecode के रूप में), और JavaScript engines (V8 का Ignition), सभी यही करते हैं।

Stack machine
ज़्यादातर bytecode VMs stack machines होती हैं: instructions अपने operands एक value stack से लेती हैं और परिणाम वापस उसी पर push करती हैं।
price * (1 + tax) इसमें compile होता है:
LOAD price stack: [price]
CONST 1 stack: [price, 1]
LOAD tax stack: [price, 1, tax]
ADD stack: [price, 1+tax]
MUL stack: [price*(1+tax)]
VM एक बड़े switch (या handlers की एक table) के चारों ओर बना एक loop है: अगली instruction लाओ, उसके opcode के आधार पर dispatch करो, उसे execute करो। कोई recursion नहीं, अच्छी memory locality, और हर instruction छोटी होती है।
तुम असली CPython bytecode देख सकते हो:
import dis
dis.dis("price * (1 + tax)")
सटीक opcodes Python के versions के बीच बदलते रहते हैं। bytecode एक आंतरिक format है, कोई स्थिर interface नहीं।
Register machines
कुछ VMs (5.0 से Lua, Android का Dalvik/ART bytecode, और V8 का Ignition, जो एक accumulator के साथ registers इस्तेमाल करता है) अपने operands को virtual registers के रूप में नाम देती हैं: ADD r3, r1, r2। इसमें कम instructions लगती हैं, पर हर instruction बड़ी होती है। दोनों design काम करते हैं। चुनाव dispatch की लागत पर निर्भर करता है।
Dynamic languages को तेज़ बनाना कठिन है
a + b में Python नहीं जानता कि a एक int है, float है, string है या __add__ वाला कोई custom object। हर ADD को run time पर types जाँचने पड़ते हैं। यही व्यापकता सबसे बड़ी लागत है, लेकिन असली programs में कोई एक line आम तौर पर बार-बार वही types देखती है।
CPython का specialising adaptive interpreter
Python 3.11 (अक्टूबर 2022) ने PEP 659 पेश किया: interpreter हर instruction पर नज़र रखता है और कुछ बार execute होने के बाद उसे उसी जगह पर एक specialised version में बदल देता है। उदाहरण के लिए, जब BINARY_OP को बार-बार दो ints मिलते हैं, तो वह BINARY_OP_ADD_INT बन जाता है। Specialised instruction अपनी धारणा को सस्ते में जाँचती है और गलत होने पर fallback कर लेती है। दूसरे सुधारों (सस्ते frames, zero-cost exception setup) के साथ मिलकर Python 3.11 मानक pyperformance benchmarks पर 3.10 से औसतन लगभग 25 % तेज़ था। असली फ़र्क काफ़ी हद तक workload पर निर्भर करता है।
और एक JIT
Python 3.13 (अक्टूबर 2024) ने एक प्रायोगिक JIT compiler (PEP 744) जोड़ा। यह default रूप से बंद रहता है और "copy-and-patch" तकनीक इस्तेमाल करता है: हर micro-instruction के लिए machine-code templates CPython के build के समय तैयार किए जाते हैं और run time पर जोड़ दिए जाते हैं। Python 3.14 तक यह प्रायोगिक ही है और workload के हिसाब से थोड़ा-बहुत फ़ायदा देता है। PyPy, Python का एक वैकल्पिक implementation, कई सालों से tracing JIT रखता है और शुद्ध Python loops पर अक्सर कई गुना तेज़ होता है।
Ahead-of-time बनाम just-in-time
- AOT compilation (C, Rust, Go) program चलने से पहले ही machine code बना देता है: तेज़ शुरुआत, अनुमान लगाने योग्य गति, पर run-time types या data की कोई जानकारी नहीं।
- JIT compilation program चलते समय compile करता है और जो देखता है उसका इस्तेमाल करता है: यह उन types के लिए specialise कर सकता है जो वास्तव में आते हैं। यही अगले पाठ का विषय है।
मुख्य बातें
- Production interpreters bytecode में compile करते हैं और उसे एक VM loop में चलाते हैं।
- Stack machines operands को push और pop करती हैं, register machines उन्हें नाम देती हैं।
dis.dis()CPython का bytecode दिखाता है, जो एक आंतरिक और version-विशिष्ट format है।- Python 3.11 का adaptive interpreter देखे गए types के लिए instructions को specialise करता है (PEP 659)।
- Python 3.13 ने एक प्रायोगिक copy-and-patch JIT (PEP 744) जोड़ा, जो 3.14 में भी प्रायोगिक है।
फ़्लैशकार्ड
- प्रश्न: Bytecode tree-walking से तेज़ क्यों है?
- उत्तर: यह instructions की एक compact linear सूची है: हर node के लिए न pointers का पीछा करना पड़ता है, न recursion।
- प्रश्न: Stack machine 1 + tax की गणना कैसे करती है?
- उत्तर: 1 को push करो, tax को push करो, ADD दोनों को pop करके योग push करता है।
- प्रश्न: PEP 659 क्या करता है?
- उत्तर: Run time पर instructions को type-specialised versions में बदल देता है (adaptive interpreter, Python 3.11)।
- प्रश्न: किस Python version ने प्रायोगिक JIT जोड़ा?
- उत्तर: Python 3.13 (PEP 744)।
खुद को जाँचो
पहले अपना जवाब सोचो, फिर उसे खोलो। ऐप में क्विज़ इंटरैक्टिव है, पॉइंट देता है और जो छूट गया उसे फिर से सामने लाता है।
2 * (3 + 4) के लिए stack-machine instructions को सही क्रम में लगाओ।
उत्तर दिखाओ
CONST 2 → CONST 3 → CONST 4 → ADD → MUL
पहले operands को stack पर push किया जाता है; ADD 3 और 4 को जोड़ता है; MUL 2 और 7 को गुणा करता है।
स्रोत: Crafting Interpreters, ch. 15
CONST 2, CONST 3, CONST 4, ADD, MUL के बाद stack पर कौन-सा मान बचता है?
उत्तर दिखाओ
14
3 + 4 = 7, फिर 2 × 7 = 14।
स्रोत: Crafting Interpreters, ch. 15
CPython का specialising adaptive interpreter क्या करता है?
- generic instructions को देखने के बाद उन्हें type-specialised instructions से बदल देता है
- चलाने से पहले Python को C में compile करता है
- GIL को हटा देता है
- compile time पर types की जाँच करता है
उत्तर दिखाओ
generic instructions को देखने के बाद उन्हें type-specialised instructions से बदल देता है
PEP 659 देखे गए types के आधार पर instructions को quicken करता है, सस्ते guards के साथ।
स्रोत: PEP 659
2026 के अंत तक Python के JIT के बारे में कौन-से कथन सही हैं?
- इसे Python 3.13 में जोड़ा गया था
- यह copy-and-patch का उपयोग करता है
- यह default रूप से चालू है और सारे code को 10× तेज़ बना देता है
- इसे अब भी experimental बताया जाता है
उत्तर दिखाओ
इसे Python 3.13 में जोड़ा गया था; यह copy-and-patch का उपयोग करता है; इसे अब भी experimental बताया जाता है
PEP 744: experimental, default रूप से बंद, मामूली फ़ायदे।
स्रोत: PEP 744 / What’s New in Python 3.14
स्रोत
- Crafting Interpreters, Robert Nystrom, Genever Benning, 2021 (free online)
- PEP 659 – Specializing Adaptive Interpreter, Mark Shannon, 2021
- Python documentation: dis — Disassembler for Python bytecode, Python Software Foundation, 2025
तथ्य 2026-11-30 को जाँचे गए। Skillgrove पाठ मुफ़्त सीखने की सामग्री हैं — कोई सर्टिफ़िकेट या मान्यता नहीं।