unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Ihor Radchenko <yantar92@gmail.com>
To: Andreas Schwab <schwab@suse.de>
Cc: emacs-devel@gnu.org
Subject: Re: Comparing hash table objects
Date: Wed, 08 Jun 2022 17:17:31 +0800	[thread overview]
Message-ID: <87leu7tsyc.fsf@localhost> (raw)
In-Reply-To: <mvmr13zedxl.fsf@suse.de>

Andreas Schwab <schwab@suse.de> writes:

> On Jun 08 2022, Ihor Radchenko wrote:
>
>> Is there any way to compare objects containing hash tables for equality?
>
> Hash tables are only equal if eq.

This is somewhat unexpected.

To clarify my problem, let me explain in more details what I am trying
to achieve.

Currently, I am using structs to store objects with properties:
(setq obj1 (type (:key1 val1 :key2 val2 ...)))
(setq obj2 (type (:key1 val1 :key2 val2 ...))) ;; the same properties

With this approach, I can easily compare objects with (equal obj1 obj2),
use them as hash table keys, etc.

However, when the number of properties increases, :key access becomes
slow because plist-get has O(N) complexity.

The natural solution is using hash tables:

(setq obj1 (type (make-hash-table)))
(setq obj2 (type (make-hash-table)))
;; fill the hash tables with :keyN valN pairs

Now, I can retrieve the :key in O(1).

However, all of a sudden, I am unable to compare two objects with this
new internal structure. (equal obj1 obj2) returns nil.

The described behavior potentially generates a lot of room for errors.
It is relatively easy to remember that different hash table objects are
never equal (only equal when also eq). However, when the hash tables are
hidden inside a struct, it is easy to overlook that the structs cannot
be compared with equal as usual.

Best,
Ihor




  reply	other threads:[~2022-06-08  9:17 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <87wn1axgh6.fsf@breatheoutbreathe.in>
     [not found] ` <83jzx925lv.fsf@gnu.org>
     [not found]   ` <87a5xubwoo.fsf@breatheoutbreathe.in>
     [not found]     ` <87v8csku60.fsf@breatheoutbreathe.in>
     [not found]       ` <83cyyz94c6.fsf@gnu.org>
     [not found]         ` <87a5u2ydzg.fsf@breatheoutbreathe.in>
     [not found]           ` <83msy25g0s.fsf@gnu.org>
     [not found]             ` <624CBB7F-1442-400D-8D4D-1B26EBE9DACB@breatheoutbreathe.in>
     [not found]               ` <jwvr0ndq3b8.fsf-monnier+emacs@gnu.org>
     [not found]                 ` <877cp5bmig.fsf@breatheoutbreathe.in>
     [not found]                   ` <jwvtts8ib4b.fsf-monnier+emacs@gnu.org>
2022-06-08  8:41                     ` Comparing hash table objects Ihor Radchenko
2022-06-08  8:51                       ` Andreas Schwab
2022-06-08  9:17                         ` Ihor Radchenko [this message]
2022-06-10 22:45                           ` Richard Stallman
2022-06-11  5:52                             ` Ihor Radchenko
2022-06-11 16:04                               ` Stefan Monnier
2022-06-12  9:16                                 ` Ihor Radchenko
2022-06-12 23:55                                 ` Sam Steingold
2022-06-13 13:02                                   ` Stefan Monnier
2022-06-13 16:18                                     ` Sam Steingold
     [not found]                       ` <8734zoaolv.fsf@localhost>
     [not found]                         ` <jwv1qf8iqua.fsf-monnier+emacs@gnu.org>
     [not found]                           ` <87fs3o8uil.fsf@localhost>
     [not found]                             ` <87msxwa8kd.fsf@breatheoutbreathe.in>
     [not found]                               ` <87il8j7ji9.fsf@localhost>
     [not found]                                 ` <80479897-500e-fe60-6586-0a44ccb5993b@daniel-mendler.de>
     [not found]                                   ` <877coz7f6h.fsf@localhost>
     [not found]                                     ` <86d6e412-9e5b-9086-56ce-e3794085096a@daniel-mendler.de>
2023-09-09 12:12                                       ` compat.el and Emacs unstable master branch features (was: bug#63513: [PATCH] Make persist-defvar work with records and hash tables) Ihor Radchenko
2023-09-09 12:29                                         ` Daniel Mendler
2023-09-09 16:52                                           ` Joseph Turner
2023-09-11  8:45                                           ` Ihor Radchenko
2023-09-12 10:02                                             ` compat.el and Emacs unstable master branch features Philip Kaludercic
2023-09-12 10:27                                               ` Daniel Mendler
2023-09-13 10:31                                                 ` Philip Kaludercic
2023-09-13 17:11                                                   ` Daniel Mendler
2023-10-15  8:43                                                     ` Ihor Radchenko
2023-10-15 12:09                                                       ` Philip Kaludercic

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/emacs/

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

  git send-email \
    --in-reply-to=87leu7tsyc.fsf@localhost \
    --to=yantar92@gmail.com \
    --cc=emacs-devel@gnu.org \
    --cc=schwab@suse.de \
    /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.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

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).