develooper Front page | perl.perl5.porters | Postings from July 2012

a cautionary tale of C (micro)optimisation

Thread Next
Nicholas Clark
July 11, 2012 03:32
a cautionary tale of C (micro)optimisation
Message ID:
Part of this commit:

commit 3edf23ff129b6c5edde184cb3b63953432223591
Author: Andy Lester <>
Date:   Thu Jan 5 19:44:48 2006 -0600

    performance tweaking op.c
    Message-ID: <>
    p4raw-id: //depot/perl@26674

was this:

@@ -507,19 +505,25 @@ Perl_op_refcnt_unlock(pTHX)
 OP *
 Perl_linklist(pTHX_ OP *o)
+    OP *first;
     if (o->op_next)
 	return o->op_next;
     /* establish postfix order */
-    if (cUNOPo->op_first) {
+    first = cUNOPo->op_first;
+    if (first) {
         register OP *kid;
-	o->op_next = LINKLIST(cUNOPo->op_first);
-	for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
-	    if (kid->op_sibling)
+	o->op_next = LINKLIST(first);
+	kid = first;
+	for (;;) {
+	    if (kid->op_sibling) {
 		kid->op_next = LINKLIST(kid->op_sibling);
-	    else
+		kid = kid->op_sibling;
+	    } else {
 		kid->op_next = o;
+		break;
+	    }

It turns out not to be what it seems. Not only does it add a local variable
'first' that effectively duplicates 'kid' (now refactored on a branch that
I'm working on), the intended "optimisation" actually makes the code larger
(at least with gcc -Os on both x86_64 and ARM), and uses more load and store

[To be fair, I've not chased the code flow to work out whether there are
more instructions called. That feels like even more work than this deserves.
Given, for example, that I needed to run nmap to find the IP address of the
Raspberry Pi so that I could log into it to build on ARM :-)]

"Change is inevitable, progress is not"

Not all changes are progress. Maximise progress as a function of change.
(And no, it's usually not obvious what really is progress.)

Nicholas Clark

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About