Empty main street in peak hour

Learning every week – 27-Mar-2020

I normally work from home yet this seems strange to me. That’s because my usual schedule has me going to different cafes. I am also taking a break from work this week. This means that my day is spent on reading, experimenting, and learning new things. And movies and games too. I’m quite close to completing the main quest-line of Assassin’s Creed: Origins and I will get back to Red Dead Redemption 2.

It also means I was able to complete a lot of smaller tasks for which I didn’t have the time before. I learnt many things along the way and I’m going to write all about it here (or as much as I can).

History API in browsers

It’s a while since I had to write non-trivial functionality in vanilla JavaScript. On one of my websites, I had implemented a simple tab-based layout. There was user feedback from a long time that using browser back button would not bring back the user to the tab they left. I knew what I had to implement but I had only built such things using routers in frameworks like Angular or React. I didn’t have the first clue about how to do this with vanilla JavaScript.

In my first round of search for this, I found that this needs to be implemented using the History API. I had used this API only for things like sending the user back. I tried finding an abstraction to make it simpler but it turned out that the History API is simple enough to use. After reading through several articles, I found one which was very suitable for what I was trying to achieve. It took about a couple of hours worth of research and implementation to implement this.

YADAT (Yet Another Drupal Admin theme)

You might have heard of Claro that’s an experimental theme in Drupal 8.8 and would be default soon. This week, I found another theme called Gin and wanted to try it out. It looks really polished and clean and with a much better experience compared to Claro when it came out (to be fair, I tried it really early and I am sure Claro is much better now as well, if not the same). Gin is based on Claro but I am not sure of how much is different. I am looking forward to when Claro gets stable and becomes the default.

The only problem I faced with Gin was that when I enabled it, it picked up all the block configuration of my regular front-end theme. And since there are not as many regions, everything got placed in the header. I spent about 10 mins removing all the blocks one at a time from the header.

GitLab migration

At work, we are moving to another GitLab instance which is provisioned in a different way. Due to various reasons, we could not do an in-place upgrade. We started out migrating the project by hand but, as I have said many times before, I am lazy and I couldn’t keep doing that. I looked up GitLab API and found that there is an API endpoint for importing and exporting projects. Using this, I was able to migrate about 145 projects in about half a day. And it took about half the day to write and test the Python script itself. I think that migrating 145 projects is good for a day’s work.

A faster Docker on macOS

You might know that Docker performance leaves a lot to be desired on non-Linux systems. The reason is the filesystem. The default mounting options are quite slow and Docker provides a way to switch it. One popular method is to use NFS while mounting the volumes so that sync between host and Docker containers are faster. I use Lando for reproducing local environments and there is no built-in option to use NFS. There is an open issue which has some steps which appear outdated. I adapted the steps but the database container kept crashing whenever I loaded a heavy page.

I checked the driver options but they are the same as documented on various sites. My next guess was that the database container had the directory mounted using the regular mount and that might cause problems. I got more confidence with the method after seeing a recent blog post by geerlingguy. After using NFS to mount the volume in all containers, the results were better but it still eventually crashed. The problem is with Docker rather than Lando. After the container crashes, Docker stops responding and the only way is to restart. I didn’t stick with this and it’s a problem for another day.

Concurrency in Go

I finally continued with the Exercism exercise on concurrency. I learnt about sync.Map and watched a lightning talk about it. Ultimately, I implemented it using the mutex approach as described here. At the same time, I also learnt about WaitGroups. My solution involved me calling another function and then looping over the result and summing all the counts. I wanted to avoid the double loop and tried to directly loop over the string to count the letters. This was twice as slow, even though I removed a loop and a function call.

I think this was because of the performance problems with mutexes as described in the video. Iterating over the string ourselves meant that we are locking and unlocking the mutex many more times. When we just loop over the results (as in my solution linked above), we are locking only a few times. I can avoid all that with sync.Map() but I don’t see how to do that without changing the signature of Frequency() function. The tests use this function and I can’t change the tests. In any case, I can’t think of how to do this with sync.Map() without mutexes as there is no atomic mutation. There are only atomic store and load but we need to load a value, update it, and store it back. The only way to do this without a race condition would be to use a mutex again.

Based on mentor feedback, I rewrote this using channels. The mentor mentioned this Go proverb: “Don’t communicate by sharing memory; share memory by communicating.” I found that the channel approach is much slower for the tests in Exercism but it is much more scalable.

That’s it for this week. More next week.


Posted

in

by

Comments

One response to “Learning every week – 27-Mar-2020”

  1. […] from the same topic from the last week: I was trying to figure out other solutions, particularly one where I could just wait on multiple […]

Leave a Reply

Your email address will not be published. Required fields are marked *