Reduce GNU Make latency 17% for cosmo

fgets() is now 4x faster which makes Make 2% faster. Landlock Make now
has a builtin $(uniq ...) function that uses critbit trees rather than
functional programming. Since uniq is the most important function this
optimization makes our cold start latency 15% faster.
This commit is contained in:
Justine Tunney 2022-08-19 15:29:24 -07:00
parent 8835b82a7c
commit d76dfadc7a
7 changed files with 96 additions and 11 deletions

View file

@ -23,6 +23,9 @@ this program. If not, see <http://www.gnu.org/licenses/>. */
#include "third_party/make/job.h"
#include "third_party/make/os.h"
#include "third_party/make/commands.h"
#include "libc/mem/critbit0.h"
#include "libc/log/rop.h"
#include "libc/runtime/runtime.h"
#include "third_party/make/debug.h"
struct function_table_entry
@ -1160,6 +1163,45 @@ func_sort (char *o, char **argv, const char *funcname UNUSED)
return o;
}
/*
chop argv[0] into words, and remove duplicates.
*/
static char *
func_uniq (char *o, char **argv, const char *funcname UNUSED)
{
char *p;
size_t len;
int mutated;
bool once = false;
const char *s = argv[0];
struct critbit0 t = {0};
while ((p = find_next_token (&s, &len)))
{
++s;
p[len] = 0;
RETURN_ON_ERROR ((mutated = critbit0_insert (&t, p)));
if (mutated)
{
if (once)
o = variable_buffer_output (o, " ", 1);
else
once = true;
o = variable_buffer_output (o, p, len);
}
}
critbit0_clear (&t);
return o;
OnError:
critbit0_clear (&t);
OSS (error, NILF, "%s: function failed: %s",
"uniq", strerror (errno));
exit (1);
}
/*
$(if condition,true-part[,false-part])
@ -2137,6 +2179,7 @@ static struct function_table_entry function_table_init[] =
FT_ENTRY ("value", 0, 1, 1, func_value),
FT_ENTRY ("eval", 0, 1, 1, func_eval),
FT_ENTRY ("file", 1, 2, 1, func_file),
FT_ENTRY ("uniq", 0, 1, 1, func_uniq),
#ifdef EXPERIMENTAL
FT_ENTRY ("eq", 2, 2, 1, func_eq),
FT_ENTRY ("not", 0, 1, 1, func_not),