You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Simon Rosenthal <sr...@northernlight.com> on 2000/03/24 20:36:57 UTC

Small Apache::Session improvement

Hi:

Here'a a couple of small changes we've  made  here to Apache::Session which 
other people might find useful.

a) we didn't want to use the supplied generate_id function for various 
reasons, so we rolled our own, and put a reference to it in a new global, 
$Apache::Session::IdGenerator .

b) An efficiency tweak in the STORE method for the tied hash ; we found we 
were sometimes updating the session record unnecessarily  because we were 
too lazy to  consistently write code like

	$sesshash{'foo'}  = 'bar' unless ($sesshash('foo'} eq  'bar).

so, since Laziness is  said to be a  virtue for Perl programmers, we 
moved  that check into the STORE method.

context diffs follow:

- Simon
==================================================================
*** Session.pm.ORIG	Fri Mar 24 13:21:05 2000
--- Session.pm	Fri Mar 24 14:31:59 2000
***************
*** 284,293 ****
   package Apache::Session;

   use strict;
! use vars qw($VERSION);

   $VERSION = '1.03';
!
   use MD5; #yes, you need MD5.pm

   #State constants
--- 284,295 ----
   package Apache::Session;

   use strict;
! use vars qw($VERSION $IdGenerator);

   $VERSION = '1.03';
! # You can change the function used to generate ids by changing this 
global at startup time
! # e.g. $Apache::Session::IdGenerator = \&my_own_function;
! $IdGenerator = \&generate_id;
   use MD5; #yes, you need MD5.pm

   #State constants
***************
*** 371,377 ****
       }
       else {
           $self->{status} |= NEW;
!         $self->{data}->{_session_id} = generate_id();
           $self->save;
       }

--- 373,379 ----
       }
       else {
           $self->{status} |= NEW;
!         $self->{data}->{_session_id} =  &$IdGenerator();
           $self->save;
       }

***************
*** 389,399 ****
       my $self  = shift;
       my $key   = shift;
       my $value = shift;
!
!     $self->{data}->{$key} = $value;
!
!     $self->{status} |= MODIFIED;
!
       return $self->{data}->{$key};
   }

--- 391,403 ----
       my $self  = shift;
       my $key   = shift;
       my $value = shift;
! # to avoid unnecessary updates, only set MODIFIED flag if the variable 
really changed...
! 	my $prev = $self->{data}->{$key};
! 	return undef if (!defined($prev) && !defined($value));
!     if($prev ne $value) {
!       $self->{data}->{$key} = $value;
!       $self->{status} |= MODIFIED;
! 	}
       return $self->{data}->{$key};
   }








-----------------------------------------------------
Simon Rosenthal	(srosenthal@northernlight.com)    	
Web Systems Architect
Northern Light Technology 	222 Third Street, Cambridge MA 02142
Phone:  (617)577-2796  :       URL:  http://www.northernlight.com
"Northern Light - Just what you've been searching for"


Re: Small Apache::Session improvement

Posted by Adi <ad...@certsite.com>.
"Jeffrey W. Baker" wrote:
> 
> Actually the behavior is backward-compatible with existing Apache::Session
> code.  My code already ignores changes that do not occur in the top level
> hash.  This was both easy to right (sloth is a virtue), and fast at
> runtime.  The module could be rewritten to do a "deep" data structure
> comparison, but that would be both hard and slow.
> 
> -jwb

I agree it should not do a deep comparison.  That should be done outside
Apache::Session if need be.  My concern was whether you could make changes
to the top-level hash and still get the same memory location for the new
hash.

say your session data was:

	$sesshash->{'foo'} => { 'bar' => 'baz' }

	at location: HASH(0x80c8554)

you do a:

	$sesshash->{'foo'} = { 'bar' => 'baz', 'a' => 'b' };

can this new hash be allocated at the same memory location HASH(0x80c8554) ?

If this can happen, it would lose data.  Though I would guess it cannot
occur, since perl's anonymous hash syntax {} creates a brand new hash, in
unused memory, right?

So I guess it's not much of a concern.  Pardon the rambling..

-Adi

Re: Small Apache::Session improvement

Posted by "Jeffrey W. Baker" <jw...@acm.org>.
On Fri, 24 Mar 2000, Adi wrote:

> Simon Rosenthal wrote:
> > 
> > b) An efficiency tweak in the STORE method for the tied hash ; we found we
> > were sometimes updating the session record unnecessarily  because we were
> > too lazy to  consistently write code like
> > 
> >         $sesshash{'foo'}  = 'bar' unless ($sesshash('foo'} eq  'bar).
> > 
> > so, since Laziness is  said to be a  virtue for Perl programmers, we
> > moved  that check into the STORE method.
> > 
> 
> I like this hack, but what if the data you're storing is not scalar?  Since
> Apache::Session supports multi-dimensional data structures using Manfredi's
> Storable module, what would happen if you do:
> 
> 	$sesshash{'foo'} = { 'bar' => 'baz' };
> 
> >From your code below, looks like it would try to compare the two scalar-ized
> HASHes such as:
> 
> 	if("HASH(0x80c8554)" ne "HASH(0x80c8554)") {
> 
> I'm not familiar enough with perl's innards to know if this produces the
> desired behaviour.  Can a hash ref that has been modified be stored in the
> SAME MEMORY LOCATION, and thus give identical values for $prev and $value? 
> If so, then your patch would lose data in these cases.

Actually the behavior is backward-compatible with existing Apache::Session
code.  My code already ignores changes that do not occur in the top level
hash.  This was both easy to right (sloth is a virtue), and fast at
runtime.  The module could be rewritten to do a "deep" data structure
comparison, but that would be both hard and slow.

-jwb


Re: Small Apache::Session improvement

Posted by Adi <ad...@certsite.com>.
Simon Rosenthal wrote:
> 
> b) An efficiency tweak in the STORE method for the tied hash ; we found we
> were sometimes updating the session record unnecessarily  because we were
> too lazy to  consistently write code like
> 
>         $sesshash{'foo'}  = 'bar' unless ($sesshash('foo'} eq  'bar).
> 
> so, since Laziness is  said to be a  virtue for Perl programmers, we
> moved  that check into the STORE method.
> 

I like this hack, but what if the data you're storing is not scalar?  Since
Apache::Session supports multi-dimensional data structures using Manfredi's
Storable module, what would happen if you do:

	$sesshash{'foo'} = { 'bar' => 'baz' };