The ultimate performance hack

I'm developing Bash SSG on Windows using BusyBox (because I'm a bit lazy). Rendering on Linux is noticeably faster than on Windows, even when Linux is running in a virtual machine (it's an older laptop - out of memory is my freud).

There is a simple way to bypass this inconvenience. This is because bash first looks for the function, and if it doesn't find it, it scans the PATH variable for the tool.

The problem is that creating a new process in Windows is expensive, and SSG uses external tools at almost every step (BusyBox won't help here).

One of the most commonly used tools is cat - it is mainly used to write files and heredocs to standard output.

While looking through my archived projects I remembered that I have a shellscript replacement for cat that doesn't use a subshell (functions readFile and readStream in the controller.sh file from the TCPServer HTTP controller project).

After combining both functions, the dog looks like this:

  1. cat()
  2. {
  3. local i
  4. local line
  5. if [ "${1}" = '' ]; then
  6. while IFS= read -r line || [ -n "${line}" ]; do
  7. echo "${line}"
  8. done
  9. return
  10. fi
  11. for i in ${@}; do
  12. if [ ! -f "${i}" ]; then
  13. echo "cat(): ${i} is not a file" >&2
  14. continue
  15. fi
  16. while IFS= read -r line || [ -n "${line}" ]; do
  17. echo "${line}"
  18. done < "${i}"
  19. done
  20. }

Copy the code above or download the ready file and place it in the database/defaults.rc.d/a_cat.rc file. This code works with bash, dash, and also with ash from BusyBox.

Warning

The above function may behave differently than the original cat.

For example, if the input file does not have a newline character at the end, one will be added.

If you follow the instructions, the replacement will be used by the generate script and all modules without exception.

Consider whether you need the exact behavior of the original cat, or whether it doesn't matter and the text is what matters most to you.

You can also call the original cat, e.g.: /usr/bin/env cat path/to/file

This trick significantly speeds up database rendering on Windows. On my machine, which is not particularly fast, rendering the sample database drops from 9 seconds to 4. However, on Linux, the difference may be negligible.

BTW, Windows handles low memory very poorly when the swap file is disabled. The entire program that is hogging memory should be killed immediately, but instead the OS may freeze and the only thing you can do is pull the plug. I kind of miss the days when I used Win2k with 2000lite.

Code copied to clipboard
Code copying error
Starting...