Vivek Akupatni

Welcome to my personal blog!

Using Golang Iterators for Efficiency

TL;DR In this blog post, we will see how new golang iterators1 enables us to write efficient code. Although, we talk about iterators in golang, same argument holds for other languages as well2. Traditional Approach Consider a hypothetical example of writing powerset function to generate all possible subsets: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 // We are using slice instead of set func PowerSet[T any](values []T) [][]T { if len(values) == 0 { return nil } results := make([][]T, 0) for i := 0; i < int(math.Pow(2, float64(len(values)))); i++ { result := make([]T, 0) for j := 0; j < len(values); j++ { if i&(1<<j) != 0 { result = append(result, values[j]) } } results = append(results, result) } return results } What is going on here: ...

November 10, 2024 · 581 words · Vivek Akupatni

Evaluating Alternate Variations of Kosaraju Sharir Algorithm for Computing Strongly Connected Components

TL;DR In this blog post, we will explore examples illustrating why alternate variations of Kosaraju-Sharir’s algorithm for computing strongly connected components fail. Prerequisites I assume that reader is already familiar with the below topics: Strongly connected component problem Rough idea of Kosaraju Sharir Algorithm1. Finally, a handy reference to clrs book as we will be using the conceptual framework from this book. Terminology Discovery time of a vertex: When a vertex is first discovered. Denoted as v.discovery finish time of a vertex: When the vertex is fully explored and recursive dfs function for the vertex returns. Denoted as v.finish Conventions used: An edge being explored is in bold red color and an edge in blue color is part of dfs tree. Time is monotonically increasing and it starts from 1. discovery_time/finish_time times appear below the node. Nodes are that fully processed are in blue color and currently being explored are in red color. Consider an example of running dfs from a vertex a ...

February 3, 2024 · 621 words · Vivek Akupatni

Thoughts on GoLang - Part 1

Couple of years ago, one of my friends wanted me to look at golang. Back then, golang was a shiny language and there was a lot of hype around it. I briefly look at the language and I had two primary concerns back then: Lack of generics: No longer exists now as generics were added in go 1.181. Better ergonomic error handling: This problem even exists at the time of writing. Most of the times I see a common pattern mentioned below: // common pattern result, err := fetch_value() if err != nil { return nil, err } ​ I wish there was a way2 in which I can return an error if I don’t want to deal with it. Something along the lines of: ...

March 26, 2023 · 596 words · Vivek Akupatni

SQLite Tips

In this post, I will go over some of the useful operations that can be performed over sqlite files. I always keep forgetting about this, so I decided to write a blog post. Assumptions: All the below examples assume that we have a sqlite file named social_db.sqlite3 . If you don’t have sqlite3 on your machine, you can get the latest precompiled binary from here. All the examples can be executed interactively as well. ...

January 28, 2023 · 1611 words · Vivek Akupatni

Preallocate Java's ArrayList And HashMap For Better Performance

TL;DR Preallocation avoids unnecessary memory reallocations due to dynamic resizing, copying of the data, rehashing for maps and puts less pressure on GC1. Although we will only look at Java, the same behavior exists in c++, rust and many other languages as well. ArrayList There are two ways to create an ArrayList . Method 1: Created with zero argument constructor. This uses a default capacity of size 102. var arr = new ArrayList<String>(); arr.add(elem) // add something later Method 2: Provide an initial capacity with int argument constructor. ...

August 21, 2022 · 1105 words · Vivek Akupatni

Migration from Wordpress to Jekyll

I migrated this site that you are reading from Wordpress to Jekyll recently. In the last couple of months, I encountered many issues with Wordpress that prompted me to look for better alternatives. Some of the issues I faced with Wordpress: Wordpress plugins breaking on me during upgrade Lack of documentation or incompatibilities between plugins Themes not properly working with certain plugins I experienced slow load time1 Difficulty to keep my local setup consistent with my production setup Honestly, the issues I faced with Wordpress can be resolved given sufficient time and budget. However, I felt that static site generators are more appropriate for my use case and there was no need for powerful cms like Wordpress. ...

July 23, 2022 · 257 words · Vivek Akupatni

Python Logging Good Practices

Logging library in python has been in standard library since a very long time. In many aspects, this logging framework bears similiarties with log4j libraries in Java. In this blog post, I will go over some of the good approaches when using python log formatting. Avoid using root logger Avoiding root logger provides the following benefits: Better descriptive names regarding where the log originated from. Helps during searching of logs Allows better log customization Many times, we find it quite easy to start logging using logging.info/logging.error methods. However, using any of the methods under logging module will result in root logger being invoked. Take a look at the examples below involving two modules: logger_usage.py and user_record.py ...

July 9, 2022 · 671 words · Vivek Akupatni

Populating Java PriorityQueue Efficiently

TL;DR Use PriorityQueue constructor(s) to populate elements in queue instead of addAll, add or offer methods Couple of weeks back, I was going over PriorityQueue docs in Java and wondered if there is any difference in performances over how Priorityqueue is constructed. After some analysis, I realized that it does make a difference how tree is constructed. In this blog post, I try will to explain the reasons and provide some actual benchmarks with some sample test cases. ...

October 10, 2021 · 627 words · Vivek Akupatni

My First Blog Post

This is my first blog post. After going back-and forth between different choices for building websites, I choose WordPress. My plan is to write at least one post per month about new stuff that I learn and find interesting.

September 21, 2021 · 39 words · Vivek Akupatni