D
Dennis Gavrilov
Hi, All!
I have two questions: strategic and technical.
Technical one first:
I need to share an array of objects (implemented as hashes, having
references to other objects and hashes, sharing done after blessing)
between all of the mod_perl2 threads. The structure can grow quite big
- tenths of thousands of array elements. It can grow as system
operates (not possible to construct at apache startup). Sharing array
is OK, but inserting mixed structures of hash refs (sometimes blessed)
afterward into it is problematic.
I've written a small routine that attempts to do a deep copy of
arbitrary data structure in order to accomplish the objective. The
problem is that threads::shared::share (or routine logic) seems to
replace original references. Hence circular reference handling code
does not help. Anyway routine does not work for more elaborated
structures.
Maybe there are some better approaches to the task, which I'm missing
out. Code of the routine is underneath.
Strategic one: I'm dealing with ex-CGI application, that has to run
under mod_perl 2. The main bottleneck is authorization system, which
is highly configurable and allows to define permissions up to a single
attribute of a particular row. It works nice under mod_perl 1. Changes
to object structure are automatically reflected in authorization in
both database and application server memory. The part in "application
server memory" speeds up the whole thing greatly. Now under 2nd
mod_perl every thread maintains its own idea of current state of
authorization information, which is sometimes way out of date.
I see currently several options of addressing the issue:
1. Insert everywhere quite expensive database checks and bring
authorization info of every thread up-to-date as needed - more
development and some performance trade-offs
2. Flush authorization info stored in every thread after some changes
to reflected object structure - little development, but significant
performance tradeoff
3. Share the whole thing between all of the threads - great memory and
performance gains, seemingly simple development, lack of stable
platform
Although giving the best benefits the third option has already turned
out to be a two day long nightmare.
I would greatly appreciate any opinions, ideas or comments on the two
questions above.
Thanks for reading it this far,
Dennis
sub share_struct {
my $struct = shift;
my $level = shift;
my $map = shift || {};
return unless ($] > 5.006001);
require threads;
require threads::shared;
import threads;
import threads::shared;
my $type = ref $struct;
if (exists $map->{$struct}) {
return $struct;
} elsif ($type) {
$map->{$struct} = $struct; }
die "Structure complexity exceeds maximum supported level" if
($level>5000);
if ($type) {
if ($type eq 'ARRAY') {
my @arr_copy = @{$struct};
for (my $i=0; $i<=$#arr_copy; $i++) {
$arr_copy[$i] = ${&share_struct(\$arr_copy[$i], $level++,
$map)}; }
share($struct);
@{$struct} = @arr_copy;
} elsif ($type eq 'REF' or $type eq 'SCALAR') {
if (ref $struct) {
&share_struct($$struct, $level++, $map); share($struct);
}
} else { my %hash_copy = %{$struct};
foreach my $key (keys %hash_copy) {
$hash_copy{$key} = ${&share_struct(\$hash_copy{$key}, $level++,
$map)};
}
share($struct);
%{$struct} = %hash_copy;
}
}
return $struct;
}
I have two questions: strategic and technical.
Technical one first:
I need to share an array of objects (implemented as hashes, having
references to other objects and hashes, sharing done after blessing)
between all of the mod_perl2 threads. The structure can grow quite big
- tenths of thousands of array elements. It can grow as system
operates (not possible to construct at apache startup). Sharing array
is OK, but inserting mixed structures of hash refs (sometimes blessed)
afterward into it is problematic.
I've written a small routine that attempts to do a deep copy of
arbitrary data structure in order to accomplish the objective. The
problem is that threads::shared::share (or routine logic) seems to
replace original references. Hence circular reference handling code
does not help. Anyway routine does not work for more elaborated
structures.
Maybe there are some better approaches to the task, which I'm missing
out. Code of the routine is underneath.
Strategic one: I'm dealing with ex-CGI application, that has to run
under mod_perl 2. The main bottleneck is authorization system, which
is highly configurable and allows to define permissions up to a single
attribute of a particular row. It works nice under mod_perl 1. Changes
to object structure are automatically reflected in authorization in
both database and application server memory. The part in "application
server memory" speeds up the whole thing greatly. Now under 2nd
mod_perl every thread maintains its own idea of current state of
authorization information, which is sometimes way out of date.
I see currently several options of addressing the issue:
1. Insert everywhere quite expensive database checks and bring
authorization info of every thread up-to-date as needed - more
development and some performance trade-offs
2. Flush authorization info stored in every thread after some changes
to reflected object structure - little development, but significant
performance tradeoff
3. Share the whole thing between all of the threads - great memory and
performance gains, seemingly simple development, lack of stable
platform
Although giving the best benefits the third option has already turned
out to be a two day long nightmare.
I would greatly appreciate any opinions, ideas or comments on the two
questions above.
Thanks for reading it this far,
Dennis
sub share_struct {
my $struct = shift;
my $level = shift;
my $map = shift || {};
return unless ($] > 5.006001);
require threads;
require threads::shared;
import threads;
import threads::shared;
my $type = ref $struct;
if (exists $map->{$struct}) {
return $struct;
} elsif ($type) {
$map->{$struct} = $struct; }
die "Structure complexity exceeds maximum supported level" if
($level>5000);
if ($type) {
if ($type eq 'ARRAY') {
my @arr_copy = @{$struct};
for (my $i=0; $i<=$#arr_copy; $i++) {
$arr_copy[$i] = ${&share_struct(\$arr_copy[$i], $level++,
$map)}; }
share($struct);
@{$struct} = @arr_copy;
} elsif ($type eq 'REF' or $type eq 'SCALAR') {
if (ref $struct) {
&share_struct($$struct, $level++, $map); share($struct);
}
} else { my %hash_copy = %{$struct};
foreach my $key (keys %hash_copy) {
$hash_copy{$key} = ${&share_struct(\$hash_copy{$key}, $level++,
$map)};
}
share($struct);
%{$struct} = %hash_copy;
}
}
return $struct;
}