unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: Mark H Weaver <mhw@netris.org>
To: ludo@gnu.org (Ludovic Courtès)
Cc: guile-devel@gnu.org
Subject: Re: [PATCH] Implement ‘hash’ for structs
Date: Tue, 09 Oct 2012 22:59:46 -0400	[thread overview]
Message-ID: <87ipajkp5p.fsf@tines.lan> (raw)
In-Reply-To: <87626juuzh.fsf@gnu.org> ("Ludovic \=\?utf-8\?Q\?Court\=C3\=A8s\=22'\?\= \=\?utf-8\?Q\?s\?\= message of "Wed, 10 Oct 2012 00:43:46 +0200")

Hi Ludovic!

ludo@gnu.org (Ludovic Courtès) writes:
> As incredible as it may seem, ‘hash’ until now always returned 263 % n
> for structs, leading to interesting experiences when using structs as
> hash table keys.

Yes, do you remember us talking about this long ago on IRC?  I wanted to
fix this, but asked whether changing the hash function was okay for 2.0,
and you never gave me an answer :)

Andy said that he improved the hash function on the master branch.
You might want to look at what he did.

[...]

> diff --git a/libguile/struct.c b/libguile/struct.c
> index 5837b7c..6287163 100644
> --- a/libguile/struct.c
> +++ b/libguile/struct.c
> @@ -922,6 +922,52 @@ scm_struct_ihashq (SCM obj, unsigned long n, void *closure)
>    return SCM_UNPACK (obj) % n;
>  }
>  
> +unsigned long
> +scm_i_struct_hash (SCM obj, unsigned long n)
> +#define FUNC_NAME "hash"
> +{
> +  SCM layout;
> +  scm_t_bits *data;
> +  size_t struct_size, field_num;
> +  unsigned long hash;
> +
> +  SCM_VALIDATE_STRUCT (1, obj);
> +
> +  layout = SCM_STRUCT_LAYOUT (obj);
> +  struct_size = scm_i_symbol_length (layout) / 2;
> +  data = SCM_STRUCT_DATA (obj);
> +
> +  hash = (unsigned long) SCM_PACK (SCM_STRUCT_VTABLE (obj));
> +  for (field_num = 0; field_num < struct_size; field_num++)
> +    {
> +      int protection;
> +
> +      protection = scm_i_symbol_ref (layout, field_num * 2 + 1);
> +      if (protection != 'h' && protection != 'o')
> +	{
> +	  int type;
> +	  type = scm_i_symbol_ref (layout, field_num * 2);
> +	  switch (type)
> +	    {
> +	    case 'p':
> +	      if (!scm_is_eq (obj, SCM_PACK (data[field_num])))
> +		hash ^= scm_ihash (SCM_PACK (data[field_num]), n);

I guess this 'if' is to avoid an infinite loop if the struct points back
to itself.  However, it apparently fails to detect cycles in the general
case.  I think this is a show stopper.  I think we need to detect cycles
and DTRT.

     Mark



  reply	other threads:[~2012-10-10  2:59 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-10-09 22:43 [PATCH] Implement ‘hash’ for structs Ludovic Courtès
2012-10-10  2:59 ` Mark H Weaver [this message]
2012-10-10 20:36   ` Ludovic Courtès
2012-10-11 13:00     ` Mark H Weaver
2012-10-11 14:24       ` Ludovic Courtès
2012-10-12 21:48       ` Ludovic Courtès

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87ipajkp5p.fsf@tines.lan \
    --to=mhw@netris.org \
    --cc=guile-devel@gnu.org \
    --cc=ludo@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).