Skip to content

scripts/config: Use ERE (-E) and in-place editing (-i) in sed portably

Rong Zhang linux-kbuild lkml 2 versions – In review

Get series with b4
b4 am [email protected] change-id b2d6414d-config-sed-633649d7b0de

Versions newest first

  1. v2 Latest2 patches 1 replyIn review
  2. v12 patches 2 repliesSuperseded

Changes in v2 from the cover letter

- Fix a typo in commit message (thanks Nicolas Schier)
- Do not delete .swp file when sed is not invoked, e.g., `--state'
- In my test, unnecessarily deleting .swp file makes `--state' 10% slower due to the overhead of extra fork() and execve()
- Unnecessarily deleting .swp file may corrupt Vim even if no editing command is passed
- Link to v1: https://patch.msgid.link/[email protected]

Patches in v2 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/2scripts/config: Use POSIX standard ERE (-E) in sedscripts/config+7 -100 replies
2/2scripts/config: Use in-place editing (-i) in sed portablyscripts/config+19 -120 replies

Reviews trailers per patch

All 2 patches have a Reviewed-by or Acked-by of their own; the series has one for all of them.

PatchNicolas Schier
SeriesR-b
1/2R-b
2/2R-b

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v2 thread

Cover letter [PATCH v2 0/2] scripts/config: Use ERE (-E) and in-place editing (-i) in sed portably

The use of Extended Regular Expressions and in-place editing was removed by commit 83e8b90e1d2c ("scripts/config: use sed's POSIX interface").

Before that, the script used -r' to enable ERE, which is indeed non-portable. However, POSIX.1-2024 [1][2] has accepted -E' as a standard option to use ERE for matching, and major sed implementations (GNU, FreeBSD, OpenBSD, NetBSD, macOS) have supported `-E' for over two decades, so it makes no sense to use Basic Regular Expressions any more. Hence, we can safely take the advantage of ERE and merge chained calls to sed.

Before the said commit, the script used bare -i' to skip creating a backup file. In fact, major sed implementations have supported -i' for over a decade. It's really doubtful if anyone would still build Linux on a Unix system without it. The issue is more about how we use it:

FreeBSD and macOS disallow bare -i'. To skip creating a backup, an empty string ("zero-length extension") must be passed as a separate argument following -i'.

GNU and other BSDs accept bare `-i' to skip creating a backup, but disallow passing a zero-length extension.

That being said, when thinking about it optimistically, using `-i' is portable as long as a backup is created. Hence, creating a backup file with a .swp extension (the same name as the current temporary file) anyway makes it portable. The backup file will be deleted on exit.

A rough benchmark with ~1000 editions showed a 48.6% speedup (8.78s => 4.51s, GNU sed) in total. The FreeBSD sed showed a similar speedup.

I split the series into two patches so that PATCH 2 can be dropped if strict POSIX compliance is desired.

---
Changes in v2:
- Fix a typo in commit message (thanks Nicolas Schier)
- Do not delete .swp file when sed is not invoked, e.g., `--state'
- In my test, unnecessarily deleting .swp file makes `--state' 10% slower due to the overhead of extra fork() and execve()
- Unnecessarily deleting .swp file may corrupt Vim even if no editing command is passed
- Link to v1: https://patch.msgid.link/[email protected]

---
Rong Zhang (2):
scripts/config: Use POSIX standard ERE (-E) in sed scripts/config: Use in-place editing (-i) in sed portably

 scripts/config | 42 +++++++++++++++++++++++-------------------
 1 file changed, 23 insertions(+), 19 deletions(-)

---
base-commit: 1590cf0329716306e948a8fc29f1d3ee87d3989f
change-id: b2d6414d-config-sed-633649d7b0de

Thanks,
Rong