Uri said:
my point is that needing large numbers of bit flags says that the design
is flawed.
Uh, that's like saying that needing a large number of objects says that
the design is flawed. A flag is nothing special. It's just a variable
that can take only two values.
this doesn't cover the case where you are munging large
numbers of boolean flags for statistics or related work (and those apps
should use pdl or bit::vector). but for a reasonable sized set of flags,
What is a reasonable sized set of flags?
Lets say you want to represent a Windows file system structure in memory
for some reason. A file has a name, a type (regular, directory, ...) and
an access control list. Each ACL is a list of entries which consist of
an entity (typically a user or group) and a number (13 with Win2K) of
permissions which may be allowed or denied.
So you could represent that as
{
name => 'foo/bar.txt',
type => 'f',
acl => {
[
{ user => 'hjp',
allow => {
read => 1,
write => 1,
append => 1,
list_perm => 1,
...
},
deny => {
},
},
{ group => 'users',
allow => {
read => 1,
list_perm => 1,
...
},
deny => {
take_ownership => 1,
...
},
},
],
}
}
Per file it isn't that much: Two hashes with at most 13 keys for each
entity which has access (which will typically be only a hand full). But
if you want to represent one million files, that's a lot of memory.
So you can store that in a much more compact form as:
{
name => 'foo/bar.txt',
type => 'f',
acl => {
[
{ user => 'hjp',
allow => 0x...,
deny => 0,
},
{ group => 'users',
allow => 0x...,
deny => 0x...,
},
],
}
}
Sure instead of
if ($ace->{allow}{read}) ...
you will have to write
if ($ace->{allow} & ACE_READ) ...
which may be less "perlish", but isn't much worse to read and probably
faster as well.
(Or you can make it an object with getter and setter methods and hide
the implementation)
it is better to use a simpler coding technique than to deal with the
trickiness of bit sized flags.
I don't find bit masks particularly tricky. But that's probably my C
background showing.
using bits for a small number of flags is micro-optimization and
shouldn't be done unless the app is ridiculously tight on space.
If you are talking about two orders of magnitude of overhead, it
doesn't take that much data to be ridiculously tight on space. My
workstation has only 512 megs of RAM
.
hp