develooper Front page | perl.perl5.porters | Postings from October 2001

Re: Clamp, wherefor art thou

Thread Previous | Thread Next
From:
Tim Bunce
Date:
October 30, 2001 05:23
Subject:
Re: Clamp, wherefor art thou
Message ID:
20011030132328.G15404@dansat.data-plan.com
On Mon, Oct 29, 2001 at 11:42:57PM -0800, Jeffrey Friedl wrote:
> 
> --- bleedperl.orig/hv.h	Sun Jun 17 19:01:05 2001
> +++ bleedperl/hv.h	Mon Oct 29 22:00:47 2001
> @@ -31,7 +31,7 @@
>      char *	xhv_array;	/* pointer to malloced string */
>      STRLEN	xhv_fill;	/* how full xhv_array currently is */
>      STRLEN	xhv_max;	/* subscript of last element of xhv_array */
> -    IV		xhv_keys;	/* how many elements in the array */
> +    IV		xhv_realkeys;	/* elements in the array (excl placeholders) */
>      NV		xnv_nv;		/* numeric value, if any */
>      MAGIC*	xmg_magic;	/* magic for scalar array */
>      HV*		xmg_stash;	/* class package */
> @@ -40,6 +40,7 @@
>      HE		*xhv_eiter;	/* current entry of iterator */
>      PMOP	*xhv_pmroot;	/* list of pm's for this package */
>      char	*xhv_name;	/* name, if a symbol table */
> +    IV		xhv_keys;	/* elements in the array (incl placeholders) */
>  };

I'm not very happy about growing the size of the hv struct (and it
may have knock on effects in terms of bucket allocation in malloc).

Didn't we discuss this before and agree that xnv_nv might be available
for use?  Did you (could you) look into that?

Failing that, clamped hashes could compute the key count on demand,
like perl4 always did.

> +/* "approved" (but not exists()) keys in access-clamped hashes */
> +PERLVAR(Isv_placehold,	SV)

No longer used?

> -#define SvIMMORTAL(sv) ((sv)==&PL_sv_undef || (sv)==&PL_sv_yes || (sv)==&PL_sv_no)
> +#define SvIMMORTAL(sv) ((sv)==&PL_sv_undef || (sv)==&PL_sv_yes || (sv)==&PL_sv_no || (sv)==&PL_sv_undef)

Umm, PL_sv_undef twice? (Was sv_placehold?)

> --- bleedperl.orig/hv.c	Tue Oct 16 13:44:11 2001
> +++ bleedperl/hv.c	Mon Oct 29 22:05:32 2001
> @@ -222,6 +222,14 @@
>  	    continue;
>  	if (key != keysave)
>  	    Safefree(key);
> +
> +	/* if we find the entry but it's a placeholder, it's as if not found */
> +	if (HeVAL(entry) == &PL_sv_undef)
> +	    break;
> +
> +	if (lval && SvREADONLY(HeVAL(entry)) && !SvMAGICAL(HeVAL(entry)))
> +	    Perl_warner(aTHX_ WARN_INTERNAL, "Can't modify readonly value for key {%s}", key);

Can't modify readonly value is fatal elsewhere in Perl and ...

> +    if (lval)
> +    {
> +	if (SvREADONLY(hv))
> +	    Perl_croak(aTHX_ "Can't add a new key {%s} to readonly/clamped hash", key);
> +	/* if not readonly, allowed to add a new key even if clamped */
> +    }
> +    else if (!entry && HvCLAMPEDACCESS(hv))
> +    {
> +	/*
> +	 * If we get here, we're trying to access (not add) a new key.
> +	 * If entry has something, it's because we bailed above due to
> +	 * &PL_sv_undef, and in that case it's a "pre-approved" key
> +	 * that we can access. But if not and we get here, it's an attempt
> +	 * to access a brand new key, and that's not allowed if clamped.
> +	 */
> +	Perl_croak(aTHX_ "Can't access nonexistant key {%s} of readonly/clamped hash", key);
> +    }

Those are fatal.

> -	SvREFCNT_dec(HeVAL(entry));
> +
> +	if (HeVAL(entry) == &PL_sv_undef) {
> +	    /*
> +	     * We'll be using this same slot, so the number of allocated
> +	     * keys doesn't go up, but the number of user-visible keys does.
> +	     */
> +	    xhv->xhv_realkeys++; /* HvREALKEYS(hv)++ */
> +	} else {
> +	    if (SvREADONLY(HeVAL(entry)) && !SvMAGICAL(HeVAL(entry)))
> +		Perl_croak(aTHX_ "Can't modify readonly value for key {%s}", key);
> +
> +	    SvREFCNT_dec(HeVAL(entry));
> +	}

Since PL_sv_undef is SvREADONLY and !SvMAGICAL you could move the
    (HeVAL(entry) == &PL_sv_undef)
test inside the
    (SvREADONLY(HeVAL(entry)) && !SvMAGICAL(HeVAL(entry)))
test and save a test from the common case.

> +	Perl_croak(aTHX_ "Can't delete key {%s} of readonly/clamped", key);

And don't forget perldiag.pod entries fro all these :)

Tim.

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About