From f8156314eaf9d9df3cb4d20e281bacf5cd853017 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 v1] 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.
---
 src/backend/meson.build         |  9 +--------
 src/interfaces/ecpg/meson.build |  2 +-
 src/meson.build                 | 21 ++++++++++++++++++---
 3 files changed, 20 insertions(+), 12 deletions(-)

diff --git a/src/backend/meson.build b/src/backend/meson.build
index 436c04af080..f8f86c8a8ed 100644
--- a/src/backend/meson.build
+++ b/src/backend/meson.build
@@ -1,12 +1,6 @@
 # Copyright (c) 2022-2024, PostgreSQL Global Development Group
 
-backend_build_deps = [backend_code]
-backend_sources = []
-backend_link_with = [pgport_srv, common_srv]
-
-generated_backend_sources = []
-post_export_backend_sources = []
-
+# NB: parser is entered directly from the meson file in the src directory
 subdir('access')
 subdir('archive')
 subdir('backup')
@@ -21,7 +15,6 @@ subdir('libpq')
 subdir('main')
 subdir('nodes')
 subdir('optimizer')
-subdir('parser')
 subdir('partitioning')
 subdir('port')
 subdir('postmaster')
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..f331eb3b68c 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -1,7 +1,24 @@
 # Copyright (c) 2022-2024, PostgreSQL Global Development Group
 
+backend_build_deps = [backend_code]
+backend_sources = []
+backend_link_with = [pgport_srv, common_srv]
+
+generated_backend_sources = []
+post_export_backend_sources = []
+
 # 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. We also move backend/parser to the top for the same reason, but
+# also because we want to start building the parser before the ecpg files.
+# Otherwise an error in the grammer would confusingly show up as an issue in
+# the ecpg file.
+
+subdir('backend/parser')
+
+subdir('interfaces')
 
 subdir('timezone')
 
@@ -11,8 +28,6 @@ subdir('bin')
 
 subdir('pl')
 
-subdir('interfaces')
-
 subdir('tools/pg_bsd_indent')
 
 

base-commit: 0bd4b0689ba1f12fbbd9919ca76a71df3e7702a2
-- 
2.34.1

