Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize reverse RANGE #22825

Open
grr opened this issue Dec 5, 2024 · 1 comment
Open

Optimize reverse RANGE #22825

grr opened this issue Dec 5, 2024 · 1 comment

Comments

@grr
Copy link

grr commented Dec 5, 2024

Description

Optimize reverse RANGE so that it doesn't produce a temporary array and "burn" memory.

I came upon an ancient perlmonks thread that suggested this would be "an easy candidate for optimization", but was surprised to find nobody had actually requested it.

Steps to Reproduce

for (reverse 0..1e7) { }

Expected behavior

No use of a temporary array and hence no memory inflation.

Perl configuration

This is perl 5, version 40, subversion 0 (v5.40.0) built for darwin-2level

@richardleach
Copy link
Contributor

perl -MO=Concise -E 'for (0..1e7) { }' gives:

d  <@> leave[1 ref] vKP/REFC ->(end)
1     <0> enter v ->2
2     <;> nextstate(main 2 -e:1) v:%,us,{,fea=15 ->3
c     <2> leaveloop vK/2 ->d
7        <{> enteriter(next->9 last->c redo->8) vKS/DEF ->a
-           <0> ex-pushmark s ->3
-           <1> ex-list lK ->6
3              <0> pushmark s ->4
4              <$> const[IV 0] s ->5
5              <$> const[NV 10000000] s ->6
6           <#> gv[*_] s ->7
-        <1> null vK/1 ->c
b           <|> and(other->8) vK/1 ->c
a              <0> iter s ->b
-              <@> lineseq vK ->-
8                 <0> stub v ->9
9                 <0> unstack v ->a

perl -MO=Concise -E 'for (reverse 0..1e7) { }' gives:

d  <@> leave[1 ref] vKP/REFC ->(end)
1     <0> enter v ->2
2     <;> nextstate(main 2 -e:1) v:%,us,{,fea=15 ->3
c     <2> leaveloop vK/2 ->d
7        <{> enteriter(next->9 last->c redo->8) vK/DEF,REVERSED ->a
-           <0> ex-pushmark s ->3
-           <1> ex-list lKM ->6
3              <0> pushmark s ->4
-              <1> ex-reverse lKM/1 ->-
-                 <0> ex-pushmark s ->4
5                 <1> rv2av lKP/1 ->6
4                    <$> const[AV ARRAY] s ->5
6           <#> gv[*_] s ->7
-        <1> null vK/1 ->c
b           <|> and(other->8) vK/1 ->c
a              <0> iter s/REVERSED ->b
-              <@> lineseq vK ->-
8                 <0> stub v ->9
9                 <0> unstack v ->a

OP_REVERSE is not involved.

Addressing this request might involve:

  • Changing the optree construction to again give 2x OP_CONST, instead of 1x OP_CONST + 1x OP_RV2AV
  • Changing how OP_ENTERITER sets up the context
  • Adding a CXt_LOOP_LAZYIV_REV case to OP_ITER

I've no idea exactly how easy/feasible this is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants