[Dbix-class] Using Class::Std under-the-hood
Adrian Howard
adrianh at quietstars.com
Fri Aug 12 15:42:51 CEST 2005
On 11 Aug 2005, at 08:05, Dan Kubb wrote:
>> What are the performance implications of using Class::Std?
>
> From Class::Std docs:
>
> "Of the several popular methods of reliably enforcing
> encapsulation in Perl,
> inside-out objects are also by far the cheapest. The run-time
> performance of
> inside-out classes is effectively identical to that of regular
> hash-based
> classes. In particular, in both schemes, every attribute access
> requires only
> a single hash look-up. The only appreciable difference in speed
> occurs when
> an inside-out object is destroyed."
>
> And later Damian says:
>
> "There is a simple, convenient, and utterly secure way to prevent
> client code
> from accessing the internals of the objects you provide. Happily,
> that approach
> also guards against misspelling attribute names (a common error
> in hash-based
> classes), as well as being just as fast as--and often more memory-
> efficient
> than--ordinary hash-based objects."
>
> As fast as -- and *more* memory efficient. ;)
</lurk>
Well - yes and no.
Since 5.004 perl is bright enough to only store hash keys once no
matter how many hashes use them - so the overhead of having duplicate
hash keys in each object disappears. Depending on what you use as the
hash index inside-out objects can be a little bit smaller or a little
bit larger than normal blessed hash objects - but nothing to write
home about.
As for speed, depends on how you index the attribute hashes. If you
just use the string $self then attribute access can be quite slow -
about half the speed of normal hash based objects in some situations.
If you do something moderately clever like caching refaddr $self in $
$self you can get access speeds that are of the same order as normal
blessed hash objects.
As for Class::Std - remember this does a /lot/ more than just inside-
out objects. I've just tacked on an example to a very basic benchmark
[1] I used when looking at different ways to do inside-out objects
and it comes in at about half the speed of blessed hash objects and
hand rolled refaddr caching inside-out objects.
That said any large system will probably end up needing some of the
extras that Class::Std provides - so will get the speed hit in any case.
Cheers,
Adrian
[1] <http://www.quietstars.com/perl/object-bench.pl> for those who
care :-)
<lurk>
More information about the Dbix-class
mailing list