1Class::Std::Fast(3) User Contributed Perl Documentation Class::Std::Fast(3)
2
3
4
6 Class::Std::Fast - faster but less secure than Class::Std
7
9 This document describes Class::Std::Fast 0.0.8
10
12 package MyClass;
13
14 use Class::Std::Fast;
15
16 1;
17
18 package main;
19
20 MyClass->new();
21
23 Class::Std::Fast allows you to use the beautiful API of Class::Std in a
24 faster way than Class::Std does.
25
26 You can get the object's ident via scalarifiyng your object.
27
28 Getting the objects ident is still possible via the ident method, but
29 it's faster to scalarify your object.
30
32 new
33 The constructor acts like Class::Std's constructor. For extended
34 constructors see Constructors below.
35
36 package FastObject;
37 use Class::Std::Fast;
38
39 1;
40 my $fast_obj = FastObject->new();
41
42 ident
43 If you use Class::Std::Fast you shouldn't use this method. It's only
44 existant for downward compatibility.
45
46 # insted of
47 my $ident = ident $self;
48
49 # use
50 my $ident = ${$self};
51
52 initialize
53 Class::Std::Fast::initialize();
54
55 Imported from Class::Std. Please look at the documentation from
56 Class::Std for more details.
57
58 Methods for accessing Class::Std::Fast's internals
59 Class::Std::Fast exposes some of it's internals to allow the
60 construction of Class::Std::Fast based objects from outside the auto-
61 generated constructors.
62
63 You should never use these methods for doing anything else. In fact you
64 should not use these methods at all, unless you know what you're doing.
65
66 ID
67 Returns an ID for the next object to construct.
68
69 If you ever need to override the constructor created by
70 Class::Std::Fast, be sure to use Class::Std::Fast::ID as the source for
71 the ID to assign to your blessed scalar.
72
73 More precisely, you should construct your object like this:
74
75 my $self = bless \do { my $foo = Class::Std::Fast::ID } , $class;
76
77 Every other method of constructing Class::Std::Fast - based objects
78 will lead to data corruption (duplicate object IDs).
79
80 ID_GENERATOR_REF
81 Returns a reference to the ID counter scalar.
82
83 The current value is the next object ID !
84
85 You should never use this method unless you're trying to create
86 Class::Std::Fast objects from outside Class::Std::Fast (and possibly
87 outside perl).
88
89 In case you do (like when creating perl objects in XS code), be sure to
90 post-increment the ID counter after creating an object, which you may
91 do from C with
92
93 sv_inc( SvRV(id_counter_ref) )
94
95 OBJECT_CACHE_REF
96 Returns a reference to the object cache.
97
98 You should never use this method unless your're trying to (re-)create
99 Class::Std::Fast objects from outside Class::Std::Fast (and possibly
100 outside perl).
101
102 See <L/EXTENSIONS TO Class::Std> for a description of the object cache
103 facility.
104
106 Methods
107 real_can
108
109 Class::Std::Fast saves away UNIVERSAL::can as
110 Class::Std::Fast::real_can before overwriting it. You should not use
111 real_can, because it does not check for subroutines implemented via
112 AUTOMETHOD.
113
114 It is there if you need the old can() for speed reasons, and know what
115 you're doing.
116
117 Constructors
118 Class::Std::Fast allows the user to chose between several constructor
119 options.
120
121 · Standard constructor
122
123 No special synopsis. Acts like Class::Std's constructor
124
125 · Basic constructor
126
127 use Class::Std::Fast qw(2);
128 use Class::Std::Fast constructor => 'basic';
129
130 Does not call BUILD and START (and does not walk down the
131 inheritance hierarchy calling BUILD and START).
132
133 Does not perform any attribute initializations.
134
135 Really fast, but very basic.
136
137 · No constructor
138
139 use Class::Std::Fast qw(3);
140 use Class::Std::Fast constructor => 'none';
141
142 No constructor is exported into the calling class.
143
144 The recommended usage is:
145
146 use Class::Std::Fast constructor => none;
147 sub new {
148 my $self = bless \do { my $foo = Class::Std::Fast::ID } , $_[0];
149 # do what you need to do after that
150 }
151
152 If you use the Object Cache (see below) the recommended usage is:
153
154 use Class::Std::Fast constructor => 'none', cache => 1;
155 sub new {
156 my $self = pop @{ Class::Std::Fast::OBJECT_CACHE_REF()->{ $_[0] } }
157 || bless \do { my $foo = Class::Std::Fast::ID() } , $_[0];
158 }
159
160 Destructors
161 Class::Std sorts the @ISA hierarchy before traversing it to avoid
162 cleaning up the wrong class first. However, this is unneccessary if the
163 class in question has a linear inheritance tree.
164
165 Class authors may disable sorting by calling
166
167 use Class::Std::Fast unsorted => 1;
168
169 Use only if you know your class' complete inheritance tree...
170
171 Object Cache
172 Synopsis
173
174 use Class::Std::Fast cache => 1;
175
176 Description
177
178 While inside out objects are basically an implementation of the
179 Flyweight Pattern (object data is stored outside the object), there's
180 still one aspect missing: object reuse. While Class::Std::Fast does not
181 provide flyweights in the classical sense (one object re-used again and
182 again), it provides something close to it: An object cache for re-using
183 destroyed objects.
184
185 The object cache is implemented as a simple hash with the class names
186 of the cached objects as keys, and a list ref of cached objects as
187 values.
188
189 The object cache is filled by the DESTROY method exported into all
190 Class::Std::Fast based objects: Instead of actually destroying the
191 blessed scalar reference (Class::Std::Fast based objects are nothing
192 more), the object to be destroyed is pushed into it's class' object
193 cache.
194
195 new() in turn does not need to create a new blessed scalar, but can
196 just pop one off the object cache (which is a magnitude faster).
197
198 Using the object cache is recommended for persistent applications (like
199 running under mod_perl), or applications creating and destroying lots
200 of Class::Std::Fast based objects again and again.
201
202 The exported constructor automatically uses the Object Cache when
203 caching is enabled by setting the cache import flag to a true value.
204
205 For an example of a user-defined constructor see "Constructors" above.
206
207 Memory overhead
208
209 The object cache trades speed for memory. This is a very perlish way
210 for adressing performance issues, but may cause your application to
211 blow up if you're short of memory.
212
213 On a 32bit Linux, Devel::Size reports 44 bytes for a Class::Std::Fast
214 based object - so a cache containing 1 000 000 (one million) of objects
215 needs around 50MB of memory (Devel Size only reports the memory use it
216 can see - the actual usage is system dependent and something between 4
217 and 32 bytes more).
218
219 If you are anxious about falling short of memory, only enable caching
220 for those classes whose objects you know to be frequently created and
221 destroyed, and leave it turned off for the less frequently used classes
222 - this gives you both speed benefits, and avoids holding a cache of
223 object that will never be needed again.
224
226 see Class::Std.
227
228 Additional diagnostics are:
229
230 · Class::Std::Fast loaded too late - put >use Class::Std::Fast<
231 somewhere at the top of your application (warning)
232
233 Class::Std has been "use"d before Class::Std::Fast. While both
234 classes happily coexist in one application, Class::Std::Fast must
235 be loaded first for maximum speedup.
236
237 This is due to both classes overwriting UNIVERSAL::can.
238 Class::Std::Fast uses the original (fast) can where appropritate,
239 but cannot access it if Class::Std has overwritten it before with
240 it's (slow) replacement.
241
244 · version
245
246 · Class::Std
247
248 · Carp
249
251 see Class::Std
252
254 · You can't use the :SCALARIFY attribute for your Objects.
255
256 We use an increment for building identifiers and not
257 Scalar::Util::refaddr like Class::Std.
258
259 · Inheriting from non-Class::Std::Fast modules does not work
260
261 You cannot inherit from non-Class::Std::Fast classes, not even if
262 you overwrite the default constructor. To be more precise, you
263 cannot inherit from classes which use something different from
264 numeric blessed scalar references as their objects. Even so
265 inheriting from similarly contructed classes like Object::InsideOut
266 could work, you would have to make sure that object IDs cannot be
267 duplicated. It is therefore strongly discouraged to build classes
268 with Class::Std::Fast derived from non-Class::Std::Fast classes.
269
270 If you really need to inherit from non-Class::Std::Fast modules,
271 make sure you use Class::Std::Fast::ID as described above for
272 creating objects.
273
274 · No runtime initialization with constructor => 'basic' / 'none'
275
276 When eval'ing Class::Std::Fast based classes using the basic
277 constructor, make sure the last line is
278
279 Class::Std::Fast::initialize();
280
281 In contrast to Class::Std, Class::Std::Fast performs no run-time
282 initialization when the basic constructor is enabled, so your code
283 has to do it itself.
284
285 The same holds true for constructor => 'none', of course.
286
287 CUMULATIVE, PRIVATE, RESTRICTED and anticumulative methods won't
288 work if you leave out this line.
289
291 Last changed by
292 $Author: ac0v $
293
294 Id $Id: Fast.pm 469 2008-05-26 11:26:35Z ac0v $
295
296 Revision
297 $Revision: 469 $
298
299 Date
300 $Date: 2008-05-26 13:26:35 +0200 (Mon, 26 May 2008) $
301
302 HeadURL
303 $HeadURL:
304 file:///var/svn/repos/Hyper/Class-Std-Fast/branches/0.0.8/lib/Class/Std/Fast.pm
305 $
306
308 Andreas 'ac0v' Specht "<ACID@cpan.org>"
309
310 Martin Kutter "<martin.kutter@fen-net.de>"
311
313 Copyright (c) 2007, Andreas Specht "<ACID@cpan.org>". All rights
314 reserved.
315
316 This module is free software; you can redistribute it and/or modify it
317 under the same terms as Perl itself.
318
319
320
321perl v5.32.0 2020-07-28 Class::Std::Fast(3)