[PATCH] Cygwin: Speed up mkimport

Mark Geisert mark@maxrnd.com
Thu Nov 26 09:56:20 GMT 2020


Cut mkimport elapsed time in half by forking each iteration of the two
time-consuming loops within.  Only do this if more than one CPU is
present.  In the second loop, combine the two 'objdump' calls into one
system() invocation to avoid a system() invocation per iteration.

---
 winsup/cygwin/mkimport | 34 ++++++++++++++++++++++++++--------
 1 file changed, 26 insertions(+), 8 deletions(-)

diff --git a/winsup/cygwin/mkimport b/winsup/cygwin/mkimport
index 2b08dfe3d..919dc305b 100755
--- a/winsup/cygwin/mkimport
+++ b/winsup/cygwin/mkimport
@@ -47,6 +47,9 @@ for my $sym (keys %replace) {
     $import{$fn} = $imp_sym;
 }
 
+my $ncpus = `grep -c ^processor /proc/cpuinfo`;
+my $forking = $ncpus > 1; # Decides if loops below should fork() each iteration
+
 for my $f (keys %text) {
     my $imp_sym = delete $import{$f};
     my $glob_sym = $text{$f};
@@ -56,25 +59,30 @@ for my $f (keys %text) {
 	$text{$f} = 0;
     } else {
 	$text{$f} = 1;
-	open my $as_fd, '|-', $as, '-o', "$dir/t-$f", "-";
-	if ($is64bit) {
-	    print $as_fd <<EOF;
+	if ($forking && fork) {
+	    ; # Testing shows sleep here is unneeded. 'as' runs very quickly.
+	} else {
+	    open my $as_fd, '|-', $as, '-o', "$dir/t-$f", "-";
+	    if ($is64bit) {
+		print $as_fd <<EOF;
 	.text
 	.extern	$imp_sym
 	.global	$glob_sym
 $glob_sym:
 	jmp	*$imp_sym(%rip)
 EOF
-	} else {
-	    print $as_fd <<EOF;
+	    } else {
+		print $as_fd <<EOF;
 	.text
 	.extern	$imp_sym
 	.global	$glob_sym
 $glob_sym:
 	jmp	*$imp_sym
 EOF
+	    }
+	    close $as_fd or exit 1;
+	    exit 0 if $forking;
 	}
-	close $as_fd or exit 1;
     }
 }
 
@@ -86,8 +94,18 @@ for my $f (keys %text) {
     if (!$text{$f}) {
 	unlink $f;
     } else {
-	system $objcopy, '-R', '.text', $f and exit 1;
-	system $objcopy, '-R', '.bss', '-R', '.data', "t-$f" and exit 1;
+	if ($forking && fork) {
+	    # Testing shows parent does need to sleep a short time here,
+	    # otherwise system is inundated with hundreds of objcopy processes
+	    # and the forked perl processes that launched them.
+	    my $delay = 0.01; # NOTE: Slower systems may need to raise this
+	    select(undef, undef, undef, $delay); # Supports fractional seconds
+	} else {
+	    # Do two objcopy calls at once to avoid one system() call overhead
+	    system '(', $objcopy, '-R', '.text', $f, ')', '||',
+		$objcopy, '-R', '.bss', '-R', '.data', "t-$f" and exit 1;
+	    exit 0 if $forking;
+	}
     }
 }
 
-- 
2.29.2



More information about the Cygwin-patches mailing list