develooper Front page | perl.perl5.porters | Postings from February 2015

Potential Optimisation in pp_reverse (but only for trueSvMAGICAL(av) )

Thread Next
From:
Shlomi Fish
Date:
February 6, 2015 19:58
Subject:
Potential Optimisation in pp_reverse (but only for trueSvMAGICAL(av) )
Message ID:
20150206215759.3fc8b6b5@telaviv1.shlomifish.org
Hi all,

reviewing the code for pp_reverse in pp.c (thanks to someone who asked about it
on beginners@perl.org earlier today) got me looking at this code:

«
	    if (SvMAGICAL(av)) {
		SSize_t i, j;
		SV *tmp = sv_newmortal();
		/* For SvCANEXISTDELETE */
		HV *stash;
		const MAGIC *mg;
		bool can_preserve = SvCANEXISTDELETE(av);

		for (i = 0, j = av_tindex(av); i < j; ++i, --j) {
		    SV *begin, *end;

		    if (can_preserve) {
			if (!av_exists(av, i)) {
			    if (av_exists(av, j)) {
				SV *sv = av_delete(av, j, 0);
				begin = *av_fetch(av, i, TRUE);
				sv_setsv_mg(begin, sv);
			    }
			    continue;
			}
			else if (!av_exists(av, j)) {
			    SV *sv = av_delete(av, i, 0);
			    end = *av_fetch(av, j, TRUE);
			    sv_setsv_mg(end, sv);
			    continue;
			}
		    }

		    begin = *av_fetch(av, i, TRUE);
		    end   = *av_fetch(av, j, TRUE);
		    sv_setsv(tmp,      begin);
		    sv_setsv_mg(begin, end);
		    sv_setsv_mg(end,   tmp);
		}
	    }
»

Now for my notes and questions:

1. can_preserve may be made const (assuming sufficient compiler portability).

2. «if (can preserve)» is being calculated at each loop iteration and always
yields the same verdict. As a result, we can extract the condition outside and
write something like:

«
if (can_preserve) {
	/* for loop with the conditional body */
}
else {
	/* for loop without it. */
}
»

(Possibly with some macros to avoid duplicate code).

3. The SV *s "begin" and "end", probably should not be predeclared at the
beginning of the loop because those used inside the «if (can_preserve)»
conditional are always followed by a "continue;" statement in the C code.

4. Since this only happens if SvMAGICAL is set - is all this trouble worth it?
Do we care about speeding up code with SvMAGICAL set?

----------------

Please let me know what you think.

Regards,

	Shlomi Fish

-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
My Favourite FOSS - http://www.shlomifish.org/open-source/favourite/

Chuck Norris once counted all the real numbers on his fingers. (by: ZadYree)
    — http://www.shlomifish.org/humour/bits/facts/Chuck-Norris/

Please reply to list if it's a mailing list post - http://shlom.in/reply .

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About