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

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

Thread Next
Shlomi Fish
February 6, 2015 19:58
Potential Optimisation in pp_reverse (but only for trueSvMAGICAL(av) )
Message ID:
Hi all,

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

	    if (SvMAGICAL(av)) {
		SSize_t i, j;
		SV *tmp = sv_newmortal();
		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);
			else if (!av_exists(av, j)) {
			    SV *sv = av_delete(av, i, 0);
			    end = *av_fetch(av, j, TRUE);
			    sv_setsv_mg(end, sv);

		    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.


	Shlomi Fish

Shlomi Fish
My Favourite FOSS -

Chuck Norris once counted all the real numbers on his fingers. (by: ZadYree)

Please reply to list if it's a mailing list post - .

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