From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Ihor Radchenko Newsgroups: gmane.emacs.devel Subject: Re: Comparing hash table objects Date: Wed, 08 Jun 2022 17:17:31 +0800 Message-ID: <87leu7tsyc.fsf@localhost> References: <871qvz4kdw.fsf@localhost> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="31936"; mail-complaints-to="usenet@ciao.gmane.io" Cc: emacs-devel@gnu.org To: Andreas Schwab Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Wed Jun 08 11:19:05 2022 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nyrqC-0007w9-4j for ged-emacs-devel@m.gmane-mx.org; Wed, 08 Jun 2022 11:19:04 +0200 Original-Received: from localhost ([::1]:52260 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nyrqA-0002Ag-Hd for ged-emacs-devel@m.gmane-mx.org; Wed, 08 Jun 2022 05:19:02 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:42992) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nyro8-0001NC-Fv for emacs-devel@gnu.org; Wed, 08 Jun 2022 05:16:56 -0400 Original-Received: from mail-pg1-x535.google.com ([2607:f8b0:4864:20::535]:40670) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nyro7-0004oO-1m for emacs-devel@gnu.org; Wed, 08 Jun 2022 05:16:56 -0400 Original-Received: by mail-pg1-x535.google.com with SMTP id f65so8144821pgc.7 for ; Wed, 08 Jun 2022 02:16:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:in-reply-to:references:date:message-id :mime-version; bh=m3b8ctcJlCcX4vK9sHhuW8WYhoIiZ7LLfDNH6YwggU0=; b=mDsw+1AEPj23Ub34Vg7LK7IEE1EhyAEBG1wyyAlwDnnpaJNten0mLJp/uqEBTCK5zC Kpn2020e+uuvIU2oEqwktrOIhRVuHYt+GgLO74XtWKfNtyMOmcxsc+xNWM7y4P+dQgZH 740PVwhmyVdvsixZoblQ7n9vhyf3HSjjilQ+gHxU9rTWPXNN+VJRI9gNvuA5BZOSo6hU gB1xG5BJ5pkYDnabEL3Te3m2dwk+4F5nAOdp0gNirMC8THec2YhTGIoRur4KXf4jAE0J SngRzJLC7SBJEwCm5a95viKt++5GacMbp7/umCEyfLMgJVkLkPrQ5W+K3mRricKZFqrl ou2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:in-reply-to:references:date :message-id:mime-version; bh=m3b8ctcJlCcX4vK9sHhuW8WYhoIiZ7LLfDNH6YwggU0=; b=VeNnZwd8QagpVp4ieQsnD6SaCmRgE6cYMWZQU7ESUwSu21zoGVRYKLtEz2I9l7r3HJ xK1OO4uhm9mW3z7qBeU0GnDnbwuLhkmtfflkUP5HlVDX9yrJmC85aKBpvqhLnbgAXFya EAKTKXtSNPzgeIm7Gkh0T+h4g3nJ9whIP+SlPahrsBV/Xo9dn859x81FJ85TIdmBIzrG gSuPCh3HrUDx3XnKfp7HfRyCYUo8MV44g7WfmAgz+20//wfzVjEM77H6fMgttefIB2M9 yesdcJOZ8IfGPicFNHHM+lxGsqSFuRD7h/1cW5Vs0b+DHq/s40hs1aVo+6pqLvUsFbrl iGxg== X-Gm-Message-State: AOAM533LG/L8M6OlknYch+/eHtyewGAf8MEw6xRgOs9F+2s4Cbj1Meq1 IzV0zmySZYEoSWNs8fcGhiY= X-Google-Smtp-Source: ABdhPJykjEiYXgZ3E9LJuDm1/su+obZ3VuZah27WBfGR1ItK4q1tPTK2g6xMzVPbTElQmjnYFjNzBQ== X-Received: by 2002:a63:2ccb:0:b0:3fe:1929:b30b with SMTP id s194-20020a632ccb000000b003fe1929b30bmr4875550pgs.64.1654679813628; Wed, 08 Jun 2022 02:16:53 -0700 (PDT) Original-Received: from localhost ([64.32.23.62]) by smtp.gmail.com with ESMTPSA id l12-20020a170903004c00b00161929fb1adsm13961208pla.54.2022.06.08.02.16.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Jun 2022 02:16:53 -0700 (PDT) In-Reply-To: Received-SPF: pass client-ip=2607:f8b0:4864:20::535; envelope-from=yantar92@gmail.com; helo=mail-pg1-x535.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.io gmane.emacs.devel:290912 Archived-At: Andreas Schwab 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