<?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 Vishnu Dev on Medium]]></title>
        <description><![CDATA[Stories by Vishnu Dev on Medium]]></description>
        <link>https://medium.com/@dev-vishnu?source=rss-b7e54406058b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*V7BmXYwTY122iuDi9bWghg.jpeg</url>
            <title>Stories by Vishnu Dev on Medium</title>
            <link>https://medium.com/@dev-vishnu?source=rss-b7e54406058b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 18:24:16 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@dev-vishnu/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[Save a byte]]></title>
            <link>https://dev-vishnu.medium.com/save-a-byte-82551a16cd8a?source=rss-b7e54406058b------2</link>
            <guid isPermaLink="false">https://medium.com/p/82551a16cd8a</guid>
            <category><![CDATA[backend]]></category>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[go-programming]]></category>
            <category><![CDATA[memory-improvement]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Vishnu Dev]]></dc:creator>
            <pubDate>Fri, 21 Mar 2025 20:32:00 GMT</pubDate>
            <atom:updated>2025-03-24T04:26:34.197Z</atom:updated>
            <content:encoded><![CDATA[<h3>Save a byte in your next Go program</h3><p>Hi Gophers,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/837/1*NucKlcq4xM-X299V8v4WaA.png" /></figure><p>No stories, drama, introduction.. just a simple tip for a memory efficient Go program.</p><blockquote>If you are using your Golang maps <strong>only to check existence of a key</strong> and value is irrelevant, try using <strong>struct{} </strong>as the value type and use <strong>struct{}{} </strong>as value for all keys instead of boolean values or integers. This is one of the best approaches.</blockquote><p>If the single-sentence message confused you, let me explain with an example for a better clarity 😀</p><p>Suppose you have a program which processes a slice of member IDs.</p><pre>var memberIDs = []string{&quot;M1&quot;, &quot;M2&quot;, &quot;M3&quot;, &quot;M2&quot;,  &quot;M4&quot;, &quot;M3&quot;, &quot;M1&quot;, &quot;M2&quot;}</pre><p>For each member ID, if it is the first occurrence we need to perform a certain operation, and for the rest of the duplicate occurrences, we need to perform another operation.</p><pre>for _, memberID := range memberIDs {<br>  // if first occurrence, operation 1<br>  // if duplicate occurrence, operation 2<br>}</pre><p>For this purpose we generally uses maps as a temporary in-memory storage, We will store the once processed memberID in the map in such a way that the <strong>memberID is the key</strong>. The value associated with the key is irrelevant here, so most of us choose any simple data type as the map value such as an <strong>empty string</strong> or integer <strong>0</strong>/<strong>1</strong> or boolean <strong>true</strong>/<strong>false</strong>.</p><pre>processedMap := make(map[string]bool)<br>for _, memberID := range memberIDs {<br>  if _, exists := processedMap[memberID]; !exists {<br>    // OPERATION - 1<br>    processedMap[memberID] = true<br>  }<br>  // OPERATION - 2<br>}</pre><p>Most of us leave this implementation as it is since it serves our purpose.</p><p>However, the problem here is that we use boolean(or any as mentioned above) as the value even it has no impact on our code logic. But a boolean (true/false) occupies 1 byte of memory in the RAM. Now think of a scenario where we are processing a large quantity of memberIDs, This approach would waste a significant number of bytes without any real use.</p><p>So to serve our purpose for a placeholder in the map’s value, we need a data type or data structre with <strong>minimal(or zero) memory allocation</strong>. This is where the struct{} type comes for our rescue. We can use <strong><em>struct{} as map value type</em></strong> <strong><em>and struct{}{} as the value placeholder</em></strong> for all associated keys. struct{}{} have very minimal or rather zero impact on memory usage. This is because the empty struct has no fields, so it does not need to require any internal memory allocation for fields. So our implementation will become,</p><pre>processedMap := make(map[string]struct{})<br>for _, memberID := range memberIDs {<br>  if _, exists := processedMap[memberID]; !exists {<br>    // OPERATION - 1<br>    processedMap[memberID] = struct{}{}<br>  }<br>  // OPERATION - 2<br>}</pre><h3><strong>KEY POINTS</strong></h3><ol><li>Use struct{} as the value type in maps when the value is irrelevant.</li><li>struct{}{} occupies zero bytes of memory, making it highly memory-efficient.</li><li>This approach is particularly useful when dealing with large datasets where memory optimisation is critical.</li></ol><p>Please do comment your thoughts on this 🙇‍♂</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=82551a16cd8a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[About me]]></title>
            <link>https://dev-vishnu.medium.com/about-me-53041716d661?source=rss-b7e54406058b------2</link>
            <guid isPermaLink="false">https://medium.com/p/53041716d661</guid>
            <dc:creator><![CDATA[Vishnu Dev]]></dc:creator>
            <pubDate>Tue, 15 Oct 2024 19:05:38 GMT</pubDate>
            <atom:updated>2025-03-21T20:35:55.512Z</atom:updated>
            <content:encoded><![CDATA[<p>A machine enthusiast</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*-ASETt1QewRj2-WV" /><figcaption><em>Gopher image by </em><a href="https://reneefrench.blogspot.com/"><em>Renee French</em></a><em>, licensed under </em><a href="https://creativecommons.org/licenses/by/3.0/"><em>Creative Commons 3.0 Attributions license</em></a><em>.</em></figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=53041716d661" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>