File: Attribute-MemoizeToConstant.pm

package info (click to toggle)
libconstant-defer-perl 6-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 296 kB
  • ctags: 75
  • sloc: perl: 1,603; sh: 128; makefile: 2
file content (180 lines) | stat: -rw-r--r-- 4,080 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# no good because Attribute::Handlers doesn't run at the right phase in a
# require or eval



# Copyright 2008, 2009, 2010 Kevin Ryde

# This file is part of constant-defer.
#
# constant-defer is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 3, or (at your option) any later
# version.
#
# constant-defer is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with constant-defer.  If not, see <http://www.gnu.org/licenses/>.

package Attribute::MemoizeToConstant;
use Attribute::Handlers;
## no critic (RequireUseStrict RequireUseWarnings)
no strict;
no warnings;

push @UNIVERSAL::ISA, __PACKAGE__;

use constant DEBUG => 0;

our $c;

my @pending;
my $checked = 0;

sub MODIFY_CODE_ATTRIBUTES {
  my ($package, $coderef, @attrs) = @_;
  if (DEBUG) { print "MemoizeToConstant pending $package $coderef\n"; }
  push @pending, [$package, $coderef];
  if ($checked) {
    run_pending();
  }
  return grep {$_ ne 'MemoizeToConstant'} @attrs;
}

CHECK {
  if (DEBUG) { print "MemoizeToConstant CHECK\n"; }
  $checked = 1;
  run_pending();
}

sub run_pending {
  while (@pending) {
    my ($package, $oldcode) = @{pop @pending};
    if (DEBUG) { print " $package $oldcode\n"; }

    $c = $oldcode;
    require Scalar::Util;
    Scalar::Util::weaken ($c);

    my $found;
    my $phash = \%{"${package}::"};
    foreach my $name (keys %$phash) {
      my $glob = $phash->{$name};
      ref(\$glob) eq 'GLOB' or next;
      (*{$glob}{CODE} || 0) == $oldcode or next;

      my $fullname = "${package}::$name";
      if (DEBUG) { print "  install to $fullname\n"; }

      *$fullname = sub () {
        my $value = $oldcode->(@_);
        *$fullname = sub () { $value };
        return $value;
      };
      $found = 1;
    }
    $found or warn "MemoizeToConstant func $oldcode not found in $package";
  }
}

1;

__END__


  
# sub make_func {
#   return sub () {
#     my $value = $_[0]oldcode->(@_);
#     *$sym = sub () { $value };
#     return $value;
#   }
# }
  
#   use Data::Dumper;
#   print Dumper(\@_);
# 
#   use Data::Dump;
#   print Data::Dump::dump(\*main::foo);
# 
#   my $x = findsym ($package, $coderef);
#   print "findsym ",Dumper($x);
# 
#   no strict;
# 
#   my $type = ref($coderef);



# Attribute::Handlers holds onto to the original coderef, so it's not freed
# when the func is turned into a constant ...

package Attribute::MemoizeToConstant;
use Attribute::Handlers;
use strict;
use warnings;

our $VERSION = 1;

use constant DEBUG => 0;

our $c;

sub UNIVERSAL::MemoizeToConstant : ATTR(CODE) {
  my ($package, $typeglob, $oldcode) = @_;
$c = $oldcode;
Scalar::Util::weaken ($c);

  if (DEBUG) {
    print "MemoizeToConstant on '$package' '",
      *{$typeglob}{NAME}, "' ", $oldcode,"\n";
  }

  no warnings;
  *$typeglob = sub () {
    my $value = $oldcode->(@_);
    *$typeglob = sub () { $value };
    return $value;
  };
}

1;
__END__

=head1 NAME

Attribute::MemoizeToConstant -- memoize functions to become constants

=head1 SYNOPSIS

 use Attribute::MemoizeToConstant;
 sub myfunc : MemoizeToConstant {
   # some long calculation
   return $x;
 }

=head1 BUGS!

Doesn't work when used in a module or file which is loaded by C<require>,
C<do>, etc, only from the main program and things brought in C<use>.

=head1 DESCRIPTION

Attribute C<MemoizeToConstant> arranges for a function to be memoized so its
first call runs the code but it's then transformed into a constant sub (like
C<use constant>) with that first return value.

=head1 SEE ALSO

=for comment
Actually it wants to be attributes(3perl) to avoid attributes(3ncurses), but
the formatters complain about that ...

L<Attribute::Memoize>, L<Memoize::Attrs>, L<attributes>,
L<Attribute::Handlers>, L<Memoize>

=cut