# Complex structure vs object

This is not, strictly speaking, code review, but I think my question can be asked here.

This is a request to find out what seems to be the cleanest. This project is a blog engine that uses the file system. There are still many things to do to improve the code (it's not great). I have such a class article at initialization that will parse actual items and store it in a structure and CHI a cache system. In CHI, I store a table of hash:

 push(
@articles,
{
title   => $title, tags => \@tags, date =>$dt,
content => $content, link =>$url
}
);


I wonder if it would not be better to make a class item and store it in my cache as an object list:

 package MahewinBlogEngine::Article;

use Moo;

use Type::Params qw( compile );
use Type::Utils;
use Types::Standard qw( Object Str Int ArrayRef );

has title => (
is       => 'rw',
isa      => Str,
required => 1,
);

has tags  => (
is       => 'rw',
isa      => ArrayRef,
required => 1,
);

has date  => (
is       => 'rw',
isa      => Object,
required => 1,
);

has content => (
is       => 'ro',
isa      => Str,
required => 1,
);

is       => 'ro',
isa      => Str,
required => 1,
);

1;


And:

 my $article = MahewinBlogEngine::Article->new( title =>$title,
tags    => \@tags,
date    => $dt, content =>$content,
link    => $url ); push(@articles,$article);


This makes objects more maneuverable, powerful and extensible. It also allows you to add methods such as formatting or other things. But at the moment, I see no specific methods. It will simply be a class with attributes and accessors. So, I wonder what is best in terms of quality, performance and memory.

Another question: I think CHI already compresses, but do you think it would be wise to use Sereal and compress before storing in the CHI system cache?

The CHI uses many caching backends and you did not said which one you are using. Fastest is perl's internal arrays, then hashes and then any other implementations. CHI may use persistent files to caching, like Berkeley DB backend or one of mmap implementation to reduce memory usage. When you are using such caching to improve perfomance you may place cache files to tmpfs (or some of ramfs) on *nix systems (linux, *bsd).

So, when I'm storing a lot of data in cache and I need to reduce memory usage I prefer use BDB without CHI. Then I need speed and managing few data in cache I prefer perl's internal structures. In last case you need to remember that Perl uses optimistic memory allocation. Thats means that Perl reallocates and allocates new memory segments and never returns consumed memory back to the OS. It is very important when writing daemons and you need to limit somehow memory usage. In other cases you may use any caching system and do not worry about performance or memory usage. Just use Devel::NYTProf to find bottlenecks.

• Thank you for this very constructive response. For the moment I use CHI memory as backend. – Hobbestigrou Sep 16 '13 at 14:24