Closed Bug 99421 Opened 24 years ago Closed 23 years ago

[FIX] if cascade cleanup

Categories

(Core :: Layout, defect, P3)

defect

Tracking

()

RESOLVED FIXED
mozilla0.9.7

People

(Reporter: drepper, Assigned: attinasi)

Details

(Keywords: memory-footprint)

Attachments

(1 file, 1 obsolete file)

layout/html/style/src/nsCSSFrameConstructor.cpp contains a if cascade which can ge cleaned up significantly with the result of fewer generated code (124 bytes worth in my case).
Attached patch cleanup patch (obsolete) — Splinter Review
Keywords: patch, review
Personally, I like the part here: + if (val.EqualsIgnoreCase("submit") || + val.EqualsIgnoreCase("reset") || + val.EqualsIgnoreCase("button") || + val.EqualsIgnoreCase("hidden")) { + return ConstructButtonControlFrame(aPresShell, aPresContext, aFrame); but I don't like the scads of early returns. Why is returning at each case better than setting the return value and then returning at the bottom of the method? To skip one else statement?
> but I don't like the scads of early returns. Why is returning at each case > better than setting the return value and then returning at the bottom of the > method? To skip one else statement? It allows to fold more cases. Otherwise you'd have to operate with some local variable to keep track of what has been done. I don't like this. Early return statements are no problem. The function has a very specific purpose and this is to call one other function. Writing the code this way also makes it easier (and likelier) for the compiler to recognize tail calls and optimize appropriately. With a local variable this is not likely to happen.
I think you only save one else statement with the early returns: nsresult nsCSSFrameConstructor::CreateInputFrame(nsIPresShell *aPresShell, nsIPresContext *aPresContext, nsIContent *aContent, nsIFrame *&aFrame, nsIStyleContext *aStyleContext) { nsresult rv; // Figure out which type of input frame to create nsAutoString val; if (NS_OK == aContent->GetAttr(kNameSpaceID_HTML, nsHTMLAtoms::type, val)) { if (val.EqualsIgnoreCase("submit") || val.EqualsIgnoreCase("reset") || val.EqualsIgnoreCase("button") || val.EqualsIgnoreCase("hidden")) { rv = ConstructButtonControlFrame(aPresShell, aPresContext, aFrame); } else if (val.EqualsIgnoreCase("checkbox")) { rv = ConstructCheckboxControlFrame(aPresShell, aPresContext, aFrame, aContent, aStyleContext); } else if (val.EqualsIgnoreCase("file")) { rv = NS_NewFileControlFrame(aPresShell, &aFrame); } else if (val.EqualsIgnoreCase("image")) { rv = NS_NewImageControlFrame(aPresShell, &aFrame); } else if (val.EqualsIgnoreCase("radio")) { rv = ConstructRadioControlFrame(aPresShell, aPresContext, aFrame, aContent, aStyleContext); } else { // here is the extra else rv = ConstructTextControlFrame(aPresShell, aPresContext, aFrame, aContent); } return rv; } rv = ConstructTextControlFrame(aPresShell, aPresContext, aFrame, aContent); return rv; } no local variables or anything like that needed. ... But I don't know what a tail call is, nor do I know how likely or important it is for the compiler to optimize it. Anyway, there are not that many early returns, and the method is still pretty manageable, so I'll take it your way, with pleasure! [s]r=attinasi
Target Milestone: --- → mozilla1.0
Why for 1.0? It already is reviewed? Somebody just has to apply it. Actually, I'll update it first ("hidden" is handled differently today) but then somebody just has to do it. I don't have write access which is why it hasn't happened yet. Maybe if I add footprint to the keywords...
Keywords: footprint
Attached patch Updated patchSplinter Review
Updated patch which handles "hidden" buttons like the current code.
Attachment #49172 - Attachment is obsolete: true
Target Milestone: mozilla1.0 → mozilla1.2
Come on, this patch is obviously correct and is reviewed and it helps to reduce the footprint. And nevertheless you push it out to 1.2? This is a good way to discourage anybody from contributing. The only reason I haven't checked in the patch is that I have no write access. Somebody please simply check it in.
Ulrich, Kevin made a mistake pusing this out, and I apologize also for having forgotten about it. Yes, we should check it in, but we still need a review for it (I have super-reviewed it but we need review too). I'll ping Rods for the r= and then check it in. Thanks again, and sorry for the mistake.
Status: NEW → ASSIGNED
OS: Linux → All
Priority: -- → P3
Hardware: PC → All
Target Milestone: mozilla1.2 → mozilla0.9.7
r=rods
I'll check this in today
Summary: if cascade cleanup → [FIX] if cascade cleanup
Checked into trunk. Thansk Ulrich!
Status: ASSIGNED → RESOLVED
Closed: 23 years ago
Resolution: --- → FIXED
Marc, a tail call is when the compiler turns "return foo();" into a direct jump to the start of the foo() function. When foo() returns it effectively returns from the caller function. It saves cycles and stack space.
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: