You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by "Barksdale, Ray" <ba...@mdot.state.ms.us> on 2005/02/17 20:56:51 UTC

sharing big global hash and objects

I have a large structure loaded from a db table that contains parameter
values.
The structure looks something like following (please excuse formatting, MS
lookOut)

my $hash = {
	 table1 => {
		field1 => {
			param1 => val,
			param2 => val,
			etc. },
	
		field2 => {
			param1 => val,
			param2 => val,
			etc. },

	},

	table2 => {
		fieldA => {
			param1 => val,
			param2 => val,
			etc. },

		fieldB => {
			param1 => val,
			param2 => val,
			etc. },
	},
};
		
The module containing $hash is loaded in startup.pl at boot.
I then have a package that creates an object based on a slice of the hash:

package FooBar;

use Config qw($hash);

sub new {
	my ($class, $table, $field) = @_;

	# bless hash slice instead of copying to {} and blessing
	my $self = bless $hash->{$table}->{$field}, $class;

	return $self;

}

followed by various read-only access methods.
__END__


My question is will this cause $hash (or the used parts) to come unshared
in mod_perl2? Does Perl mark the anonymous hash ($hash->{$table}->{$field})
as being blessed into FooBar thereby causing a copy-on-write?

I feel like I may have taken my shoe off and started loading bullets in the
gun :?

Thanks,

jrbiii



*****CONFIDENTIALITY NOTICE*****
This e-mail and any files or attachments may contain confidential and
privileged information.  If you have received this message in error, please
notify the sender at the above e-mail address and delete it and all copies
from your system.



Re: sharing big global hash and objects

Posted by Perrin Harkins <pe...@elem.com>.
On Thu, 2005-02-17 at 13:56 -0600, Barksdale, Ray wrote:
> The module containing $hash is loaded in startup.pl at boot.
> I then have a package that creates an object based on a slice of the hash:
> 
> package FooBar;
> 
> use Config qw($hash);
> 
> sub new {
> 	my ($class, $table, $field) = @_;
> 
> 	# bless hash slice instead of copying to {} and blessing
> 	my $self = bless $hash->{$table}->{$field}, $class;
> 
> 	return $self;

Does that work?  It looks really strange to me, and may be doing some
copying.

> My question is will this cause $hash (or the used parts) to come unshared
> in mod_perl2?

Probably.  There are some random factors involved here, like page
boundaries.

>  Does Perl mark the anonymous hash ($hash->{$table}->{$field})
> as being blessed into FooBar thereby causing a copy-on-write?

It does mark variables when you bless them.

Why don't you do all of this before forking?

- Perrin