tech-pkg archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: Doubling performance of pkgsrc/mk



    Date:        Thu, 20 Oct 2022 18:20:44 +0100
    From:        Jonathan Perkin <jperkin%mnx.io@localhost>
    Message-ID:  <Y1GDbKFmYy15SMhZ%mnx.io@localhost>

  | >I wonder, is "${TEST} -d ${.TARGET:H} || ${MKDIR} ${.TARGET:H}" really 
  | >faster than just "${MKDIR} ${.TARGET:H}"?
  |
  | If it's only ever called once then no, but if there's any chance it 
  | might be called more than once then yes it makes a difference.

It isn't so much the number of calls that (should) make a difference,
but the context in which the calls occur

If we have just

	setup_target_dir: ${.TARGET:H}
		${MKDIR} ${.TARGET:H}

vs

	setup_target_dir: ${.TARGET:H}
		${TEST} -d ${.TARGET:H} || ${MKDIR} ${.TARGET:H}

then I'd expect the former to be faster, no matter how many times it
is run, as the second demands that make exec a shell, but for the
first, make can simply fork/exec mkdir, and that is going to be faster
to run than any shell's startup time.

On the other hand, if make isn't making that optimisation and always execs
a shell to run commands, then the second will be faster, since the shell
startup costs exist anyway, and in the second, all that will usually run
is the test, which amounts to a single stat() or lstat() call (depending
on just how ${TEST} is defined) whereas for the former, the shell needs
to always exec mkdir, which is going to be slower.   The trivial extra
cost of a single stat() in the case it fails, or finds something not a
directory, is immaterial compared to the cost of the exec().

But if the usage is more like

	setup:  <whatever it depends upon>
		# long script of sh commands amongst which
		# is the ${MKDIR} of ${.TARGET:H}

then there's also always going to be a shell, and most likely,
it will need to fork() as well as exec(mkdir) which is an even
bigger cost which can be avoided if the target directory already
exists.   In that kind of usage, the version with the test so the
mkdir can be avoided is almost guaranteed to be lots faster.

All of this depends upon test being a built-in command in the shell,
but that's more or less a given for any shell these days.

kre



Home | Main Index | Thread Index | Old Index