Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

No directory.


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

SubjectAuthor
o Re: Best practice for caching hashChris Angelico

1
Re: Best practice for caching hash

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: ros...@gmail.com (Chris Angelico)
Newsgroups: comp.lang.python
Subject: Re: Best practice for caching hash
Date: Wed, 16 Mar 2022 10:57:48 +1100
Lines: 43
Message-ID: <mailman.312.1647388680.2329.python-list@python.org>
References: <CABbU2U9CwzB=ko_2n-QCG32MC3kn6R7R-m-TcOJZPPMWsULGxw@mail.gmail.com>
<YjEF9a4RRTjnEpBx@cskk.homeip.net>
<CAPTjJmreW-t5ggSg+Re_Rvw7yf9RRLp_mav_8pGej-zrfwoAhw@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
X-Trace: news.uni-berlin.de nkGluSh6N1XAXLn+2fRL9QIE32VpeKafPkVdw19DTLmQ==
Return-Path: <rosuav@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=ZhHRiztO;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.010
X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'traceback': 0.04; '2022':
0.05; 'error:': 0.05; 'mar': 0.07; 'compute': 0.09; 'example:':
0.09; 'objects,': 0.09; '(it': 0.16; 'arbitrary': 0.16; 'caching':
0.16; 'cameron': 0.16; 'chrisa': 0.16; 'exception.': 0.16;
'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16;
'hash': 0.16; "it'd": 0.16; 'object,': 0.16; 'objects.': 0.16;
'permitted': 0.16; 'reference,': 0.16; 'sensible': 0.16;
'simpson': 0.16; 'such,': 0.16; 'tuple': 0.16; 'wrote:': 0.16;
'problem': 0.16; "can't": 0.17; 'to:addr:python-list': 0.20;
'idea': 0.24; 'anything': 0.25; 'object': 0.26; "isn't": 0.27;
'function': 0.27; 'wrong': 0.28; 'error': 0.29; 'raise': 0.31;
'think': 0.32; 'here,': 0.32; 'issues.': 0.32; 'objects': 0.32;
'message-id:@mail.gmail.com': 0.32; 'but': 0.32; "i'm": 0.33;
'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; 'those':
0.36; "it's": 0.37; 'received:209.85': 0.37; 'received:209': 0.39;
'added': 0.39; 'valid': 0.39; 'wed,': 0.39; 'exact': 0.40; 'skip:h
10': 0.61; 'reasonable': 0.62; 'here': 0.62; 'simply': 0.63;
'similar': 0.65; 'well': 0.65; 'numbers': 0.67; 'entire': 0.67;
'type:': 0.69; 'times': 0.69; 'little': 0.73; 'associates': 0.76;
'subsequent': 0.76; 'composed': 0.84; 'permits': 0.84;
'stability': 0.84; 'stable,': 0.84; 'sulla': 0.84; 'cheap': 0.91;
'alive.': 0.93; 'details,': 0.95
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=JX7LPrvPmPiAQCnHH432CbehAf1EpgONwBAj2x2cPuQ=;
b=ZhHRiztO56AQ5fN4eTusbmhnT2hj7N94C5I7oBrHXr8o0epV1uOlMk8pUOSDMnp7uF
98HEHwy5/wyzTAwmuTjVEHACS2EFbeU37gd1qOMIa51hKF+bFCMm/fuaPbX6gmNDsZc6
vCWgCYVwkPvtz3pgFWMUniFthWl+B2GnqByntos013Os0a8dvceO0m9dDvxZGDixh2Yg
iQW2vF7ET7LPhDV36Exmtd9xgD8wjMY0YSK69xxhuNwOoyGdvuQQp4wdXgUz2dVM0nEs
CgbZ7KJKl8vPZCbVrehhG9mKf7j51Cgo994a1zT7k/PcNgU2ilZ5Dae6zxZaZxNwxb8O
OELQ==
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=JX7LPrvPmPiAQCnHH432CbehAf1EpgONwBAj2x2cPuQ=;
b=ISeqGh4vwwGkuR6fWgzO5lHhjREtXgPmTVseEVcNlxVgHkgd6TU6I684JYAJz9eYVO
T/BY9OWDzOh7UXTtZurNvP3CW+xavAYXmkdMsclG41dWf6jsJ25S07gJQ8WeaLN2cEq+
U5bbo7rAkxOeJBBvRQAGJLjpBDy1tezFACvnOTbEcVByTPax9AcAyDEbx66Av0tiK3h+
aXhCJp9/0CB5tJavgh9shUbjZqtVHJ7aKYUEKRyzFiMAwyIBLTtJ7Ce9LTxkUrOaDJpl
6IY7mTQD3o4f6dEolQgJ4kQvCYeP5h4StwxHAs2952WxIp5wWaoIHSMqTncQ2T3O85CB
q3VA==
X-Gm-Message-State: AOAM5315cS9+EpGoW3rBFzghpPYhNmCQAfzoq3nhGuk2fsnAkWaSIyv+
BSo0PyZH+CVij2e2M4QXzYBWfaFN9gTBAHh1wDkjy2flsjU=
X-Google-Smtp-Source: ABdhPJwhgJtjfnik2YNWB3Lelv/xDF15p2oy0gC4S91lm/XlYF0v0ymSdO69yuPMD9D0aRZHKRvCu5e7y7dLgXdvvtA=
X-Received: by 2002:a05:600c:1c02:b0:389:cf43:eafa with SMTP id
j2-20020a05600c1c0200b00389cf43eafamr5157257wms.203.1647388678870; Tue, 15
Mar 2022 16:57:58 -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: <CAPTjJmreW-t5ggSg+Re_Rvw7yf9RRLp_mav_8pGej-zrfwoAhw@mail.gmail.com>
X-Mailman-Original-References: <CABbU2U9CwzB=ko_2n-QCG32MC3kn6R7R-m-TcOJZPPMWsULGxw@mail.gmail.com>
<YjEF9a4RRTjnEpBx@cskk.homeip.net>
 by: Chris Angelico - Tue, 15 Mar 2022 23:57 UTC

On Wed, 16 Mar 2022 at 10:42, Cameron Simpson <cs@cskk.id.au> wrote:
>
> On 12Mar2022 21:45, Marco Sulla <Marco.Sulla.Python@gmail.com> wrote:
> >I have a custom immutable object, and I added a cache for its hash
> >value. The problem is the object can be composed of mutable or
> >immutable objects, so the hash can raise TypeError.
>
> 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.

My understanding - and I'm sure Marco will correct me if I'm wrong
here - is that this behaves like a tuple: if it contains nothing but
hashable objects, it is itself hashable, but if it contains anything
unhashable, the entire tuple isn't hashable.

(Though it's a little confusing; a frozendict has to have nothing but
immutable objects, yet it permits them to be unhashable? I know of
hashable mutable objects, but can't think of any unhashable immutable
objects. And I'm not sure whether a hashable-mutable would be
permitted in a frozendict, or whether it'd even know.)

As such, any valid hash value will be stable, and "asking for a hash
will raise TypeError" is also stable.

> >The problem is the first time I get an error with details, for example:
> >
> >TypeError: unhashable type: 'list'
> >
> >The subsequent times I simply raise a generic error:
> >
> >TypeError
>
> You could, you know, cache the original exception. That does keep links
> to the traceback and therefore all the associates stack frames, so that
> isn't cheap (it is peerfectly fast - just the reference, t just prevents
> those things from being reclaimed).

I don't like that idea myself, for that exact reason - it'll keep
arbitrary numbers of objects alive. But caching the stringified form
would be more reasonable here, and have similar effect.

ChrisA

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor