<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Harshit Sinha on Medium]]></title>
        <description><![CDATA[Stories by Harshit Sinha on Medium]]></description>
        <link>https://medium.com/@hsinha610?source=rss-8da3b4d8e24e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*diWFIgEhHC_imJffATHXSQ.png</url>
            <title>Stories by Harshit Sinha on Medium</title>
            <link>https://medium.com/@hsinha610?source=rss-8da3b4d8e24e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 23:17:38 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@hsinha610/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[The Java Virtual Machine Has No Idea Generics Exist]]></title>
            <link>https://medium.com/@hsinha610/the-java-virtual-machine-has-no-idea-generics-exist-1b5260154347?source=rss-8da3b4d8e24e------2</link>
            <guid isPermaLink="false">https://medium.com/p/1b5260154347</guid>
            <category><![CDATA[generics]]></category>
            <category><![CDATA[type-erasure]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[java-virtual-machine]]></category>
            <dc:creator><![CDATA[Harshit Sinha]]></dc:creator>
            <pubDate>Tue, 06 Oct 2026 21:54:45 GMT</pubDate>
            <atom:updated>2026-10-06T21:54:45.357Z</atom:updated>
            <content:encoded><![CDATA[<h4>How Java tricked its own Virtual Machine using Generics &amp; Type Erasure to bring compile-time type safety to billions of devices and why Kotlin had to invent reified parameters to work around it.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*WhP5C16zPrVtXKPk" /><figcaption>Photo by <a href="https://unsplash.com/@sebastiansvenson?utm_source=medium&amp;utm_medium=referral">Sebastian Svenson</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Every modern JVM developer relies on generics daily — Repository&lt;T&gt;, ApiResponse&lt;T&gt;, Flow&lt;T&gt;. It feels clean, natural, and safe.</p><p>Until runtime hits you with an illogical boundary:</p><ul><li>if (value is List&lt;String&gt;) fails in Kotlin due to type erasure.</li><li>List&lt;String&gt;::class and List&lt;Int&gt;::class resolve to the exact same class at runtime.</li></ul><p>Why do generics even exist if the JVM erases them during execution?</p><p>Because JVM generics are governed by two decisions: <strong>a compile-time safety contract</strong> and <strong>a runtime compatibility constraint</strong>.</p><h3>1. Why Do We Have Generics? (The Compile-Time Problem)</h3><p>Before Java 5 (released in 2004), collections had no concept of contained types. Arrays were typed, but collections and caches operated purely on raw java.lang.Object references.</p><p>Consider how an in-memory cache or state manager had to store data in Java 1.4:</p><pre>// Java 1.4 legacy collection usage<br>List userSessions = new ArrayList();<br><br>userSessions.add(new UserSession(&quot;usr_1092&quot;));<br><br>userSessions.add(Integer.valueOf(42)); // Perfectly valid at compile time!<br><br><br>// Retrieval required explicit downcasting<br>for (int i = 0; i &lt; userSessions.size(); i++) {<br>    UserSession session = (UserSession) userSessions.get(i); <br>    System.out.println(session.getUserId());<br>}</pre><p>This design created two massive failure modes:</p><ul><li><strong>Pervasive Downcasting:</strong> Callers were forced to manually downcast every object retrieved from a data structure.</li><li><strong>Deferred Production Crashes:</strong> Mismatched data passed compilation silently. In the code above, index 1 contained an Integer. The compiler raised no flags, but during execution, calling (UserSession) userSessions.get(1) threw a fatal ClassCastException.</li></ul><h3>The Core Goal of Generics</h3><p>Generics were <strong>not</strong> created to save keystrokes or make code look aesthetic. <strong>They were created to move error detection from runtime to compile time.</strong></p><p>By establishing a type contract between the developer and the compiler (List&lt;UserSession&gt;), the compiler gains the authority to block invalid insertions before your application ever reaches production.</p><pre>List&lt;UserSession&gt; userSessions = new ArrayList&lt;&gt;();<br><br>userSessions.add(new UserSession(&quot;usr_1092&quot;));<br><br>userSessions.add(42); // COMPILE ERROR: Incompatible types</pre><h3>2. Why Type Erasure? (The Runtime Compromise)</h3><p>If generics are so critical for type safety, why does the JVM strip them away at runtime?</p><p>When Sun Microsystems set out to add generics in Java 5, Java was already the foundation of global enterprise software. Millions of applications, application servers, and third-party libraries were running compiled Java bytecode across the world.</p><p>Language architects faced a critical fork in the road:</p><ul><li><strong>Path A: Reified Generics (Full Runtime Awareness).</strong> Retain generic arguments inside class metadata at runtime (e.g., List&lt;String&gt; and List&lt;Integer&gt; become distinct runtime types). C# took this path when introducing generics in .NET 2.0. However, doing this on the JVM would have required modifying the JVM instruction set, breaking binary compatibility across existing compiled JAR files, and forcing every enterprise application to be recompiled.</li><li><strong>Path B: Type Erasure (Compile-Time Illusion).</strong> Enforce type safety strictly at compile time, but wipe away generic parameters during compilation, emitting plain bytecode that legacy JVMs could execute without modification.</li></ul><p>To preserve complete backward compatibility, Java chose <strong>Path B</strong>. Legacy code could call new generic APIs, and new generic libraries could consume old JARs seamlessly.</p><h3>3. How Type Erasure Transforms Your Bytecode</h3><p>Under type erasure, generic parameters exist purely as a compiler contract. Before emitting bytecode, the compiler verifies type safety and then <strong>erases</strong> generic parameters, replacing them with their upper bound (usually Object).</p><p>Consider a generic wrapper class:</p><pre>public class NetworkResult&lt;T&gt; {<br>    private final T data;<br>    private final int statusCode;<br><br>public NetworkResult(T data, int statusCode) {<br>        this.data = data;<br>        this.statusCode = statusCode;<br>    }<br><br>    public T getData() {<br>        return data;<br>    }<br>}</pre><p>After compilation, the JVM sees bytecode equivalent to this erased class:</p><pre>public class NetworkResult {<br>    private final Object data; // Erased to Object<br>    private final int statusCode;<br><br>public NetworkResult(Object data, int statusCode) {<br>        this.data = data;<br>        this.statusCode = statusCode;<br>    }<br>    public Object getData() {<br>        return data;<br>    }<br>}</pre><h3>How synthetic casts maintain safety</h3><p>If the class compiles down to Object, how does generic code remain type-safe when you call result.getData()?</p><p>The compiler automatically inserts <strong>synthetic downcasts</strong> at the call site:</p><pre>// What you write:<br><br>NetworkResult&lt;UserProfile&gt; result = client.fetchProfile();<br>UserProfile profile = result.getData();<br><br><br>// What the compiler actually emits in bytecode:<br><br>NetworkResult result = client.fetchProfile();<br>UserProfile profile = (UserProfile) result.getData(); // Synthetic cast inserted by compiler</pre><p>Generics do not change how the JVM executes code at runtime. They simply automate the casting process and guarantee that those casts will never fail at runtime because the compiler already validated the types upfront.</p><p>This explains why runtime type checks yield surprising results:</p><pre>NetworkResult&lt;UserProfile&gt; profileResult = new NetworkResult&lt;&gt;(new UserProfile(), 200);<br>NetworkResult&lt;OrderHistory&gt; orderResult = new NetworkResult&lt;&gt;(new OrderHistory(), 200);<br><br>// Evaluates to TRUE because both share NetworkResult.class at runtime<br>System.out.println(profileResult.getClass() == orderResult.getClass());</pre><h3>4. Kotlin Inherits the JVM Constraint</h3><p>Because Kotlin compiles directly to JVM bytecode to ensure 100% interoperability with Java, standard Kotlin generics are bound by the exact same erasure rules.</p><p>In Kotlin, this manifests as a strict compiler boundary:</p><pre>sealed interface SystemEvent {<br>    data class PayloadReceived&lt;T&gt;(val payload: T) : SystemEvent<br>}<br><br>fun handleEvent(event: SystemEvent) {<br>    // COMPILE ERROR: Cannot check for instance of erased type: PayloadReceived&lt;String&gt;<br>    if (event is SystemEvent.PayloadReceived&lt;String&gt;) {<br>        println(&quot;Processing string payload&quot;)<br>    }<br>}</pre><p>At runtime, the JVM only knows that event is an instance of PayloadReceived. It has no record of whether it was instantiated with String, Int, or a domain model.</p><p>You can only perform a <strong>star-projection check</strong> (PayloadReceived&lt;*&gt;) to verify the outer instance:</p><pre>if (event is SystemEvent.PayloadReceived&lt;*&gt;) {<br>    // Valid! Verifies it is a PayloadReceived, but element type is unknown.<br>}</pre><h3>5. Kotlin’s Escape Hatch: inline + reified</h3><p>To work around JVM type erasure without altering runtime rules, Kotlin introduced <strong>inlined reified parameters</strong>.</p><p>In standard Java or Kotlin, if you write a JSON deserializer or dependency lookup function, you are forced to pass the explicit .class token as a parameter because T is erased inside the function body:</p><pre>// Java/Standard Kotlin approach — requires passing Class&lt;T&gt; manually<br>fun &lt;T&gt; parseJson(json: String, clazz: Class&lt;T&gt;): T {<br>    return objectMapper.readValue(json, clazz)<br>}<br><br>// Call site boilerplate:<br>val user = parseJson(jsonPayload, UserProfile::class.java)</pre><p>Because T is erased inside parseJson, the JVM cannot evaluate T::class.java on its own.</p><h3>The reified Solution</h3><p>When a function is marked as inline, the Kotlin compiler avoids generating a standard JVM function call. Instead, it substitutes the actual body of the function directly at every call site.</p><p>Because the concrete type argument is known at the exact location where the function is called, combining inline with the reified keyword allows the compiler to access T::class.java directly:</p><pre>// Inline + Reified eliminates class parameter boilerplate<br>inline fun &lt;reified T&gt; String.parseJson(objectMapper: ObjectMapper): T {<br>    // T::class.java is accessible ONLY because T is reified in an inline function!<br>    return objectMapper.readValue(this, T::class.java)<br>}<br><br>// Clean call site:<br>val user: UserProfile = jsonPayload.parseJson(objectMapper)</pre><h3>What the compiler emits at the call site</h3><p>Under the hood, the compiler inlines the function implementation directly into the caller’s code, replacing T::class.java with the concrete class known at that specific line:</p><pre>// Emitted bytecode at the call site:<br>val user: UserProfile = objectMapper.readValue(jsonPayload, UserProfile::class.java) as UserProfile</pre><h3>The Important Distinction</h3><p>reified <strong>does not disable type erasure on the JVM.</strong></p><p>It is a clever compilation technique. By copying the function implementation directly into the caller’s scope, the compiler eliminates the generic abstraction altogether and bakes direct, concrete class references and type checks straight into the generated bytecode.</p><h3>Summary</h3><ul><li><strong>Generics (Compile Time):</strong> A compile-time safety contract. They exist to catch type mismatches before code runs, eliminating manual downcasting and preventing ClassCastException crashes in production.</li><li><strong>Type Erasure (Runtime):</strong> A runtime backward-compatibility strategy. The compiler verifies generic types and then strips them away before emitting bytecode, substituting Object and synthetic downcasts so legacy and modern JVM binaries interoperate seamlessly.</li><li><strong>Kotlin </strong><strong>reified (Call-Site / Compiler):</strong> A compiler trick enabled by inline functions. It doesn&#39;t alter JVM runtime rules; instead, it copies the function body directly into the caller&#39;s scope, allowing the compiler to hardcode concrete class references and instanceof checks at every call site.</li></ul><p>👏🏻👏🏻 <strong>Your claps help this article reach more readers.</strong></p><p><strong>Thanks for making it to the end — hope you found something useful here.</strong></p><p><strong>Stay curious. 💡</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1b5260154347" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Anatomy of an Open-Source AI Model]]></title>
            <link>https://medium.com/@hsinha610/the-anatomy-of-an-open-source-ai-model-2491e1acf53e?source=rss-8da3b4d8e24e------2</link>
            <guid isPermaLink="false">https://medium.com/p/2491e1acf53e</guid>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[open-source]]></category>
            <category><![CDATA[hugging-face]]></category>
            <category><![CDATA[large-language-models]]></category>
            <dc:creator><![CDATA[Harshit Sinha]]></dc:creator>
            <pubDate>Tue, 22 Sep 2026 20:46:27 GMT</pubDate>
            <atom:updated>2026-09-22T20:46:27.692Z</atom:updated>
            <content:encoded><![CDATA[<h4>A developer-friendly guide to understanding open-source AI models</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Z5pqOnpBkXO44MeL" /><figcaption>Photo by <a href="https://unsplash.com/@sebastiansvenson?utm_source=medium&amp;utm_medium=referral">Sebastian Svenson</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>When you first start exploring open-source AI models, the terminology hits like a wall of random technical jargon. You open a repository on Hugging Face and see filenames like this:</p><p>Qwen3-TTS-0.6B-Q4_K_M.gguf</p><p>Before long, you’re bombarded with terms like <strong>parameters, weights, FP16, BF16, quantization, Safetensors, GGUF, tokenizers, runtimes, LoRA, </strong>and<strong> distillation.</strong></p><p>It feels like you need a degree in machine learning just to run a model locally. You don’t.</p><p>To evaluate and run open-source AI, you simply need to understand how a model flows from a concept on paper down to the hardware on your desk:</p><pre>Model ➔ Parameters ➔ Precision &amp; Quantization ➔ Format ➔ Runtime ➔ Backend ➔ Hardware</pre><p>Here is the complete, practical guide to breaking down every layer of that stack.</p><h3>1. What Is a Model?</h3><p>At its core, an AI model is a neural network whose parameters have been learned during training.</p><p>When people talk about AI, they often refer to <strong>model families</strong> — lineages of models created by specific research teams or companies:</p><ul><li><strong>Llama</strong> (Meta)</li><li><strong>Qwen</strong> (Alibaba)</li><li><strong>Gemma</strong> (Google)</li><li><strong>DeepSeek</strong> (DeepSeek AI)</li><li><strong>Mistral</strong> (Mistral AI)</li><li><strong>Whisper</strong> (OpenAI)</li></ul><p>A single model family usually contains variants optimized for drastically different tasks:</p><pre>Qwen<br>├── Qwen3         ──► General Text Generation (LLM)<br>├── Qwen3-Coder   ──► Code Generation<br>├── Qwen3-VL      ──► Vision + Language (Multimodal)<br>└── Qwen3-TTS     ──► Text-to-Speech</pre><p>Before looking at file sizes or runtimes, the first question you should always ask is simple: <strong>What is this model designed to do?</strong></p><h3>2. Parameters vs. Weights</h3><p>The distinction between parameters and weights is one of the most common sources of confusion for beginners.</p><h3>Parameters</h3><p>A <strong>parameter</strong> is any learned numerical value inside a neural network. During training, the model adjusts these values iteratively so it gets better at its task. A tiny model might be composed of numbers like [0.12, -0.42, 0.87, 0.31].</p><p>When a model is labeled <strong>7B</strong>, it means it contains roughly <strong>7 billion learned numerical values</strong>.</p><h3>Weights</h3><p>A <strong>weight</strong> is a specific type of parameter. Neural networks use weights in large matrices to determine how strongly information from one neuron influences another.</p><pre>Input Signal<br>  │<br>  ├── × weight (0.8)  ──┐<br>  ├── × weight (-0.2) ──┼──► Output Neuron<br>  └── × weight (0.4)  ──┘</pre><p>Besides weights, models also contain other learned parameters, such as <strong>biases</strong> (which offset activation thresholds) and <strong>normalization parameters</strong>.</p><pre>All Learned Parameters<br>├── Weights (The vast majority)<br>├── Biases<br>└── Normalization Parameters</pre><blockquote><strong><em>The Rule:</em></strong><em> All weights are parameters, but not all parameters are weights. In modern large language models, weights make up over 99% of all parameters — which is why the terms are often used interchangeably in casual conversation.</em></blockquote><h3>3. Model Size</h3><p>Model parameter counts are typically abbreviated in <strong>Billions (B)</strong>:</p><ul><li><strong>0.6B ~ </strong>600 Million parameters</li><li><strong>7B</strong> ~ 7 Billion parameters</li><li><strong>70B </strong>~ 70 Billion parameters</li></ul><p>Parameter count gives you a rough idea of the model’s overall capacity and memory footprint. However, <strong>more parameters do not automatically mean better performance</strong>.</p><p>A model’s real-world capability depends heavily on:</p><ul><li>Architecture efficiency</li><li>Quality and diversity of training data</li><li>Fine-tuning and post-training methods</li><li>Domain specialization</li></ul><p>A smaller 7B model fine-tuned specifically on legal contracts will consistently outperform a general-purpose 70B model on legal analysis.</p><h3>4. Precision</h3><p>Once you know a model has 7 billion parameters, the next question is: <strong>how are those individual numbers stored in memory?</strong></p><p>Computers can store numbers using different levels of numerical precision (bits):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kUunI4TfhNrmQ_LJtaGALg.png" /></figure><p>Changing the precision changes the memory requirement without changing the parameter count:</p><ul><li><strong>7B Model at FP32:</strong> 7B x 4 bytes ~ 28 GB RAM</li><li><strong>7B Model at FP16:</strong> 7B x 2 bytes ~ 14 GB RAM</li><li><strong>7B Model at 4-bit:</strong> 7B x 0.5 bytes ~ 3.5 GB RAM</li></ul><h3>5. Quantization</h3><p><strong>Quantization</strong> is the process of compressing model parameters by converting them from higher precision (like FP16) to lower precision (like INT8 or INT4)</p><pre>FP16 (14 GB) ──► Q8 (7 GB) ──► Q6 (5.2 GB) ──► Q4 (3.5 GB)<br>(Higher Accuracy / More RAM)  ──────────────►  (Slightly Lower Accuracy / Less RAM)</pre><p>Quantization introduces a trade-off:</p><ul><li><strong>Lower Precision (e.g., Q4):</strong> Requires significantly less RAM/VRAM and runs faster on consumer hardware, but incurs a minor loss in reasoning quality.</li><li><strong>Higher Precision (e.g., FP16):</strong> Retains full accuracy, but requires high-end enterprise GPUs.</li></ul><h3>6. Model File Formats</h3><p>After parameters are trained and quantized, they must be saved to disk. Different formats belong to different hardware and runtime ecosystems:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-Ne093kRcSmQ1kYN-Qu1Aw.png" /></figure><blockquote><strong><em>Note:</em></strong><em> A file format is simply a container. Having a </em><em>.gguf or </em><em>.safetensors file doesn&#39;t guarantee your system can run it — your runtime must also support the specific underlying model architecture.</em></blockquote><h3>7. Model Variants</h3><p>Model families release different versions tailored for specific usage patterns:</p><ul><li><strong>Base:</strong> Pretrained on raw text. It excels at text continuation, but struggles with direct instructions or chat.</li><li><strong>Instruct:</strong> Fine-tuned to follow human instructions, answer questions, and hold conversations safely.</li><li><strong>Coder:</strong> Fine-tuned on millions of code repositories for programming languages.</li><li><strong>Vision-Language (VL):</strong> Expanded to accept visual inputs (images, diagrams) alongside text prompts.</li></ul><pre>Qwen3-8B         ──► Base model (Text completion)<br>Qwen3-Coder-8B   ──► Specialized coding model</pre><p>Though both have 8 billion parameters, they will perform radically differently depending on the task.</p><h3>8. Adaptation Techniques: LoRA &amp; Distillation</h3><p>When modifying or shrinking models, you will encounter two key techniques:</p><h3>LoRA (Low-Rank Adaptation)</h3><p>Instead of updating all billions of parameters during fine-tuning, LoRA freezes the original base model and trains a tiny set of auxiliary layers (often under 100 MB).</p><pre>[ Base Model (Read-Only) ] + [ Tiny LoRA Adapter (~50 MB) ] = Specialized Behavior</pre><blockquote><em>Key Takeaway: A LoRA adapter (</em><em>adapter_model.safetensors) is </em><strong><em>not</em></strong><em> a standalone model. It cannot run without its matching base model.</em></blockquote><h3>Knowledge Distillation</h3><p>Distillation uses a large, powerful model (the <strong>Teacher</strong>) to train a smaller model (the <strong>Student</strong>) to imitate its behavior.</p><pre>Large 70B Model (&quot;Teacher&quot;) ──► Knowledge Transfer ──► Small 7B Model (&quot;Student&quot;)</pre><h3>Quantization vs. Distillation vs. LoRA</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*djXr5UEkjcND3Dg0v20oOg.png" /></figure><h3>9. The Execution Stack: Runtime, Backend &amp; Hardware</h3><p>A model file sitting on your disk cannot execute itself. To get output, you need an execution stack:</p><pre>Model File (.gguf / .safetensors)<br>       │<br>       ▼<br>Inference Runtime (llama.cpp, PyTorch, ONNX Runtime)<br>       │<br>       ▼<br>Backend Driver (CUDA, Metal, Vulkan)<br>       │<br>       ▼<br>Physical Hardware (NVIDIA GPU, Apple Silicon, CPU)<br>       │<br>       ▼<br>Generated Output</pre><ul><li><strong>Runtime:</strong> The engine that loads the model architecture and manages execution steps (e.g., llama.cpp, vLLM, ExecuTorch).</li><li><strong>Backend:</strong> The hardware acceleration layer that translates runtime commands for specific chips:<br><strong>CUDA</strong> for NVIDIA GPUs<br><strong>Metal</strong> for Apple Silicon<br><strong>ROCm / Vulkan</strong> for AMD GPUs and cross-platform fallbacks</li></ul><h3>10. Real-World Examples: How to Read Filenames</h3><p>Now you can decode almost any open-source model filename instantly.</p><h3>Example A: Quantized Local Model</h3><pre>Llama-3.1-8B-Instruct-Q4_K_M.gguf</pre><ul><li><strong>Llama</strong> → Model Family</li><li><strong>3.1</strong> → Version</li><li><strong>8B</strong> → Parameter count (~8 Billion)</li><li><strong>Instruct</strong> → Instruction-tuned variant</li><li><strong>Q4_K_M</strong> → 4-bit quantization scheme (Medium K-quant)</li><li><strong>.gguf</strong> → GGUF binary format (Runs on llama.cpp / Ollama)</li></ul><h3>Example B: Specialized Coding Model</h3><pre>Qwen2.5-Coder-32B-Instruct.safetensors</pre><ul><li><strong>Qwen</strong> → Model Family</li><li><strong>2.5</strong> → Version</li><li><strong>Coder</strong> → Fine-tuned for programming tasks</li><li><strong>32B</strong> → Parameter count (~32 Billion)</li><li><strong>Instruct</strong> → Instruction-tuned variant</li><li><strong>.safetensors</strong> → Standard Hugging Face format (Uncompressed FP16/BF16)</li></ul><h3>The Model Repository Checklist</h3><p>Before downloading any open-source model, run through this quick checklist:</p><ul><li>[ ] <strong>Task:</strong> What is this model designed to do (Text, Code, Speech, Vision)?</li><li>[ ] <strong>Parameters:</strong> How big is it (0.6B, 7B, 70B)?</li><li>[ ] <strong>Variant:</strong> Is it a Base, Instruct, or Specialized model?</li><li>[ ] <strong>Precision/Format:</strong> Is it an uncompressed .safetensors file or a quantized .gguf file?</li><li>[ ] <strong>Runtime:</strong> Which software engine supports this architecture (llama.cpp, vLLM, etc.)?</li><li>[ ] <strong>Hardware Requirements:</strong> How much RAM or VRAM will it require?</li><li>[ ] <strong>License:</strong> Is it open-permissive (Apache 2.0 / MIT) or restricted for commercial use?</li></ul><p>Once you master these core distinctions, open-source AI models stops looking like a collection of random file extensions. You can evaluate any model on Hugging Face, determine if your hardware can run it, and deploy it with confidence.</p><p>👏🏻👏🏻 <strong>Your claps help this article reach more readers.</strong></p><p><strong>Thanks for making it to the end — hope you found something useful here.</strong></p><p><strong>Stay curious. 💡</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2491e1acf53e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Implicit Metadata Propagation in Kotlin Coroutines]]></title>
            <link>https://medium.com/@hsinha610/implicit-metadata-propagation-in-kotlin-coroutines-bff5126dff43?source=rss-8da3b4d8e24e------2</link>
            <guid isPermaLink="false">https://medium.com/p/bff5126dff43</guid>
            <category><![CDATA[kotlin-coroutines]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[android-development]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Harshit Sinha]]></dc:creator>
            <pubDate>Tue, 08 Sep 2026 22:01:38 GMT</pubDate>
            <atom:updated>2026-09-08T22:01:38.372Z</atom:updated>
            <content:encoded><![CDATA[<h4>Leverage custom CoroutineContext elements to route user journey tags and analytics metadata without parameter clutter.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*IUWJuA3kkhC_DYWf" /><figcaption>Photo by <a href="https://unsplash.com/@sebastiansvenson?utm_source=medium&amp;utm_medium=referral">Sebastian Svenson</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>When building software at scale, cross-cutting concerns — such as user journey tagging, analytics attribution, and request correlation — frequently fight with domain logic for space in function signatures.</p><p>Consider a common scenario: a checkout operation can be triggered from multiple entry points across an application.</p><pre>Home Screen ───────┐<br>Cart Screen ───────┼──→ Payment ViewModel ──→ Checkout UseCase ──→ Checkout Repository ──→ Network<br>Product Page ──────┘</pre><p>Your backend or analytics team needs to know which entry point initiated the checkout request (&quot;home_checkout&quot;, &quot;cart_checkout&quot;, or &quot;product_checkout&quot;) to accurately track conversion funnels.</p><h3>The Scale Problem: Deep Call Stacks and API Pollution</h3><p>In a small project, passing a flowId parameter might seem harmless. But in large-scale production applications, architecture isn&#39;t just three classes—it&#39;s a deeply structured chain of responsibility.</p><p>A single user action often traverses through:</p><ul><li>ViewModel</li><li>Action Handler / Intent Processor</li><li>Primary UseCase</li><li>Child UseCases / Interactors</li><li>Repository</li><li>Local / Remote Data Sources</li><li>Network Client or Analytics Logger</li></ul><p>In enterprise codebases with dozens of engineers working across feature modules, passing flowId through every parameter signature creates severe friction:</p><ul><li><strong>Massive API Refactoring:</strong> You end up modifying 5 to 10+ method signatures across multiple modules just to carry a single identifier from top to bottom.</li><li><strong>Brittle Interfaces &amp; Merge Conflicts:</strong> Every intermediate interface now requires this extra parameter. Any change to observability metadata creates a cascading diff across your entire codebase.</li><li><strong>Polluted Domain Models:</strong> Intermediate business layers (like CheckoutUseCase or ValidateCartUseCase) are forced to accept, hold, and pass parameters that have zero relevance to their actual domain responsibility.</li></ul><blockquote><strong>Core Design Principle:</strong></blockquote><blockquote>Business parameters<em> (e.g., cart items, promo codes) belong in function signatures because they define domain logic.</em></blockquote><blockquote>Execution metadata<em> (e.g., entry point flow IDs, correlation keys) should travel implicitly alongside execution.</em></blockquote><h3>The Naive Approach: Signature Clutter</h3><p>Here is what happens when execution metadata is forced into function signatures:</p><pre>// ViewModel<br>checkoutUseCase.execute(cart, flowId = &quot;cart_checkout&quot;)<br><br>// Interactor / UseCase<br>class CheckoutUseCase(<br>    private val validateCartUseCase: ValidateCartUseCase,<br>    private val repository: CheckoutRepository<br>) {<br>    suspend fun execute(cart: Cart, flowId: String) {<br>        validateCartUseCase.execute(cart, flowId = flowId)<br>        repository.checkout(cart, flowId = flowId)<br>    }<br>}<br><br>// Repository<br>class CheckoutRepository(private val api: CheckoutApi) {<br><br>    suspend fun checkout(cart: Cart, flowId: String) {<br>        api.checkout(CheckoutRequest(cart), flowId = flowId)<br>    }<br>}</pre><p>Notice how every single method in the call chain is forced to carry flowId, solely to hand it off to the next layer down.</p><h3>The Solution: Custom CoroutineContext.Element</h3><p>Kotlin’s CoroutineContext is an immutable, thread-safe metadata dictionary that automatically propagates down through coroutine hierarchies. It provides an ideal channel for carrying request-scoped execution metadata without touching function signatures.</p><h3>1. Define the Custom Context Element</h3><pre>import kotlin.coroutines.CoroutineContext<br><br>data class PaymentFlowContext(<br>    val flowId: String<br>) : CoroutineContext.Element {<br><br>    companion object Key : CoroutineContext.Key&lt;PaymentFlowContext&gt;<br><br>    override val key: CoroutineContext.Key&lt;*&gt;<br>        get() = Key<br>}</pre><h3>2. Inject Context at the Entry Boundary</h3><p>Attach the flow metadata at the point where the user triggers the action (e.g., in the ViewModel scope launch):</p><pre>// In CartViewModel.kt<br>fun onCheckoutClicked() {<br>    viewModelScope.launch(PaymentFlowContext(&quot;cart_checkout&quot;)) {<br>        checkoutUseCase.execute(cart)<br>    }<br>}<br><br>// In HomeViewModel.kt<br>fun onQuickBuyClicked() {<br>    viewModelScope.launch(PaymentFlowContext(&quot;home_checkout&quot;)) {<br>        checkoutUseCase.execute(cart)<br>    }<br>}</pre><h3>3. Keep Intermediate Layers 100% Pure</h3><p>Your domain and business logic layers remain completely unaware of the tracking metadata. No parameter additions, no signature breaking:</p><pre>class CheckoutUseCase(<br>    private val validateCartUseCase: ValidateCartUseCase,<br>    private val repository: CheckoutRepository<br>) {<br>    suspend fun execute(cart: Cart) {<br>        // Pure domain calls—no flowId clutter anywhere<br>        validateCartUseCase.execute(cart)<br>        repository.checkout(cart)<br>    }<br>}</pre><h3>4. Read Context at the Edge Boundary</h3><p>Retrieve the propagated flowId right where it is needed—inside your repository or network client:</p><pre>class CheckoutRepository(private val api: CheckoutApi) {<br><br>suspend fun checkout(cart: Cart) {<br>      // Read implicit metadata directly from the current CoroutineContext<br>      val flowId = coroutineContext[PaymentFlowContext]?.flowId ?: &quot;unknown&quot;<br><br>      metrics.logEvent(&quot;checkout_initiated&quot;, mapOf(&quot;flow&quot; to flowId))<br><br>      api.checkout(<br>          request = CheckoutRequest(cart),<br>          flowId = flowId<br>        )<br>    }<br>}</pre><h3>Automatic Inheritance Across Child Coroutines</h3><p>Because Kotlin coroutines automatically pass context down to child jobs, parallel or nested operations naturally retain access to the metadata:</p><pre>viewModelScope.launch(PaymentFlowContext(&quot;checkout_v2&quot;)) {<br>    coroutineScope {<br>        // Both async jobs inherit PaymentFlowContext(&quot;checkout_v2&quot;) automatically<br>        val userDef = async { repository.fetchUser() }          <br>        val cartDef = async { repository.fetchCartDetails() } <br>        awaitAll(userDef, cartDef)<br>    }<br>}  </pre><blockquote><em>Even if execution switches threads via </em><em>withContext(Dispatchers.IO), the </em><em>PaymentFlowContext travels along uninterrupted.</em></blockquote><h3>Best Practices and Guardrails</h3><p>While implicit context propagation solves signature pollution, keep these practical guidelines in mind:</p><ul><li><strong>Bridge Non-Coroutine Boundaries Explicitly:</strong> CoroutineContext exists only within Kotlin coroutines. When making network requests via OkHttp or Retrofit, use a network interceptor to read the CoroutineContext and attach the value as an HTTP header (e.g., X-Flow-ID).</li><li><strong>Keep Objects Lightweight and Immutable:</strong> Store small identifiers or value classes in the context. Never store mutable state, large data structures, or UI references (such as Android Context or Fragment instances).</li><li><strong>Use for Cross-Cutting Concerns Only:</strong> Reserve CoroutineContext propagation for observability, analytics tagging, and correlation IDs. <br><strong><em>Do not use it as a general-purpose dependency injection framework or a hidden shortcut for passing core business data.</em></strong></li></ul><p>👏🏻👏🏻 <strong>Your claps help this article reach more readers.</strong></p><p><strong>Thanks for making it to the end — hope you found something useful here.</strong></p><p><strong>Stay curious. 💡</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bff5126dff43" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Stop Overriding onCleared(): Clean Up Android ViewModels with AutoCloseable]]></title>
            <link>https://medium.com/@hsinha610/stop-overriding-oncleared-clean-up-android-viewmodels-with-autocloseable-ff089ab6be6c?source=rss-8da3b4d8e24e------2</link>
            <guid isPermaLink="false">https://medium.com/p/ff089ab6be6c</guid>
            <category><![CDATA[mobile-app-development]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[android-development]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[android-app-development]]></category>
            <dc:creator><![CDATA[Harshit Sinha]]></dc:creator>
            <pubDate>Mon, 07 Sep 2026 21:13:23 GMT</pubDate>
            <atom:updated>2026-09-07T21:13:23.476Z</atom:updated>
            <content:encoded><![CDATA[<h4>Keep your ViewModels clean by leveraging the same lifecycle API that powers viewModelScope under the hood.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*SG1_A-YCRkOphLAC" /><figcaption><strong>Photo by </strong><a href="https://unsplash.com/@sebastiansvenson?utm_source=medium&amp;utm_medium=referral"><strong>Sebastian Svenson</strong></a><strong> on </strong><a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral"><strong>Unsplash</strong></a></figcaption></figure><p>If you’ve been writing Android code for a while, you’re likely familiar with ViewModel.onCleared(). It’s the traditional resting place for teardown logic-closing database handles, cancelling network subscriptions, or unregistering event listeners.</p><p>However, scattering cleanup code inside onCleared() breaks encapsulation. Your ViewModel shouldn&#39;t need to know how to dismantle every single resource it uses.</p><p>By combining Java/Kotlin’s AutoCloseable interface with ViewModel.addCloseable(), you can write self-cleaning dependencies that automatically tear themselves down when the ViewModel dies — without writing a single line of code in onCleared().</p><h3><strong>The Core Concept</strong></h3><ol><li>AutoCloseable: A standard interface with a single close() method representing a resource that needs to be released when no longer needed.</li><li>ViewModel.addCloseable(AutoCloseable): An API in androidx.lifecycle that accepts any AutoCloseable instance. When the ViewModel is cleared, Android automatically iterates over every registered closeable and invokes its close() method.</li></ol><h3><strong>Real-World Example: A Real-Time WebSocket Connection</strong></h3><p>Imagine building a live crypto price tracker. You need a WebSocket connection open while the user is on the screen, but you must close the connection when the user navigates away to prevent memory leaks and battery drain.</p><h3><strong>Step 1: Create a Self-Cleaning Dependency</strong></h3><p>Implement AutoCloseable directly on your manager class:</p><pre>import javax.inject.Inject<br>import okhttp3.OkHttpClient<br>import okhttp3.Request<br>import okhttp3.WebSocket<br>import okhttp3.WebSocketListener<br><br>class LiveTickerWebSocketManager @Inject constructor(<br>    private val client: OkHttpClient<br>) : AutoCloseable {<br><br>    private var webSocket: WebSocket? = null<br><br>    fun connect(url: String, listener: WebSocketListener) {<br>        val request = Request.Builder()./service/https://medium.com/url(url).build()<br>        webSocket = client.newWebSocket(request, listener)<br>    }<br><br>    // AutoCloseable requirement<br>    override fun close() {<br>        webSocket?.close(1000, &quot;ViewModel destroyed&quot;)<br>        webSocket = null<br>        println(&quot;WebSocket connection successfully closed.&quot;)<br>    }<br>}</pre><h3><strong>Step 2: Inject and Register in the ViewModel</strong></h3><p>Inject your dependency into the ViewModel constructor and register it with addCloseable() inside the init block:</p><pre>import androidx.lifecycle.ViewModel<br>import dagger.hilt.android.lifecycle.HiltViewModel<br>import javax.inject.Inject<br><br>@HiltViewModel<br>class CryptoTickerViewModel @Inject constructor(<br>    private val webSocketManager: LiveTickerWebSocketManager<br>) : ViewModel() {<br>    init {<br>        // 🌟 The magic line: Register the closeable resource<br>        addCloseable(webSocketManager)<br><br>        // Start listening to the live stream<br>        webSocketManager.connect(<br>            url = &quot;wss://stream.binance.com/ws/btcusdt@trade&quot;,<br>            listener = TickerWebSocketListener()<br>        )<br>    }<br><br>    // Look, Ma! No onCleared() override required!<br>}</pre><p>When the user exits the screen and the ViewModel gets cleared, Android automatically calls webSocketManager.close().</p><h3><strong>Did You Know? </strong><strong>viewModelScope is an </strong><strong>AutoCloseable Too</strong></h3><p>This pattern isn’t just a best practice — it’s how the Android framework itself handles coroutine cancellation.</p><p>Under the hood, viewModelScope is implemented via a custom CloseableCoroutineScope class that implements AutoCloseable (or Closeable).</p><p>When you access viewModelScope for the first time:</p><ul><li>Android instantiates a CloseableCoroutineScope bound to Dispatchers.Main.immediate + SupervisorJob().</li><li>It calls addCloseable(VIEW_MODEL_SCOPE_KEY, scope) to attach it to the ViewModel’s internal set of closeables.</li><li>When the ViewModel clears, it loops through all registered AutoCloseable resources—calling close() on your custom resources and on viewModelScope, which cancels all active coroutines seamlessly.</li></ul><h3><strong>Key Benefits</strong></h3><ul><li><strong>Encapsulation</strong>: The logic for opening and closing a resource lives inside the resource class itself, not inside the ViewModel.</li><li><strong>Reusability</strong>: You can inject the same AutoCloseable component into multiple ViewModels without copying cleanup boilerplate.</li><li><strong>Separation of Concerns</strong>: Your ViewModel stays focused on managing UI state rather than orchestrating resource lifecycles.</li><li><strong>KMP Ready</strong>: In modern Kotlin Multiplatform development (androidx.lifecycle 2.8.0+), AutoCloseable is standard across Android, iOS, and Desktop.</li></ul><p>👏🏻👏🏻 <strong>Your claps help this article reach more readers.</strong></p><p><strong>Thanks for making it to the end — hope you found something useful here.</strong></p><p><strong>Stay curious. 💡</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ff089ab6be6c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[When a Static Retrofit Request Body Needs Runtime Fields]]></title>
            <link>https://medium.com/@hsinha610/when-a-static-retrofit-request-body-needs-runtime-fields-399483668b31?source=rss-8da3b4d8e24e------2</link>
            <guid isPermaLink="false">https://medium.com/p/399483668b31</guid>
            <category><![CDATA[android-development]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[serialization]]></category>
            <category><![CDATA[retrofit]]></category>
            <dc:creator><![CDATA[Harshit Sinha]]></dc:creator>
            <pubDate>Wed, 02 Sep 2026 20:41:47 GMT</pubDate>
            <atom:updated>2026-09-02T20:41:47.316Z</atom:updated>
            <content:encoded><![CDATA[<h4>How to add runtime-defined fields to a Retrofit request while keeping the known part of your API contract strongly typed.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*smm4T84wYNGdz9aW" /><figcaption>Photo by <a href="https://unsplash.com/@sebastiansvenson?utm_source=medium&amp;utm_medium=referral">Sebastian Svenson</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>In a ideal code architecture, API contracts are static, strongly typed, and predictable.</p><p>You define a clean Kotlin data class, hand it to Retrofit with Gson serialization, and everything works seamlessly at compile time:</p><pre>interface UserApi {<br>    @POST(&quot;user&quot;)<br>    suspend fun createUser(<br>        @Body request: CreateUserRequest<br>    )<br>}<br><br>data class CreateUserRequest(<br>    val name: String,<br>    val email: String,<br>    val age: Int,<br>    val address: String,<br>    val phone: String<br>    // ... plus other static fields<br>)</pre><p>Under the hood, serialization follows a clean, linear assembly line:</p><pre>CreateUserRequest  ──&gt;  Gson  ──&gt;  JSON Payload  ──&gt;  Retrofit  ──&gt;  Network</pre><p>This model provides compile-time safety, strict contract enforcement, and reliable code auto-completion across your codebase.</p><p>Then, business requirements change.</p><h3>The Shift to Dynamic Fields</h3><p>Your backend team informs you that the POST /user request needs to support context-driven dynamic metadata. These extra parameters might originate from remote configurations, active A/B testing variants, feature flags, or client-side analytics.</p><p>Today, the expected payload looks like this:</p><pre>{<br>  &quot;name&quot;: &quot;Alex&quot;,<br>  &quot;email&quot;: &quot;alex@example.com&quot;,<br>  &quot;age&quot;: 28<br>}</pre><p>Tomorrow, an active A/B test requires extra attributes:</p><pre>{<br>  &quot;name&quot;: &quot;Alex&quot;,<br>  &quot;email&quot;: &quot;alex@example.com&quot;,<br>  &quot;age&quot;: 28,<br>  &quot;experiment_id&quot;: &quot;checkout_v2&quot;,<br>  &quot;variant&quot;: &quot;B&quot;,<br>  &quot;source&quot;: &quot;remote_config&quot;<br>}</pre><p>Next week, a segment-targeted campaign adds completely different keys:</p><pre>{<br>  &quot;name&quot;: &quot;Alex&quot;,<br>  &quot;email&quot;: &quot;alex@example.com&quot;,<br>  &quot;age&quot;: 28,<br>  &quot;user_segment&quot;: &quot;premium&quot;,<br>  &quot;new_flow_enabled&quot;: true,<br>  &quot;campaign&quot;: &quot;summer_sale&quot;<br>}</pre><blockquote><strong><em>This presents a architectural dilemma:</em></strong><em> How do you append unpredictable, runtime-determined key-value pairs to a statically typed Kotlin model without sacrificing type safety or breaking your networking pipeline?</em></blockquote><h3>Failed Attempts and Anti-Patterns</h3><h3>1. Adding Nullable Fields to the Data Class</h3><p>The most immediate reaction is to manually append optional parameters to the existing data class as new features launch:</p><pre>data class CreateUserRequest(<br>    val name: String,<br>    val email: String,<br>    val age: Int,<br>// Dynamic additions...<br>    val experimentId: String? = null,<br>    val variant: String? = null,<br>    val userSegment: String? = null,<br>    val campaign: String? = null<br>)</pre><p><strong>Why this breaks down:</strong></p><ul><li><strong>Tight Coupling:</strong> Every new backend experiment forces an Android data model change and <strong>a fresh app store release</strong>.</li><li><strong>Model Bloat:</strong> Your core request model continuously accumulates temporary, nullable metadata attributes that outlive their utility.</li><li><strong>Architectural Instability:</strong> Statically typed contracts should represent core domain state, not arbitrary analytics payloads.</li></ul><h3>2. Abandoning Strongly Typed Models for Raw JsonObject / Maps</h3><p>Frustrated by nested payloads or rigid data classes, engineers are sometimes tempted to throw out static models altogether and pass a raw JsonObject or Map&lt;String, Any?&gt; to Retrofit:</p><pre>interface UserApi {<br>    @POST(&quot;user&quot;)<br>    suspend fun createUser(<br>        @Body request: Map&lt;String, Any?&gt;<br>    )<br>}</pre><p>Or manually constructing a raw JSON container:</p><pre>val requestPayload = JsonObject().apply {<br>    addProperty(&quot;name&quot;, &quot;Alex&quot;)<br>    addProperty(&quot;email&quot;, &quot;alex@example.com&quot;)<br>    addProperty(&quot;age&quot;, 28)<br>    addProperty(&quot;experiment_id&quot;, &quot;checkout_v2&quot;)<br>    addProperty(&quot;variant&quot;, &quot;B&quot;)<br>}</pre><p><strong>Why this breaks down:</strong></p><ul><li><strong>Loss of Compile-Time Safety:</strong> Replacing strongly typed models with untyped structures strips away type safety across your entire feature layer. Misspelling standard keys like &quot;emial&quot; or passing an invalid data type for &quot;age&quot; fails silently at runtime rather than at compile time.</li><li><strong>Degraded IDE Support:</strong> Auto-completion, refactoring tools, and field usages are destroyed. Renaming a property in your model requires manual string searches across the codebase.</li><li><strong>Erosion of API Self-Documentation:</strong> Data classes act as readable, self-documenting code contracts. Replacing them forces developers to continuously reference external API documentation just to know which standard keys are required.</li></ul><h3>3. Using an Embedded Map</h3><p>The next logical approach is introducing a map property to house dynamic parameters:</p><pre>data class CreateUserRequest(<br>    val name: String,<br>    val email: String,<br>    val age: Int,<br>    val additionalFields: Map&lt;String, Any?&gt; = emptyMap()<br>)</pre><p>You construct the object naturally:</p><pre>val request = CreateUserRequest(<br>    name = &quot;Alex&quot;,<br>    email = &quot;alex@example.com&quot;,<br>    age = 28,<br>    additionalFields = mapOf(<br>        &quot;experiment_id&quot; to &quot;checkout_v2&quot;,<br>        &quot;variant&quot; to &quot;B&quot;<br>    )<br>)</pre><p>However, standard Gson serialization produces a nested JSON structure:</p><pre>{<br>  &quot;name&quot;: &quot;Alex&quot;,<br>  &quot;email&quot;: &quot;alex@example.com&quot;,<br>  &quot;age&quot;: 28,<br>  &quot;additionalFields&quot;: {<br>    &quot;experiment_id&quot;: &quot;checkout_v2&quot;,<br>    &quot;variant&quot;: &quot;B&quot;<br>  }<br>}</pre><p>Because the backend expects these attributes at the <strong>root level</strong> alongside name and email, the server rejects this payload.</p><h3>Intercepting Serialization</h3><p>We shouldn’t sacrifice static type safety for multiple existing core fields just because 5 dynamic metadata fields need runtime flexibility.</p><p>Your static request model should remain fully typed, while a dedicated serialization step flattens dynamic map attributes directly into the root JSON object before transmission.</p><pre>┌──────────────────────────────────────┐<br>│        CreateUserRequest             │<br>│                                      │<br>│  name = &quot;Alex&quot;                       │<br>│  email = &quot;alex@example.com&quot;          │<br>│  age = 28                            │<br>│                                      │<br>│  dynamicFields = [                   │<br>│      &quot;experiment_id&quot; : &quot;checkout_v2&quot; │<br>│      &quot;variant&quot;       : &quot;B&quot;           │<br>│  ]                                   │<br>└──────────────────┬───────────────────┘<br>                   │<br>                   ▼<br>     DynamicFieldsAdapterFactory<br>  (Serializes model + flattens map)<br>                   │<br>                   ▼<br>┌──────────────────────────────────────┐<br>│              Final JSON              │<br>│                                      │<br>│  {                                   │<br>│    &quot;name&quot;: &quot;Alex&quot;,                   │<br>│    &quot;email&quot;: &quot;alex@example.com&quot;,      │<br>│    &quot;age&quot;: 28,                        │<br>│    &quot;experiment_id&quot;: &quot;checkout_v2&quot;,   │<br>│    &quot;variant&quot;: &quot;B&quot;                    │<br>│  }                                   │<br>└──────────────────┬───────────────────┘<br>                   │<br>                   ▼<br>                Retrofit</pre><h3>The Solution: A Custom TypeAdapterFactory</h3><p>First, establish a marker interface to denote request models that hold dynamic payloads:</p><pre>interface HasDynamicFields {<br>    val dynamicFields: Map&lt;String, Any?&gt;<br>}</pre><p>Implement this interface on models requiring runtime field injection:</p><pre>data class CreateUserRequest(<br>    val name: String,<br>    val email: String,<br>    val age: Int,<br>    override val dynamicFields: Map&lt;String, Any?&gt; = emptyMap()<br>) : HasDynamicFields</pre><p>Next, build a custom TypeAdapterFactory to intercept serialization for any class implementing HasDynamicFields.</p><pre>import com.google.gson.Gson<br>import com.google.gson.TypeAdapter<br>import com.google.gson.TypeAdapterFactory<br>import com.google.gson.internal.Streams<br>import com.google.gson.reflect.TypeToken<br>import com.google.gson.stream.JsonReader<br>import com.google.gson.stream.JsonWriter<br><br>class DynamicFieldsAdapterFactory : TypeAdapterFactory {<br>    override fun &lt;T&gt; create(gson: Gson, type: TypeToken&lt;T&gt;): TypeAdapter&lt;T&gt;? {<br><br>        // Only intercept models implementing HasDynamicFields<br>        if (!HasDynamicFields::class.java.isAssignableFrom(type.rawType)) {<br>            return null<br>        }<br><br>        // Fetch Gson&#39;s default adapter for this type to handle standard field processing<br>        val delegate = gson.getDelegateAdapter(this, type)<br><br>        return object : TypeAdapter&lt;T&gt;() {<br>            override fun write(out: JsonWriter, value: T?) {<br>                if (value == null) {<br>                    out.nullValue()<br>                    return<br>                }<br>                // 1. Serialize the base object using Gson&#39;s standard adapter logic<br>                val jsonObject = delegate.toJsonTree(value).asJsonObject<br><br>                val dynamicModel = value as HasDynamicFields<br><br>                // 2. Remove the internal &quot;dynamicFields&quot; map key from the root object<br>                jsonObject.remove(&quot;dynamicFields&quot;)<br><br>                // 3. Flatten dynamic key-value pairs into the root JSON object<br>                dynamicModel.dynamicFields.forEach { (key, fieldValue) -&gt;<br><br>                    // Guard rail against accidental key collisions<br>                    require(!jsonObject.has(key)) {<br>                        &quot;Dynamic field key &#39;$key&#39; conflicts with a statically defined property in ${value::class.java.simpleName}&quot;<br>                    }<br>                    jsonObject.add(key, gson.toJsonTree(fieldValue))<br>                }<br><br>                // 4. Write the final flattened JsonObject to the stream<br>                Streams.write(jsonObject, out)<br>            }<br>            override fun read(input: JsonReader): T {<br>                // Deserialization uses the standard delegate behavior<br>                return delegate.read(input)<br>            }<br>        }<br>    }<br>}</pre><h4>How It Works Step-by-Step</h4><ol><li><strong>Type Inspection:</strong> When Gson prepares a type adapter for a model, create() checks whether the target class implements HasDynamicFields. If it doesn&#39;t, returning null tells Gson to bypass this factory and proceed with standard serialization.</li><li><strong>Delegate Retrieval:</strong> By calling gson.getDelegateAdapter(this, type), we fetch Gson’s standard built-in adapter for the model class. This is crucial - it allows Gson to handle all standard field mapping, @SerializedName annotations, null checks, and nested object conversions automatically without manual reflection.</li><li><strong>Tree Conversion:</strong> Inside write(), we pass the Kotlin instance to delegate.toJsonTree(value).asJsonObject. This converts the data class into a mutable, in-memory JsonObject containing all statically defined fields.</li><li><strong>Stripping Nesting:</strong> Because dynamicFields is a property on the data class, Gson’s standard delegate automatically converts it into a nested JSON object (&quot;dynamicFields&quot;: { ... }). Calling jsonObject.remove(&quot;dynamicFields&quot;) deletes this container key from the tree before it hits the wire.</li><li><strong>Flattening Entries:</strong> We iterate through the map in dynamicModel.dynamicFields, convert each runtime value into a JsonElement via gson.toJsonTree(fieldValue), check for collisions against existing static fields, and append each key-value pair directly onto the root jsonObject.</li><li><strong>Streaming Output:</strong> Streams.write(jsonObject, out) takes the modified root JSON object and streams it directly to the outgoing network writer.</li><li><strong>Transparent Deserialization:</strong> In read(), we route the incoming JSON stream straight back to delegate.read(input), preserving default parsing logic for incoming responses.</li></ol><h3>Managing Key Collisions in Production</h3><p>When allowing dynamic key injection, you must define a clear policy for handling key collisions (e.g., if dynamicFields contains &quot;email&quot; while the data class also defines an email property).</p><p>There are three primary strategies to handle this:</p><ul><li><strong>Fail-Fast (Recommended):</strong> Throw an explicit IllegalArgumentException during serialization. This prevents silent data corruption and alerts engineers to accidental field overlaps immediately during testing:</li></ul><pre>require(!jsonObject.has(key)) {     <br>  &quot;Dynamic field &#39;$key&#39; conflicts with an existing payload property.&quot; <br>} <br>jsonObject.add(key, gson.toJsonTree(fieldValue))</pre><ul><li><strong>Static Fields Win:</strong> Ignore dynamic values if a static property already exists:</li></ul><pre>if (!jsonObject.has(key)) {     <br>  jsonObject.add(key, gson.toJsonTree(fieldValue)) <br>}</pre><ul><li><strong>Dynamic Fields Override:</strong> Overwrite static model properties with dynamic map values:</li></ul><pre>jsonObject.add(key, gson.toJsonTree(fieldValue))</pre><p>For critical production services, the <strong>Fail-Fast</strong> approach provides the strongest safety guarantee.</p><h3>Wiring Up the Infrastructure</h3><p>Register the factory with your global GsonBuilder during dependency injection setup:</p><pre>val gson = GsonBuilder()<br>    .registerTypeAdapterFactory(DynamicFieldsAdapterFactory())<br>    .create()<br><br>val retrofit = Retrofit.Builder()<br>    .baseUrl(&quot;https://api.example.com/&quot;)<br>    .addConverterFactory(GsonConverterFactory.create(gson))<br>    .build()</pre><p>Your API interfaces and business logic remain completely clean:</p><pre>val request = CreateUserRequest(<br>    name = &quot;Alex&quot;,<br>    email = &quot;alex@example.com&quot;,<br>    age = 28,<br>    dynamicFields = mapOf(<br>        &quot;experiment_id&quot; to &quot;checkout_v2&quot;,<br>        &quot;variant&quot; to &quot;B&quot;,<br>        &quot;user_segment&quot; to &quot;premium&quot;<br>    )<br>)<br><br>// Retrofit call sends a fully flattened JSON payload<br>userApi.createUser(request)</pre><p><strong>Resulting HTTP Body:</strong></p><pre>{<br>  &quot;name&quot;: &quot;Alex&quot;,<br>  &quot;email&quot;: &quot;alex@example.com&quot;,<br>  &quot;age&quot;: 28,<br>  &quot;experiment_id&quot;: &quot;checkout_v2&quot;,<br>  &quot;variant&quot;: &quot;B&quot;,<br>  &quot;user_segment&quot;: &quot;premium&quot;<br>}</pre><p>By pushing dynamic field merging down to the serialization layer, your Kotlin data classes stay clean, your Retrofit interfaces remain untouched, and you retain compile-time safety where it matters most.</p><p>👏🏻👏🏻 <strong>Your claps help this article reach more readers.</strong></p><p><strong>Thanks for making it to the end — hope you found something useful here.</strong></p><p><strong>Stay curious. 💡</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=399483668b31" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Kotlin Coroutines Are Cooperative, Not Preemptive (And Why It Matters)]]></title>
            <link>https://medium.com/@hsinha610/kotlin-coroutines-are-cooperative-not-preemptive-and-why-it-matters-5e85ab2cafad?source=rss-8da3b4d8e24e------2</link>
            <guid isPermaLink="false">https://medium.com/p/5e85ab2cafad</guid>
            <category><![CDATA[threads]]></category>
            <category><![CDATA[kotlin-coroutines]]></category>
            <category><![CDATA[android-development]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Harshit Sinha]]></dc:creator>
            <pubDate>Wed, 29 Jul 2026 20:13:13 GMT</pubDate>
            <atom:updated>2026-07-30T10:36:37.732Z</atom:updated>
            <content:encoded><![CDATA[<h4>The “lightweight thread” metaphor is a double-edged sword. Here is why assuming coroutines behave like OS threads leads to CPU starvation, delayed cancellation, and silent performance bugs.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*yllg2xIImDYrBTOK" /><figcaption>Photo by <a href="https://unsplash.com/@sebastiansvenson?utm_source=medium&amp;utm_medium=referral">Sebastian Svenson</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>One of the most persistent misconceptions in modern Kotlin development is that coroutines are simply <strong>“lightweight threads.”</strong></p><p>On paper, the metaphor makes sense. You launch them, they execute asynchronously, and they let you run thousands of concurrent operations without blowing up your heap space.</p><p><strong>Except they are not threads.</strong></p><p>Treating a coroutine like a thread leads to subtle, hard-to-reproduce bugs — especially under CPU-bound workloads. Application UI freezes, background tasks stall, and cancellation signals seem to vanish into thin air.</p><p>Consider this seemingly innocent block of Kotlin code:</p><pre>launch {<br>    while (true) {<br>        doSomeWork()<br>    }<br>}</pre><p>Many developers assume that because this runs inside a coroutine, the underlying scheduler will eventually pause it, context-switch, and let other coroutines take their turn.</p><p><strong>It won’t.</strong></p><p>The reason comes down to one fundamental principle: <strong>Coroutines are cooperative, not preemptive.</strong> Understanding this single rule unlocks the real mental model behind how coroutines execute under the hood.</p><h3>Threads vs. Coroutines: A Tale of Two Schedulers</h3><p>To understand why that infinite loop hogged your application, we need to look at how the execution target is scheduled.</p><h4>1. Thread Scheduling (Preemptive)</h4><p>Traditional operating systems manage threads using <strong>preemptive scheduling</strong>.</p><p>Imagine two OS threads running on a single CPU core. Even if Thread A enters a tight, non-stop loop:</p><pre>// Java / Native OS Thread<br>while (true) {<br>    doWork();<br>}</pre><p>The operating system doesn’t care. At regular intervals, the OS hardware timer triggers a <strong>timer interrupt</strong>. The OS scheduler steps in, forcibly pauses Thread A, saves its context, and hands execution over to Thread B.</p><pre>Thread A  ───► [ Timer Interrupt ] ───► OS Scheduler ───► Thread B</pre><p>Thread A never has to &quot;agree&quot; to yield control. The operating system forces it to.</p><h4>2. Coroutine Scheduling (Cooperative)</h4><p>Kotlin coroutines do not run on bare metal or directly control OS hardware timers. They run <em>on top</em> of a Kotlin runtime scheduler, which dispatches work onto underlying Java threads (such as a ThreadPoolExecutor).</p><p>Because the runtime cannot trigger hardware interrupts on a thread, a coroutine will continue running until one of three specific things happens:</p><ul><li>It <strong>finishes</strong> its execution.</li><li>It <strong>throws</strong> an unhandled exception.</li><li>It hits a <strong>suspension point</strong> (e.g., calling a suspend function).</li></ul><p>That’s it. If none of those three events occur, the coroutine simply holds onto the executing thread forever.</p><pre>launch {<br>    while (true) {<br>        doWork() // No delay(), yield(), await(), receive(), or emit()<br>    }<br>}</pre><p>Without an explicit suspension point, <strong>the coroutine never voluntarily yields control back to the scheduler.</strong></p><h3>What Happens Under the Hood?</h3><p>Let’s trace what happens when you launch a non-suspending loop using Dispatchers.Default.</p><p>On an 8-core device, Dispatchers.Default creates a worker pool bounded by the number of CPU cores—roughly 8 background worker threads.</p><pre>[ Worker 1 ] [ Worker 2 ] [ Worker 3 ] ... [ Worker 8 ]</pre><p>When you launch a coroutine, the scheduler assigns it to one of these worker threads (say, Worker 1).</p><p>Now imagine your coroutine executes a heavy calculation loop:</p><pre>launch(Dispatchers.Default) {<br>    while (true) {<br>        counter++<br>    }<br>}</pre><p>Behind the scenes, Worker 1 gets trapped executing that native loop:</p><pre>Worker 1: while(true) -&gt; counter++ -&gt; while(true) -&gt; counter++ ...</pre><p>Because execution never leaves the loop body, control <strong>never returns to the Dispatcher’s internal queue</strong>.</p><p>As far as the Kotlin coroutine framework is concerned, that worker thread is busy. If you spawn 8 of these unyielding coroutines simultaneously on an 8-core machine, <strong>you have completely starved the entire </strong>Dispatchers.Default<strong> pool.</strong> Any other coroutine trying to run on Dispatchers.Default will sit in a queue waiting indefinitely.</p><h3>Why Can’t the Coroutine Scheduler Just Pause It?</h3><p>Because the coroutine framework is written in application code (Java/Kotlin bytecodes). It doesn’t have access to timer interrupts or low-level CPU registers. It cannot halt execution halfway through a standard bytecode instruction sequence.</p><p>Suspension is only possible at compiled <strong>suspension points</strong> — places where the Kotlin compiler generates a state machine that can save the execution state and return control up the stack.</p><p>That’s why coroutine scheduling is called <strong>cooperative</strong>. The code must cooperate with the runtime by voluntarily yielding.</p><h3>The Solution: Yielding and Suspending</h3><p>How do you make tight, long-running CPU loops cooperative? You insert explicit suspension points.</p><h3>Enter yield()</h3><p>Kotlin provides a top-level suspend function designed specifically for this problem: yield().</p><pre>launch {<br>    while (true) {<br>        doSomeWork()<br>        yield() // Explicit suspension point<br>    }<br>}</pre><p>yield() checks if there are other coroutines waiting on the current dispatcher. If there are, it suspends the current coroutine, puts it at the back of the execution queue, and lets another coroutine run.</p><pre>Without yield():<br>Worker 1: [ Coroutine A ─────────────────────────────────────────── ]<br>            Coroutine B (Waiting...)<br>            Coroutine C (Waiting...)<br><br>With yield():<br>Worker 1: [ A ] ──► [ B ] ──► [ C ] ──► [ A ] ──► [ B ] ──► [ C ]</pre><h3>What About delay()?</h3><p>delay() is also a valid suspension point:</p><pre>while (true) {<br>    doSomeWork()<br>    delay(1) // Suspends execution for at least 1ms<br>}</pre><p>Crucially, unlike Thread.sleep(), calling delay() <strong>does not block the underlying worker thread</strong>. The worker thread drops the suspended coroutine, picks up another waiting task, executes it for 1 millisecond, and returns to resume your original task later.</p><h3>The isActive Trap</h3><p>A very common pattern seen in production code is using isActive to handle cancellation gracefully:</p><pre>// ⚠️ Improves cancellation check, but DOES NOT yield CPU execution<br>while (isActive) {<br>    doHeavyCalculation()<br>}</pre><p>Checking isActive is good practice because it allows the loop to exit if its parent job is canceled. <strong>However, </strong>isActive<strong> is just a boolean property check—it is not a suspension point.</strong></p><p>It does not suspend execution. It does not yield the thread.</p><p>If doHeavyCalculation() takes 5 seconds, that worker thread remains 100% occupied for those 5 seconds regardless of isActive. To make it both cancelable <em>and</em> cooperative, pair it with yield():</p><pre>// ✅ Cancelable AND cooperative<br>while (isActive) {<br>    doHeavyCalculation()<br>    yield() <br>}</pre><h3>Does This Mean Infinite Loops Are Bad in Coroutines?</h3><p>Not at all. In fact, long-lived loops are common in architecture patterns like event processing, channel consumption, and network polling:</p><pre>// ✅ Perfectly fine! receive() is a suspension point.<br>while (isActive) {<br>    val event = channel.receive() <br>    process(event)<br>}</pre><p>Why is this loop completely safe?</p><p>Because channel.receive() is a suspending function. When the channel has no new items, receive() <strong>suspends</strong> the coroutine. The underlying thread is freed immediately to handle other tasks across your application.</p><p>The loop spends 99% of its life cycle safely suspended, waiting for data without burning CPU cycles.</p><h3>Conclusion</h3><p>Coroutines scale effortlessly to tens of thousands of concurrent tasks because most operations (I/O, database access, network calls, timer delays) spend the vast majority of their time <strong>suspended</strong>.</p><p>However, that efficiency relies entirely on <strong>cooperation</strong>:</p><ul><li><strong>Threads are preemptive.</strong> The OS forcibly pauses them via timer interrupts.</li><li><strong>Coroutines are cooperative.</strong> They only yield control at explicit suspend points.</li><li><strong>CPU-bound loops need manual cooperation.</strong> Use yield() or delay() during long-running computations to give other tasks room to execute.</li><li>isActive<strong> is not enough.</strong> It checks cancellation status, but it doesn&#39;t yield the executing thread.</li></ul><p>The next time you write a long-running coroutine or a CPU-heavy processing block, ask yourself one simple question:</p><blockquote><strong><em>“Where does this code give the scheduler a chance to run something else?”</em></strong></blockquote><p>If the answer is <em>“nowhere,”</em> your “lightweight coroutine” is acting like an unyielding thread — and it’s time to add a yield().</p><p>👏🏻👏🏻 <strong>Your claps help this article reach more readers.</strong></p><p><strong>Thanks for making it to the end — hope you found something useful here.</strong></p><p><strong>Stay curious. 💡</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5e85ab2cafad" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Kotlin Inlining Masterclass: inline, noinline, & crossinline Demystified]]></title>
            <link>https://medium.com/@hsinha610/kotlin-inlining-masterclass-inline-noinline-and-crossinline-demystified-7e6b7731d774?source=rss-8da3b4d8e24e------2</link>
            <guid isPermaLink="false">https://medium.com/p/7e6b7731d774</guid>
            <category><![CDATA[android-development]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[performance]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Harshit Sinha]]></dc:creator>
            <pubDate>Tue, 28 Jul 2026 20:49:21 GMT</pubDate>
            <atom:updated>2026-07-28T20:50:03.500Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*XbVa4zsmoPDNRD-o" /><figcaption>Photo by <a href="https://unsplash.com/@sebastiansvenson?utm_source=medium&amp;utm_medium=referral">Sebastian Svenson</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>If you’ve spent any time writing Kotlin, you’ve undoubtedly come across higher-order functions — functions that take other functions as parameters. They make code expressive, clean, and declarative.</p><p>However, higher-order functions come with a hidden runtime cost. To solve this, Kotlin introduces three keywords: <strong>inline</strong>, <strong>noinline</strong>, and <strong>crossinline</strong>.</p><p>Let’s break down each keyword by contrasting how code behaves <strong>without</strong> it versus <strong>with</strong> it.</p><h3>1. The Baseline: Why Do We Need Inlining?</h3><p>In Kotlin, lambdas are not magic — under the hood on the JVM, <strong>every non-inlined lambda is instantiated as an object</strong>.</p><h4>❌ WITHOUT inline</h4><pre>// Normal higher-order function<br>fun doSomething(action: () -&gt; Unit) {<br>    action()<br>}<br><br>fun main() {<br>    doSomething { println(&quot;hello&quot;) }<br>}</pre><p><strong>What the JVM actually generates under the hood:</strong></p><pre>// Every call creates a new anonymous Function object on the heap!<br>doSomething(new Function0&lt;Unit&gt;() {<br>    @Override<br>    public Unit invoke() {<br>        System.out.println(&quot;hello&quot;);<br>        return Unit.INSTANCE;<br>    }<br>});</pre><ul><li><strong>Cost:</strong> Garbage Collection (GC) pressure, memory allocation overhead, and extra virtual function call dispatches for every single invocation.</li></ul><h4>✅ WITH inline</h4><pre>// Adding &#39;inline&#39; tells the compiler to copy-paste the body<br>inline fun doSomething(action: () -&gt; Unit) {<br>    action()<br>}<br><br>fun main() {<br>    doSomething { println(&quot;hello&quot;) }<br>}</pre><p><strong>What the JVM actually generates under the hood:</strong></p><pre>// No lambda objects, no extra function dispatch — directly pasted!<br>System.out.println(&quot;hello&quot;);</pre><h3>2. Non-Local Returns</h3><p>Inlined lambdas don’t just save memory — they also change how control flow works inside the lambda body.</p><h4>❌ WITHOUT inline</h4><p>Non-inlined lambdas run inside a separate Function object context. Because of this boundary, you <strong>cannot</strong> use return to exit the enclosing function.</p><pre>fun findFirst(list: List&lt;Int&gt;, predicate: (Int) -&gt; Boolean): Int? {<br>    for (item in list) {<br>        if (predicate(item)) return item<br>    }<br>    return null<br>}<br><br><br>fun main() {<br>    val result = findFirst(listOf(1, 2, 3)) { item -&gt;<br>        if (item == 2) return item // ❌ COMPILE ERROR: &#39;return&#39; is not allowed here<br>        item &gt; 1<br>    }<br>}</pre><h4>✅ WITH inline</h4><p>Because an inline function&#39;s code is pasted directly into the calling location, a return statement acts as if it were written directly inside the caller. This is called a <strong>non-local return</strong>.</p><pre>inline fun findFirst(list: List&lt;Int&gt;, predicate: (Int) -&gt; Boolean): Int? {<br>    for (item in list) {<br>        if (predicate(item)) return item<br>    }<br>    return null<br>}<br><br>fun main() {<br>    val result = findFirst(listOf(1, 2, 3)) { item -&gt;<br>        if (item == 2) return item // ✅ WORKS: Exits main() directly!<br>        item &gt; 1<br>    }<br>}</pre><h3>3. noinline — Opting Out Individual Lambdas</h3><p>When a function is marked inline, Kotlin inlines <strong>all</strong> lambda parameters passed to it by default. But an inlined lambda <strong>does not exist as an object at runtime</strong>. If you need to store or pass a lambda as a real object reference, inlining breaks.</p><h4>❌ WITHOUT noinline</h4><pre>inline fun process(<br>    inlinedAction: () -&gt; Unit,<br>    storedAction: () -&gt; Unit<br>) {<br>    inlinedAction()<br>    val list = mutableListOf&lt;() -&gt; Unit&gt;()<br>    list.add(storedAction) // ❌ COMPILE ERROR: Illegal usage of inline-parameter<br>    <br>    Thread(storedAction).start() // ❌ COMPILE ERROR: Cannot pass inlined lambda as an object<br>}</pre><h4>✅ WITH noinline</h4><p>Adding noinline tells the compiler: <em>&quot;Keep this function inlined overall, but leave this specific lambda parameter as a real </em><em>Function object at runtime.&quot;</em></p><pre>inline fun process(<br>    inlinedAction: () -&gt; Unit,<br>    noinline storedAction: () -&gt; Unit // ✅ Kept as a real object<br>) {<br>    inlinedAction() // Pasted directly at call site<br>    val list = mutableListOf&lt;() -&gt; Unit&gt;()<br>    list.add(storedAction) // ✅ WORKS: Stored in list<br>    Thread(storedAction).start() // ✅ WORKS: Passed to Thread Runnable<br>}</pre><h3>4. crossinline — Safe Inlining in Nested Contexts</h3><p>What happens when an inlined lambda needs to be executed <strong>inside another context</strong> (such as a nested lambda, an object expression, or a thread)?</p><h4>❌ WITHOUT crossinline</h4><pre>inline fun runInThread(action: () -&gt; Unit) {<br>    Thread {<br>        action() // ❌ COMPILE ERROR: Can&#39;t inline &#39;action&#39; inside a nested execution context<br>    }.start()<br>}<br><br>fun main() {<br>    runInThread {<br>        return // DANGER: If allowed, this would try to non-local return from main() inside a background thread!<br>    }<br>}</pre><h4>✅ WITH crossinline</h4><p>crossinline tells the compiler: <em>&quot;Inline this lambda for performance, but strictly forbid the caller from executing a non-local return.&quot;</em></p><pre>inline fun runInThread(crossinline action: () -&gt; Unit) {<br>    Thread {<br>        action() // ✅ WORKS: Safe to inline inside Runnable.run()<br>    }.start()<br>}<br><br>fun main() {<br>    runInThread {<br>        // return // ❌ COMPILE ERROR: Forbidden by crossinline!<br>        println(&quot;hello&quot;) // ✅ WORKS: Safe code runs normally<br>    }<br>}</pre><h3>5. Bonus: reified Type Parameters</h3><p>Normally, generic type information is erased at compile time on the JVM.</p><h4>❌ WITHOUT inline + reified</h4><pre>fun &lt;T&gt; isType(value: Any): Boolean {<br>    return value is T // ❌ COMPILE ERROR: Cannot check for instance of erased type &#39;T&#39;<br>}</pre><h4>✅ WITH inline + reified</h4><p>Because inline pastes the function code directly into the call site, the compiler knows the exact class type being passed at that specific location.</p><pre>inline fun &lt;reified T&gt; isType(value: Any): Boolean {<br>    return value is T // ✅ WORKS: T is concrete and known at call site<br>}<br><br>fun main() {<br>    println(isType&lt;String&gt;(&quot;hello&quot;)) // true<br>    println(isType&lt;Int&gt;(&quot;hello&quot;))    // false<br>}</pre><h3>Quick Comparison Summary</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iQTN3d6GdCRrTvPTsEI_-g.png" /></figure><h3>The Mental Model</h3><ul><li><strong>inline</strong> = Copy-paste the body. Zero allocations, full power, enables non-local returns.</li><li><strong>noinline</strong> = Keep this one lambda as an object reference so I can store, pass, or return it.</li><li><strong>crossinline</strong> = Copy-paste for performance, but lock down non-local returns because the execution happens inside a nested boundary.</li></ul><p>👏🏻👏🏻 <strong>Your claps help this article reach more readers.</strong></p><p><strong>Thanks for making it to the end — hope you found something useful here.</strong></p><p><strong>Stay curious. 💡</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7e6b7731d774" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The 4 Function Types Every Developer Should Master]]></title>
            <link>https://medium.com/@hsinha610/the-4-function-types-every-developer-should-master-a9edfbaeb2c4?source=rss-8da3b4d8e24e------2</link>
            <guid isPermaLink="false">https://medium.com/p/a9edfbaeb2c4</guid>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[lambda-function]]></category>
            <category><![CDATA[kotlin]]></category>
            <dc:creator><![CDATA[Harshit Sinha]]></dc:creator>
            <pubDate>Tue, 07 Jul 2026 21:18:53 GMT</pubDate>
            <atom:updated>2026-07-07T21:18:53.676Z</atom:updated>
            <content:encoded><![CDATA[<h4>Predicates, selectors, comparators, and transformers aren’t just lambdas — they’re reusable abstractions that appear in almost every modern programming language.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*7kN5ifmR1VKQ8g5Z" /><figcaption>Photo by <a href="https://unsplash.com/@sebastiansvenson?utm_source=medium&amp;utm_medium=referral">Sebastian Svenson</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>If you’ve spent any time with Kotlin, Java Streams, Swift, JavaScript, Python, or C#, you have likely written or encountered a chain of operations that looks exactly like this:</p><pre>users.filter { it.isActive }<br>     .sortedBy { it.age }<br>     .map { it.name }</pre><p>Three lambdas, three completely different jobs.</p><p>Calling them all “lambdas” or “callbacks” is syntactically correct, but it misses the point of functional abstraction. Each closure has a unique responsibility.</p><p>When you learn to recognize them by their true names, complex API chains stop looking like magic and start reading like clear sentences. Let’s break down the four foundational types.</p><h3>1. The Predicate: “Should I keep this?”</h3><h4>The Signature</h4><p>(T) -&gt; Boolean</p><p>A <strong>Predicate</strong> answers a simple, uncompromising yes-or-no question about a single object. Does this item satisfy a specific condition? It takes an object as input and <em>always</em> returns a boolean.</p><pre>val isAdult: (Person) -&gt; Boolean = { it.age &gt;= 18 }</pre><p>Given a Person, the predicate doesn&#39;t change them, move them, or extract data from them. It simply passes judgment: <em>True</em> or <em>False</em>.</p><h3>Common APIs That Depend on Predicates:</h3><ul><li>list.filter { it.age &gt;= 18 } (Keep only items that return true)</li><li>list.any { it.isAdmin } (Is there at least one true match?)</li><li>list.none { it.isDeleted } (Ensure zero true matches)</li><li>list.all { it.isVerified } (Ensure every single item returns true)</li><li>list.takeWhile { it.isActive } (Grab items until one returns false)</li></ul><p><strong>The Takeaway:</strong> Every single one of these collection methods expects a predicate. The collection handles the looping logic and decides what to do with the result; your predicate’s only job is to answer the question honestly.</p><h3>2. The Selector: “Which part do you care about?”</h3><h4>The Signature</h4><p>(T) -&gt; R</p><p>A <strong>Selector</strong> is designed to dig into an object and pull out a specific piece of data. It doesn’t transform the object into an entirely new concept; it simply isolates an existing property. Think of it as telling the system, <strong><em>“</em></strong><em>For this object, here is the specific value I want you to look at, when performing a particular operation on the collection.</em><strong><em>”</em></strong></p><pre>val ageSelector: (Person) -&gt; Int = { it.age }</pre><h3>Common APIs That Depend on Selectors:</h3><ul><li>users.sortedBy { it.age }</li><li>users.maxByOrNull { it.salary }</li><li>users.groupBy { it.department }</li><li>users.distinctBy { it.email }</li></ul><p>Notice how clean these APIs are. They never ask you, <em>“How do I compare two users or group them?”</em> Instead, they ask, <em>“Which property should I use to do my job?”</em> You provide the selector key, and the internal library logic handles the heavy lifting.</p><p>For instance, instead of writing an intricate, manual comparison block like this:</p><pre>// The old, verbose way<br>employees.sortedWith { a, b -&gt; a.salary.compareTo(b.salary) }</pre><p>You use a selector to say exactly what you mean:</p><pre>// The clean, selector-driven way<br>employees.sortedBy { it.salary }</pre><h3>3. The Comparator: “Which one comes first?”</h3><h4>The Signature</h4><p>(T, T) -&gt; Int</p><p>Sometimes, a simple selector isn’t enough. If you need to build a complex sorting order, or if the data types don’t have a natural sorting order, you need complete control over the logic. That is the domain of the <strong>Comparator</strong>.</p><p>A comparator accepts <em>two</em> objects of the same type and dictates how they sit relative to one another.</p><pre>val compareAge = Comparator&lt;Person&gt; { a, b -&gt; a.age.compareTo(b.age) }</pre><p>It expects a standard three-way integer return value:</p><ul><li><strong>Negative integer:</strong> The first item comes <em>before</em> the second item.</li><li><strong>Zero:</strong> Both items are considered equal in ranking.</li><li><strong>Positive integer:</strong> The first item comes <em>after</em> the second item.</li></ul><h3>Common APIs That Depend on Comparators:</h3><ul><li>sortedWith()</li><li>maxWith()</li><li>minWith()</li></ul><p>Comparators shine brightly when you start combining multiple selectors to build custom rules:</p><pre>// Sort by department, then by salary descending, then by name<br>employees.sortedWith(<br>    compareBy&lt;Employee&gt; { it.department }<br>        .thenByDescending { it.salary }<br>        .thenBy { it.name }<br>)</pre><h3>4. The Transformer (Mapper): “Convert this into something else”</h3><h4>The Signature</h4><p>(T) -&gt; R</p><p>A <strong>Transformer</strong> takes an object and maps it into an entirely new value or representation. Unlike a selector, a transformer isn’t limited to just grabbing an existing field. It can compute fresh data, combine properties, or completely rewrite the data layer.</p><pre>val toDto: (User) -&gt; UserDto = { UserDto(it.id, it.name) }</pre><h3>Common APIs That Depend on Transformers:</h3><ul><li>map()</li><li>flatMap()</li><li>mapValues()</li></ul><p>Transformers are the chameleons of functional programming. The inputs and outputs can belong to entirely different paradigms:</p><ul><li>Int → String</li><li>User → UserDto</li><li>JsonString → DomainModel</li><li>Bitmap → ByteArray</li></ul><h3>The Subtle Distinction: Selector vs. Transformer</h3><p>If you look closely at the type signatures for both a Selector and a Transformer, you’ll notice they look identical on paper: (T) -&gt; R.</p><p>The difference isn’t mechanical — it’s <strong>intent</strong>.</p><p>Imagine you have a basic domain model:</p><pre>data class Person(val name: String, val age: Int)</pre><ul><li><strong>Selector Intent:</strong> { it.age } — You are reaching into an object to extract an existing field to use as a key for sorting, grouping, or matching.</li><li><strong>Transformer Intent:</strong> { &quot;${it.name} (${it.age})&quot; } — You are constructing a brand new string value or completely changing the data&#39;s shape for presentation or transmission.</li></ul><p>A selector is technically a simple transformer from the type system’s perspective, but conceptually, they serve entirely different goals in your architecture.</p><h3>Quick Reference Summary</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EXiMwfpxrAqhWJ-KaCqBjw.png" /></figure><h3>Final Thoughts</h3><p>Modern collection pipelines and functional APIs are elegantly engineered out of these exact microscopic building blocks.</p><p>The next time you are chaining operators together in your codebase, don’t just think of them as ambiguous lambdas. Pause and ask yourself:</p><ul><li><em>Am I answering a yes/no question?</em> → <strong>Predicate</strong></li><li><em>Am I pointing to a specific key?</em> → <strong>Selector</strong></li><li><em>Am I defining a custom ordering rule?</em> → <strong>Comparator</strong></li><li><em>Am I altering the data structure?</em> → <strong>Transformer</strong></li></ul><p>Shifting your vocabulary from raw syntax to intent makes your code vastly easier to reason about, helps you write cleaner shared APIs, and makes functional programming patterns feel like second nature.</p><h4><strong>P.S.</strong></h4><p>These aren’t the only function types you’ll encounter. Functional programming also commonly uses Consumers (perform side effects), Suppliers (produce values), Reducers (combine values), and BiFunctions (operate on two inputs). However, Predicates, Selectors, Comparators, and Transformers are by far the most common abstractions you’ll see in collection-processing APIs.</p><p>👏🏻👏🏻 <strong>Your claps help this article reach more readers.</strong></p><p><strong>Thanks for making it to the end — hope you found something useful here.</strong></p><p><strong>Stay curious. 💡</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a9edfbaeb2c4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Daemon vs. Non-Daemon Threads in Kotlin]]></title>
            <link>https://medium.com/@hsinha610/daemon-vs-non-daemon-threads-in-kotlin-2c14153d5993?source=rss-8da3b4d8e24e------2</link>
            <guid isPermaLink="false">https://medium.com/p/2c14153d5993</guid>
            <category><![CDATA[threads]]></category>
            <category><![CDATA[daemon]]></category>
            <category><![CDATA[android-development]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Harshit Sinha]]></dc:creator>
            <pubDate>Thu, 02 Jul 2026 22:09:55 GMT</pubDate>
            <atom:updated>2026-07-02T22:10:09.004Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Dnjbu3qOMjzFbkec" /><figcaption>Photo by <a href="https://unsplash.com/@sebastiansvenson?utm_source=medium&amp;utm_medium=referral">Sebastian Svenson</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>If you are building responsive applications in Kotlin, you are inevitably working with multiple threads. But under the hood, not all threads are treated equally by the system. Flipping a single configuration switch — isDaemon = true— fundamentally changes how a thread behaves.</p><p>Understanding this distinction is the secret to preventing memory leaks, avoiding background battery drain, and ensuring your application shuts down cleanly.</p><h3>First Things First: What is a Thread?</h3><p>At its core, a <strong>thread</strong> is a single sequential path of execution within a program — the actual “worker” executing your lines of code.</p><p>Your app starts with one default path (the Main thread), but you can spawn additional threads to handle background tasks simultaneously, like downloading a file while keeping the UI smooth.</p><p><em>(You can read more about Threads </em><a href="https://medium.com/@hsinha610/what-is-a-thread-really-14e941677d2e?sharedUserId=hsinha610"><em>here</em></a><em>).</em></p><p>In Kotlin, threads are instances of the standard Thread class and can be created effortlessly:</p><pre>import kotlin.concurrent.thread<br><br>thread {<br>    println(&quot;I am running on a separate thread!&quot;)<br>}</pre><p>However, every thread you create falls into one of two distinct categories: <strong>Non-Daemon Threads</strong> or <strong>Daemon Threads</strong>.</p><h3>The Two Types: Daemon vs. Non-Daemon</h3><p>The runtime environment (the JVM or Android Runtime) divides threads based on their purpose and priority.</p><h4>1. Non-Daemon Threads (User Threads)</h4><p>These are high-priority threads designed to execute your core application logic. They represent the primary tasks your application was built to accomplish.</p><ul><li><strong>Android Example:</strong> The <strong>Main Thread (UI Thread)</strong> is the ultimate non-daemon thread. It handles user inputs, draws the UI, and manages Android component lifecycles. <br>Similarly, background threads allocated by <strong>WorkManager</strong> to upload a user’s photo or write a transaction to a local database are non-daemon threads.</li><li><strong>Why?</strong> The system must guarantee that these tasks run to completion. If a user is saving a file, cutting off the thread mid-write would result in data corruption.</li></ul><h4>2. Daemon Threads (Background Helpers)</h4><p>These are low-priority helper threads that run quietly in the background. They do not execute core business logic; instead, they exist solely to provide services to non-daemon threads.</p><ul><li><strong>Android Example:</strong> If you profile a running Android app, you will see system-managed daemon threads like <strong>GCDaemon</strong> (the Garbage Collector) and <strong>FinalizerDaemon</strong> (which cleans up object memory).</li><li><strong>Why?</strong> These threads only matter if the application is running. For instance, memory management is a pure helper task. If your active user threads finish or the app closes, there is zero reason for the garbage collector to keep running in an empty process.</li></ul><h3>How They Affect the Application Lifecycle</h3><p>The absolute biggest difference between these two types of threads is <strong>how they control the death of your application.</strong></p><ul><li>The runtime <strong>will not shut down</strong> your application process as long as there is even one non-daemon thread still running.</li><li>The runtime <strong>does not care</strong> if daemon threads are finished. The exact millisecond the last non-daemon thread finishes its job, the system kills all remaining daemon threads instantly and terminates the application.</li></ul><h3>The Lifecycle Impact in Code</h3><p>By default, any thread you manually create in Kotlin inherits its daemon status from the thread that created it (usually making it non-daemon). Here is how modifying that property alters your app’s lifecycle:</p><pre>import kotlin.concurrent.thread<br><br>fun main() {<br><br>    // 1. Spawning a Non-Daemon Thread<br>    thread(isDaemon = false) {<br>        println(&quot;Processing critical payment data...&quot;)<br>        Thread.sleep(2000)<br>        println(&quot;Payment processed successfully!&quot;) // This will print<br>    }<br><br>    // 2. Spawning a Daemon Thread<br>    thread(isDaemon = true) {<br>        while (true) {<br>            println(&quot;Logging analytics heartbeat...&quot;) // This gets cut off abruptly<br>            Thread.sleep(500)<br>        }<br>    }<br><br>    println(&quot;Main thread is finished!&quot;)<br><br>    // LIFECYCLE BEHAVIOR:<br>    // The main thread finishes, but the app process stays alive because the <br>    // payment thread is non-daemon. The app waits 2 seconds for it to finish.<br>    // The moment the payment thread finishes, the remaining heartbeat daemon <br>    // thread is killed instantly, and the app finally closes.<br>}</pre><h3>Summary</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zzBaRQndHB9pkyRoCwsREA.png" /></figure><p>When writing concurrent code in Kotlin, always ask yourself: “If the user closes the app, should this thread keep the process alive?” If the answer is no, flip isDaemon = true.</p><p>👏🏻👏🏻 <strong>Your claps help this article reach more readers.</strong></p><p><strong>Thanks for making it to the end — hope you found something useful here.</strong></p><p><strong>Stay curious. 💡</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2c14153d5993" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Beyond the Hype: The Fundamental Architecture Difference Between CPUs and GPUs]]></title>
            <link>https://medium.com/@hsinha610/beyond-the-hype-the-fundamental-architecture-difference-between-cpus-and-gpus-8cecc55ad476?source=rss-8da3b4d8e24e------2</link>
            <guid isPermaLink="false">https://medium.com/p/8cecc55ad476</guid>
            <category><![CDATA[tpu]]></category>
            <category><![CDATA[cpu-vs-gpu]]></category>
            <category><![CDATA[cpu]]></category>
            <category><![CDATA[gpu]]></category>
            <category><![CDATA[computation]]></category>
            <dc:creator><![CDATA[Harshit Sinha]]></dc:creator>
            <pubDate>Mon, 22 Jun 2026 20:40:33 GMT</pubDate>
            <atom:updated>2026-06-22T20:40:33.686Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*lXjAnl0bWh_x5DFd" /><figcaption>Photo by <a href="https://unsplash.com/@manueljota?utm_source=medium&amp;utm_medium=referral">Manuel</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Whenever someone says, <em>“GPUs are far better than CPUs,”</em> they usually mean that GPUs can perform massive amounts of parallel computation much faster for specific workloads.</p><p>But GPUs are not universally better.</p><p>They are just optimized for a fundamentally different style of computation.</p><p>If you are trying to understand why AI, graphics, and scientific computing are utterly dependent on GPUs — while your operating system still relies completely on a CPU — it all comes down to a core architectural divide: <strong>Latency vs. Throughput.</strong></p><h3>Philosophy</h3><p>Imagine you need to solve 10,000 math problems. How you approach this depends entirely on your workforce strategy.</p><h4>The CPU Philosophy: A Elite Team of Engineers</h4><p>A CPU is like having <strong>4 to 64 extremely intelligent engineers</strong>.</p><ul><li>Each engineer can solve almost any complex, multifaceted problem thrown at them.</li><li>They make decisions quickly, handle sudden changes in plans, and multitask efficiently.</li><li><strong>The Goal:</strong> Make a single, complex task finish as fast as possible.</li></ul><h4>The GPU Philosophy: An Army of Factory Workers</h4><p>A GPU is like having <strong>10,000+ average factory workers</strong>.</p><ul><li>Individually, each worker isn’t very smart and can only perform simple, repetitive arithmetic operations.</li><li>However, they all work simultaneously in perfect synchronization.</li><li><strong>The Goal:</strong> Maximize the total volume of work completed per second.</li></ul><h3>What Sits Inside the Silicon?</h3><p>The physical layout of these processors reflects these opposing philosophies. The way they spend their “transistor budget” is entirely different.</p><pre>CPU CORE                         GPU CORE Cluster<br>+--------------------------+     +--------------------------+<br>|      Control Logic       |     |  Control  |     Cache    |<br>| (Branch Predict / Sched) |     +-----------+--------------+<br>+--------------------------+     | ALU | ALU | ALU | ALU |  |<br>|       Large Cache        |     | ALU | ALU | ALU | ALU |  |<br>+--------------------------+     | ALU | ALU | ALU | ALU |  |<br>|         ALU / FPU        |     | ALU | ALU | ALU | ALU |  |<br>+--------------------------+     +--------------------------+</pre><h4>1. Inside a CPU</h4><p>A modern CPU dedicates a massive portion of its hardware budget to things <em>other</em> than pure math. It features sophisticated control logic, including:</p><ul><li><strong>Large L1/L2/L3 Caches:</strong> To keep data as close to the processor as possible and eliminate waiting times.</li><li><strong>Branch Predictors:</strong> To guess which way an if/else statement will go before it even executes.</li><li><strong>Out-of-Order &amp; Speculative Execution Engines:</strong> To re-arrange instructions on the fly, ensuring the processor never sits idle.</li></ul><p><strong>The Result:</strong> A huge amount of silicon is spent making sure a single stream of instructions (a thread) runs at blistering speeds.</p><h4>2. Inside a GPU</h4><p>A GPU strips away almost all of that control complexity. It throws out heavy branch prediction and massive individual caches. Instead, it packs the silicon with rows upon rows of <strong>ALUs (Arithmetic Logic Units)</strong>.</p><p><strong>The Result:</strong> The GPU sacrifices flexibility and single-thread speed to achieve raw mathematical throughput.</p><h3>Latency vs. Throughput</h3><p>To truly understand how they perform, we must look at the two dominant metrics in computing.</p><h4>CPU: Optimizing for Latency</h4><p>Latency is the time taken to complete a <em>single</em> task from start to finish.</p><blockquote><strong><em>The CPU Question:</em></strong><em> </em>“How fast can I execute this specific line of code right now?”</blockquote><p>CPUs excel at workloads with unpredictable logic or tasks that require immediate feedback:</p><ul><li>Opening a web browser</li><li>Running a complex SQL database query</li><li>Handling user mouse and keyboard inputs</li><li>Booting an operating system</li></ul><h4>GPU: Optimizing for Throughput</h4><p>Throughput is the <em>total volume</em> of work completed in a given unit of time.</p><blockquote><strong><em>The GPU Question:</em></strong><em> </em>“How many millions of operations can I finish in the next second?”</blockquote><p>GPUs excel at highly repetitive, predictably structured workloads:</p><ul><li>Rendering 8 million pixels on a 4K screen 60 times a second</li><li>Processing and decoding video frames</li><li>Training neural networks and multiplying massive matrices</li></ul><h3>Why AI and Matrix Math Love GPUs</h3><p>Deep learning is essentially massive-scale linear algebra. Consider a standard matrix multiplication <strong><em>A x B = C </em></strong>for a large AI model layer, like a <strong><em>4096 x 4096</em></strong> matrix.</p><p>Computing this requires millions of independent dot products.</p><ul><li>To calculate the value of cell C[1][1], you don&#39;t need to know the answer to C[1][2].</li><li>Because these calculations have zero dependencies on one another, they can be calculated completely in parallel.</li></ul><p>On a CPU, even a 64-core beast has to loop through these chunks sequentially. A GPU, however, assigns Worker 1 to C[1][1], Worker 2 to C[1][2], Worker 3 to C[1][3], and so on. Thousands of cells are calculated at the exact same clock tick.</p><h3>Where GPUs Lags: The Branching Problem</h3><p>If GPUs are so fast, why don’t we run everything on them? Because of a phenomenon known as <strong>thread divergence</strong>.</p><p>Suppose your code contains highly unpredictable logic:</p><pre>if (user.isPremium) {<br>    // Execute complex Step A<br>} else {<br>    // Execute complex Step B<br>}</pre><p>GPUs group their thousands of small cores into teams (often called <em>Warps</em> or <em>Wavefronts</em>) that must all execute the exact same instruction at the same time <strong>(SIMD: Single Instruction, Multiple Data).</strong></p><p>If Worker 1 and Worker 3 process premium users, but Worker 2 and Worker 4 process free users, they cannot run simultaneously. The GPU has to pause one group, run the if branch, and then run the else branch. This serialization causes performance to plummet.</p><p>CPUs, with their advanced branch predictors, handle this chaotic logic effortlessly.</p><h3>The Hidden Engine: Memory Bandwidth and FLOPS</h3><p>The difference in performance is also starkly visible in raw hardware specifications:</p><h4>1. Memory Bandwidth</h4><p>Feeding thousands of arithmetic units requires a massive firehose of data.</p><ul><li><strong>High-end CPUs:</strong> Typically top out around <strong>100–200 GB/s</strong> using DDR5 memory.</li><li><strong>Modern AI GPUs:</strong> Utilizing specialized HBM (High Bandwidth Memory) or GDDR6, reach speeds between <strong>1 to 5+ TB/s</strong>.</li></ul><p>Without this extreme bandwidth, a GPU’s arithmetic units would constantly sit idle waiting for data to arrive.</p><h4>2. FLOPS (Floating-Point Operations Per Second)</h4><p>When measuring raw mathematical speed, the gap becomes an abyss:</p><ul><li><strong>Modern Desktop CPUs:</strong> Deliver roughly <strong>1 to 2 TFLOPs</strong> (Trillion operations per second) of performance.</li><li><strong>Enterprise AI GPUs:</strong> Deliver anywhere from <strong>hundreds to thousands of TFLOPs</strong> depending on the numerical precision used (e.g., FP32 vs. FP16/INT8 for AI).</li></ul><h3>Summary</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4K7FiBW55OSxfwiqV6E44Q.png" /></figure><p>A CPU spends most of its silicon budget making a few execution threads run extremely fast, while a GPU spends its budget enabling thousands of threads to run simultaneously.</p><p>Neither is inherently “better” — they are the yin and yang of modern computing, dividing the digital world between complex logic and raw computational power.</p><h3>Extra: Beyond the GPU — Enter the TPU</h3><p>While the GPU revolutionized AI by adapting its massive graphics pipeline to neural networks, it is still technically a <em>general-purpose</em> processor — it was originally built to handle video games and physics simulations. To squeeze out even more efficiency for deep learning, Google introduced the <strong>TPU (Tensor Processing Unit)</strong>, a type of ASIC (Application-Specific Integrated Circuit).</p><p>If a CPU is an elite group of engineers and a GPU is an army of factory workers, a TPU is a <strong>highly automated, single-purpose assembly line built exclusively to multiply matrices</strong>.</p><p>Instead of using standard instruction pipelines, TPUs rely on an architectural design called a <strong>Systolic Array</strong>. In this setup, data flows through a massive 2D grid of hardwired multipliers like fluid pumping through a heart. It calculates entire matrix layers in a single clock cycle without constantly reading and writing back to memory registers.</p><p>By stripping away every ounce of remaining general-purpose computing logic, the TPU achieves staggering speed and energy efficiency — but it can <em>only</em> do deep learning math. If you tried to run an operating system or a video game on a TPU, it wouldn’t even boot. It represents the ultimate trade-off: sacrificing all flexibility for absolute dominance in AI.</p><p>👏🏻👏🏻 <strong>Your claps help this article reach more readers.</strong></p><p><strong>Thanks for making it to the end — hope you found something useful here. Stay curious. 💡</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8cecc55ad476" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>