Uhm. Mommy?

Today’s crappy code review.

The original.

static void _originalFn(arg1, arg2)
{
  if ( arg1.something && (arg2.something || arg2.somethingElse) )
  {
    ... lots of stuff to do ...
  }
}

This code works fine. But I need to beef up the originalFn to handle some additional cases. So, using the skillful art of “copy & paste” I re-organize the code thusly. I’m simplifying down because microscopic inspection confirms that I had not changed anything except the placement of the code.

The first attempt:

static void _newFn1(arg1, arg2)
{
  ... lots of stuff to do ...  lifted directly from originalFn 
}

static void _newFn2(arg1, arg2)
{
   empty for now 
}

static void _originalFn(arg1, arg2)
{
  if ( arg1.something && (arg2.something || arg2.somethingElse) )
    _newFn1(arg1, arg2) ;
  else if ( arg1.somethingElse )
    _newFn2(arg1, arg2) ;
}

Again I scrutinized this very carefully, used a diff checker, and nothing changed except code placement and the creation of the two new functions.

Result? Now the application crashes several calls down from within newFn1.

So I re-lift the code from _newFn1 back into originalFn.

static void _newFn1(arg1, arg2)
{
  ... lots of stuff to do ...  lifted directly from originalFn 
}

static void _newFn2(arg1, arg2)
{
   empty for now 
}

static void _originalFn(arg1, arg2)
{
  if ( arg1.something && (arg2.something || arg2.somethingElse) )
  {
    ... lots of stuff to do ...  lifted directly from originalFn 
  }
  else if ( arg1.somethingElse )
    _newFn2(arg1, arg2) ;
}

And now the application stops crashing again.

My conclusion: The stack is being trashed just far enough away that “originalFn” gets away unscathed. But add in the extra overhead of a function call to the stuff-to-do and now we’re being trampled.

I want my mommy… Speaking of which, today’s OED word of the day is “Grandmother”.

grandmother, n.
SECOND EDITION 1989

({sm}grændm{revv}{edh}{schwa}(r), {sm}grænm{revv}{edh}{schwa}(r)) Also 5 graunt(e)moder, 6 graundemoder, -mother, 7 gran-mother. [See GRAND a. 12b.]

1. a. The mother of one’s father or mother.
1424 [Implied in GRANDMOTHERLESS]. 1483 CAXTON Gold. Leg. 429/1 The graunte moder of Saynt aldebaulte. 1496 Plumpton Corr. (Camden) p. c, Sir Robert Babthorp, kt. or Dame Elizabeth his wife, grauntfeder & grauntmoder to the said Elizabeth. 1535 COVERDALE 2 Tim. i. 5 The vnfayned faith..which dwelt first in thy graundemother Lois, and in thy mother Eunica. c1645 HOWELL Lett. I. II. VI. ii. 182, I made another Latin Speech to the Duke, touching his Gran-Mothers death. 1671 LADY M. BERTIE in 12th Rep. Hist. MSS. Comm. App. v. 23 Her grandmother sent a chaire for her. 1788 H. WALPOLE Remin. ii. 18 It was the portrait of her grandmother. 1860-1 F. NIGHTINGALE Nursing 26 A great-grandmother, who was a tower of physical strength, descending into a grandmother, perhaps a little less strong.

b. Phrases. this beats my grandmother: said of something that excites astonishment. teach your grandmother to suck eggs (see EGG n. 4b). your grandmother!: said of something with which one disagrees. (Cf. GRANNY 1c.)
1870 ‘MARK TWAIN’ Screamers (1871) 50 ‘Shake the tree{em}’ ‘Shake your grandmother! Turnips don’t grow on trees!’ 1874 TROLLOPE Phineas Redux II. xiii. 111 ‘Did you see her?’ said Ned… ‘See your grandmother.’ 1883 Harper’s Mag. 889/2 Well, this does beat my grandmother, I must say! 1909 GALSWORTHY Joy 1, Mrs. Hope. You’ll just attend to what I say and look into that mine! Colonel. Look into your grandmother! 1911 G. B. SHAW Getting Married 268 Lesbia. I hate..sentimental people. Mrs. George. Oh, sentimental your grandmother! 1934 E. WAUGH Handful of Dust ii. 22 ‘I think she [sc. a horse] put in a short step.’ ‘Short step my grandmother.’

c. fig.
1626 Will of Carew (Somerset Ho.), My body to my grand~mother the Earth. 1650 B. Discolliminium 15 Ignorance is the Grand-mother of mistaken Necessity. 1774 J. ADAMS in Fam. Lett. (1876) 46, I strolled away to mother church, or rather to grandmother church. I mean the Romish chapel. 1870 E. A. FREEMAN in W. R. W. Stephens Life & Lett. (1895) II. 9 Atholl..built himself the grandmother of pews. 1879 B. TAYLOR Stud. Germ. Lit. 5 If the Gothic language be the legitimate mother of the Old German, it must also be, through the Saxon, the grandmother of English. 1959 Woman 24 Oct. 12/1 My mother and Laura Simmonds, who had lived in each other’s pockets since the age of five, had the very grandmother of a row.

d. Grandmother’s (Foot)steps, name of a children’s game in which one player stands with his back turned to the rest and the others try to approach him in a stealthy manner and touch his back without his seeing them move. The person in front is allowed to turn round often and without warning and any player caught moving is sent back to the starting-line.
1937 ‘N. BLAKE’ There’s Trouble Brewing i. 30 The children’s game called ‘Grandmother’s Steps’. 1945 E. WAUGH Brideshead Revisited 122 She took hold of her subject in a feminine, flirtatious way… She played ‘grandmother’s steps’ with it, getting nearer the real point imperceptibly while one’s back was turned, standing rooted when she was observed. 1956 L. MCINTOSH Oxford Folly 204 We were playing grandmother’s footsteps. 1966 J. BETJEMAN High & Low 37 A game of Grandmother’s Steps on the vicarage grass.

2. A female ancestor.
1526 Pilgr. Perf. (W. de W. 1531) 76 The fourth thynge that is dispraysed in our graundmother Eue, was that she was curyous. 1588 SHAKES. L.L.L. I. i. 266 With a childe of our Grandmother Eue, a female. 1606 HIERON Truth’s Purchase Wks. (1613) I. 61 Our grand-mother Heuah. 1642 FULLER Holy & Prof. St. V. iii. 366 Satan tempted our grandmother Eve.

3. attrib. (quasi-adj.)
a1649 DRUMMOND OF HAWTHORNDEN Cypress Grove Wks. (1711) 119 What excellency is there in it, for which he should..repine to be at rest, and return to his old grandmother dust? 1649 E. SPARKE in J. Shute Sarah & Hagar Pref. A3a, Our grave Author..was..master of those three Grandmother-Languages inscribed on the Cross of Christ, besides some others of their progeny. 1814 Prophetess III. iii, I held him here with these grandmother hands.

4. grandmother clock, a clock resembling a grandfather clock, but with a smaller case. Also ellipt.
1922 H. S. BARRETT A.B.C. Hist. Antique Eng. Furnit. 86 By Grandmother clocks I refer to clocks not exceeding about 5 ft. to 6 ft. in height. 1927 Daily Tel. 6 Dec. 3/7 A grandmother clock with brass dial. 1930 Aberdeen Press & Jrnl. 31 May 8/3 The gift to ex-Constable Jamie was a beautiful grandmother clock. 1931 H. SUTCLIFFE Persons Unknown i. 11 The quieter pendulum-swing of the slim clocks known as ‘grandmothers’. 1965 E. TUNIS Colonial Craftsmen vi. 145 In addition to the taller clocks, there were also ‘grandmother’ clocks about five feet high, controlled by shorter, half-second pendulums. 1967 B. PALMER Treasury Amer. Clocks 23 Grandmother clocks are diminutive editions of grandfather clocks, standing four feet or less in height.

Hence {sm}grandmotherhood, the condition or fact of being a grandmother; {sm}grandmotherism, the relation of being a grandmother; {sm}grandmotherless a., without a grandmother.
1424 E.E. Wills (1882) 57 {Th}an shall he be left..graunt~moderles. 1806 A. SEWARD Lett. (1811) VI. 324 The apparent grandmotherism seems now reversed between us. 1846 DE QUINCEY Sophocles’ Antigone Wks. 1860 XIV. 201 Surely..she will command that reverence from you, by means of her grandmotherhood, which by means of her ethics she might not.

5 Comments

For kicks and giggles…

Keep your new structure. Trim down “… lots of stuff to do … lifted directly from originalFn” to as little as you can get away with. The idea is to offset the overhead generated by the additional function call. If it does crash, well not much is proven and you might be worse off than you think, but if it doesn’t, you might sleep better knowing that your conclusion is dead on.

Why would you add extra functions if they won’t ever be called by anything other than the original function? Of course it allowed you to find some memory trampling which is a good thing because that also usually means something in that original function is only accidentally working and not REALLY working. :p

Compartmentalisation and re-usability. Most of the code in the function was replicated in three other functions, all three of which differ only on one line (ideal for a function call with an argument, n’est pas?)

The function wasn’t inline because my current build configuration (heavy debug) disallows all but __inline’s to help with debugging.

I’ve since tried it with inlines enabled, and the crash also goes away, so it does look like a stack-trash =(

So, in the gaming world do you do much threading?

Odd stuff like this is sometimes timing issues where the smallest of changes (add an extra function call) allows one thread to get to important data and step on it (change it to something other threads don’t want it to be yet) before it could previously, due to changes in which thread get’s to use which clock cycles. Mishandled (missing) critical sections will cause that a lot.

We use threading, but not in this particular process, and it wouldn’t have been applicable here.

Incidentally I cut my early programming teeth on multi-tasking and threading. My first MUD system, nearly 20 years ago on the Atari ST, was written in assembler and, since the ST didn’t multi-task, I wrote my own replacement O/S complete with threading :) After that I wrote inter-process communication tools for the Mirage 68k based operating system for kicks and giggles :)

Leave a Reply

Name and email address are required. Your email address will not be published.

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <pre> <q cite=""> <s> <strike> <strong> 

%d bloggers like this: