Optimizing existing code - a bash example
Daniel Nashed – 1 June 2025 07:11:34
Once code is implemented and works, it is usually not looked at again.
Often performance issues come up later when more data is added to an application.
This isn't just true for normal applications, but also for bash scripts as the following example shows.
The script already had verbose logging, so I could figure out quickly which part of the script takes longer.
But it wasn't clear immediately what the issue was.
Pasting code into ChatGPT asking the right questions gave me a good indication.
The code was parsing strings in a loop. For a single invocation for a full file using cut would be a good way to parse.
But it turned out that invoking cut for every operation took quite some overhead and high CPU spikes.
ChatGPT had an interesting suggestion, which did not work initially, but gave me a good direction.
I replaced the invocation of cut by internal bash parsing. This does not only reduce the overhead in CPU but is also dramatic faster.
Analyzing and re-factoring code can be very beneficial. But there needs to be potential in the optimization.
In may case it was simple. The server CPU spiked up for like 30 seconds on that Linux machine just for a bash script running to rebuild some HTML code.
I wasn't aware of the internal shell way to split strings this way into an array.
So asking ChatGPT and validating the ideas coming back can be very helpful.
But on the other side all of this only makes sense if there is optimization potential.
In my case it was easy to spot and address and I have other areas in another script that might benefit from the same type optimization.
Existing Code invoking an external command "cut"
while read LINE; do
ENTRY=$(echo "$LINE" | cut -d'|' -f1)
CATEGORY=$(echo "$ENTRY" | cut -d'/' -f1)
SUB=$(echo "$ENTRY" | cut -d'/' -f2)
COMBINED=${CATEGORY}_${SUB}
FILE=$(echo "$LINE" | cut -d'|' -f2)
DESCRIPTION=$(echo "$LINE" | cut -d'|' -f3)
HASH=$(echo "$LINE" | cut -d'|' -f4)
html_entry "$COMBINED.html" "$FILE" "$FILE" "$DESCRIPTION" "$HASH" "$SERVER_URL"
done < "$CATALOG_FILE"
New Code leveraging bash internal functions
while read LINE; do
IFS='|' read -r -a PARTS <<< "$LINE"
ENTRY=${PARTS[0]}
FILE=${PARTS[1]}
DESCRIPTION=${PARTS[2]}
HASH=${PARTS[3]}
IFS='/' read -r -a PARTS <<< "$ENTRY"
CATEGORY=${PARTS[0]}
SUB=${PARTS[1]}
COMBINED=${CATEGORY}_${SUB}
html_entry "$COMBINED.html" "$FILE" "$FILE" "$DESCRIPTION" "$HASH" "$SERVER_URL"
done < "$CATALOG_FILE"
- Comments [0]