From 9088dc0baf19b9d4c46fb5bf0a37b32f9902c1cd Mon Sep 17 00:00:00 2001
From: Jelte Fennema-Nio <jelte.fennema@microsoft.com>
Date: Fri, 5 Apr 2024 00:38:02 +0200
Subject: [PATCH v2] Speed up clean parallel meson builds a lot

Building the generated ecpg preproc file can take a long time. You can
check how long using:

ninja -C build src/interfaces/ecpg/preproc/ecpg.p/meson-generated_.._preproc.c.o

This moves that file much closer to the top of our build order, so
building it can be pipelined much better with other files.

It improved clean build times on my machine (10 cores/20 threads) from ~40
seconds to ~30 seconds. Perfermance gains are obviously dependent on
compiler, compile flags and machine.

This also updates our Makefiles in the same way to stay consistent with
the meson files, but I'm unable to get similar wall-clock time
performance gains.
---
 src/Makefile                    | 2 +-
 src/interfaces/Makefile         | 2 +-
 src/interfaces/ecpg/Makefile    | 2 +-
 src/interfaces/ecpg/meson.build | 2 +-
 src/meson.build                 | 9 ++++++---
 5 files changed, 10 insertions(+), 7 deletions(-)

diff --git a/src/Makefile b/src/Makefile
index 2f31a2f20a7..069202f0bd0 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -13,6 +13,7 @@ top_builddir = ..
 include Makefile.global
 
 SUBDIRS = \
+	interfaces \
 	common \
 	port \
 	timezone \
@@ -20,7 +21,6 @@ SUBDIRS = \
 	backend/utils/mb/conversion_procs \
 	backend/snowball \
 	include \
-	interfaces \
 	backend/replication/libpqwalreceiver \
 	backend/replication/pgoutput \
 	fe_utils \
diff --git a/src/interfaces/Makefile b/src/interfaces/Makefile
index 7d56b29d28f..1f09925b213 100644
--- a/src/interfaces/Makefile
+++ b/src/interfaces/Makefile
@@ -12,7 +12,7 @@ subdir = src/interfaces
 top_builddir = ../..
 include $(top_builddir)/src/Makefile.global
 
-SUBDIRS = libpq ecpg
+SUBDIRS = ecpg libpq
 
 $(recurse)
 
diff --git a/src/interfaces/ecpg/Makefile b/src/interfaces/ecpg/Makefile
index 3002bc3c1bb..949f8513c19 100644
--- a/src/interfaces/ecpg/Makefile
+++ b/src/interfaces/ecpg/Makefile
@@ -2,7 +2,7 @@ subdir = src/interfaces/ecpg
 top_builddir = ../../..
 include $(top_builddir)/src/Makefile.global
 
-SUBDIRS = include pgtypeslib ecpglib compatlib preproc
+SUBDIRS = include preproc pgtypeslib ecpglib compatlib
 
 # Suppress parallel build of subdirectories to avoid a bug in GNU make 3.82, cf
 # https://savannah.gnu.org/bugs/?30653
diff --git a/src/interfaces/ecpg/meson.build b/src/interfaces/ecpg/meson.build
index ac42a70a31f..05db32c226e 100644
--- a/src/interfaces/ecpg/meson.build
+++ b/src/interfaces/ecpg/meson.build
@@ -3,9 +3,9 @@
 ecpg_targets = []
 
 subdir('include')
+subdir('preproc')
 subdir('pgtypeslib')
 subdir('ecpglib')
 subdir('compatlib')
-subdir('preproc')
 
 alias_target('ecpg', ecpg_targets)
diff --git a/src/meson.build b/src/meson.build
index 65c7d17d08f..6f3946ffd54 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -1,7 +1,12 @@
 # Copyright (c) 2022-2024, PostgreSQL Global Development Group
 
 # libraries that other subsystems might depend upon first, in their respective
-# dependency order
+# dependency order. Apart from "interfaces", which we put at the top because
+# that speeds up parallel builds significantly since our generated ecpg file
+# takes a very long time to build so we want to start that as soon as
+# possible.
+
+subdir('interfaces')
 
 subdir('timezone')
 
@@ -11,8 +16,6 @@ subdir('bin')
 
 subdir('pl')
 
-subdir('interfaces')
-
 subdir('tools/pg_bsd_indent')
 
 

base-commit: 8a1b31e6e59631807a08a4e9465134c343bbdf5e
-- 
2.34.1

