Optimizing a go package by 38147125738.8x

submited by
Style Pass
2024-11-20 07:00:04

I noticed the implemention is a little slower than i expected, so i went to investigate. I cached the inode to pid mapping for a open port lookup dependency and made the whole process 38147125738.8x faster.

The first low-hanging fruit catching my eye is the isNumeric function call, which attempts to parse the directory name and checks if it is a number to make sure the directory is the directory of a process. Using unicode.IsDigit on the first byte of the directory name should be sufficient and faster:

The informed reader will notice iterating the /proc/ dir every time we hit a new inode is not that smart. Instead we should do it once and look up found inodes in the inode to pid mapping:

I would go further on this, but the above change already results in such a large performance increase, I’m going to call it a day. However if it wouldn’t be much faster, my next steps would be to remove the fmt.Sprintf in the for-loop body. Beforehand I would spin up a profiler and investigate hot functions.

Leave a Comment