Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

For every problem there is one solution which is simple, neat, and wrong. -- H. L. Mencken


devel / comp.lang.python / Re: Best practice for caching hash

SubjectAuthor
o Re: Best practice for caching hashMarco Sulla

1
Re: Best practice for caching hash

<mailman.318.1647435653.2329.python-list@python.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17534&group=comp.lang.python#17534

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: Marco.Su...@gmail.com (Marco Sulla)
Newsgroups: comp.lang.python
Subject: Re: Best practice for caching hash
Date: Wed, 16 Mar 2022 14:00:13 +0100
Lines: 37
Message-ID: <mailman.318.1647435653.2329.python-list@python.org>
References: <CABbU2U9CwzB=ko_2n-QCG32MC3kn6R7R-m-TcOJZPPMWsULGxw@mail.gmail.com>
<YjEF9a4RRTjnEpBx@cskk.homeip.net>
<CABbU2U8AuzeRq_cnF=pX0fXZZeBz_k5qmpoPnEByuEUJVwQC7w@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
X-Trace: news.uni-berlin.de Cd33npKhUu02tnmLt6bJswDr3N6cTBH/snNFQmf2B18g==
Return-Path: <elbarbun@gmail.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=gmail.com header.i=@gmail.com header.b=nQT1KmQi;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.003
X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; '2022': 0.05; 'mar': 0.07;
'pep': 0.07; 'compute': 0.09; 'identical': 0.09;
'received:209.85.219': 0.09; 'set.': 0.09; 'values.': 0.09;
'memory': 0.15; 'cameron': 0.16; 'dict': 0.16; 'exception.': 0.16;
'from:name:marco sulla': 0.16; 'hash': 0.16; 'removes': 0.16;
'sensible': 0.16; 'simpson': 0.16; 'tuple': 0.16; 'understand.':
0.16; 'values,': 0.16; 'wrote:': 0.16; 'values': 0.17; "can't":
0.17; 'calls': 0.19; 'to:addr:python-list': 0.20; "i'd": 0.24;
'function': 0.27; 'raise': 0.31; 'issues.': 0.32; 'message-
id:@mail.gmail.com': 0.32; 'subject:for': 0.33; 'same': 0.34;
'header:In-Reply-To:1': 0.34; 'received:google.com': 0.34;
'from:addr:gmail.com': 0.35; 'member': 0.37; 'currently': 0.37;
"it's": 0.37; 'received:209.85': 0.37; 'could': 0.38;
'received:209': 0.39; 'wed,': 0.39; 'both': 0.40; 'requirement':
0.64; 'well': 0.65; 'types': 0.67; 'that,': 0.67; 'little': 0.73;
'subsequent': 0.76; 'inclined': 0.84; 'prevented': 0.84;
'stability': 0.84; 'badly': 0.91
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112;
h=mime-version:references:in-reply-to:from:date:message-id:subject:to;
bh=nwCVxeYkyovU33CFfG9wnLOR2JoJDqhEtIUWNr7fnsw=;
b=nQT1KmQiH4Dm7cGJM5d//JAApinL6rC1mQZ4dZWK8Q/ZpNDlQtv2V2RH7gYLTFuJQp
ZjMffCeBKCszyjFoMuHcLrRY++HiFnm8SQrBTvcGnf3KzgFwxb2STGh1I+BzX30ojLYj
m1yJK9+3S50bX08f/UqCZiSt6JhRwUswwE8MNcSmXkCZhU6tkZsmJMIKvQl4n5INWCj7
bhLgjAjz10sow9sKxLo2wPVTZJ3Z+vz9icI3o3EHSCNZlN9pSVqFvvEcVOQu3ztgfC6+
HLByeL2k5BECSy5JosePTuPH1hNmJ+Htv96SVEsocsIrC69265uycHWGueyTld3FkTBh
VNBQ==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20210112;
h=x-gm-message-state:mime-version:references:in-reply-to:from:date
:message-id:subject:to;
bh=nwCVxeYkyovU33CFfG9wnLOR2JoJDqhEtIUWNr7fnsw=;
b=jtSUV2z4lcc652cKZYIuKnZ/Xgk6KKdh2AOLJV84k+/84fO8rA06sMi2qB74tpOckn
mAIPc/eWExcJGCn/x44GR+j9hO3/gh4tfWPoic+E6OxhMBosoEtlIOY2m+PwkdttHk8r
IN4DXuRG697B2agvIpaQ4WQdga/1PTmgvJf3Q0lgh+m4z0KariEMQ3doVRgLlN/0V6HP
fW/hym35zebaPoailSTRrpIvDqDVi5767jBqBpLChy7OEpbRBtWB2pzWnbZGDbmEPgu+
hei59b6/MJpk7tXehksFQ1gSVc2QpUlModp2x+/jS+pPwoB4/a1mIoX8tPvxz1Ibgqph
Xm2w==
X-Gm-Message-State: AOAM530aJ/SOf4kyvQ0OEZW4orCdia7WQx3Nh48Tb656/y/HBAP9tOKJ
rZe4OOn1X0pZ1mznS1TADdYm6aucPLaNTJqQFpcsmy+y
X-Google-Smtp-Source: ABdhPJyTKxcbLqGpELFBi94ALUdKwZCEnj/HDt9edcsQUytBDDf4u/LPoCUdCJXE5A27grcR6dNdqP1JbnAnI1QsF+o=
X-Received: by 2002:a5b:242:0:b0:61d:b9c9:5431 with SMTP id
g2-20020a5b0242000000b0061db9c95431mr30227291ybp.82.1647435650428; Wed, 16
Mar 2022 06:00:50 -0700 (PDT)
In-Reply-To: <YjEF9a4RRTjnEpBx@cskk.homeip.net>
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <CABbU2U8AuzeRq_cnF=pX0fXZZeBz_k5qmpoPnEByuEUJVwQC7w@mail.gmail.com>
X-Mailman-Original-References: <CABbU2U9CwzB=ko_2n-QCG32MC3kn6R7R-m-TcOJZPPMWsULGxw@mail.gmail.com>
<YjEF9a4RRTjnEpBx@cskk.homeip.net>
 by: Marco Sulla - Wed, 16 Mar 2022 13:00 UTC

On Wed, 16 Mar 2022 at 00:42, Cameron Simpson <cs@cskk.id.au> wrote:
>
> Is it sensible to compute the hash only from the immutable parts?
> Bearing in mind that usually you need an equality function as well and
> it may have the same stability issues.

[...]

> In that case I would be inclined to never raise TypeError at all. I'd
> compute the hash entirely from the keys of the dict and compute equality
> in the normal fashion: identical keys and then equal corresponding
> values. That removes the requirement that values be immutable and/or
> hashable.

Well, I followed PEP 416, so I allowed immutable types for values, as
tuple does. Also tuple is hashable only if all its values are
hashable.

The equality function is the same as dict, with a little modification.
I do not check for hash in equality. I could add that, if both the
hash are calculated and different from -1 and they differ, False is
returned.

> >In this case I currently cache the value -1. The subsequent calls to
> >__hash__() will check if the value is -1. If so, a TypeError is
> >immediately raised.
>
> This will also make these values behave badly in dicts/sets, as they all
> hash to the same bucket.

Not sure to understand. If the hash is -1, it's not hashable, so it
can't be a member of a dict or set.

> You could, you know, cache the original exception.

I thought about it :) What prevented me is that is another PySsize_t
to store in memory

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor