Re: frozendict: an experiment

2020-07-21 Thread Marco Sulla
On Tue, 21 Jul 2020 at 06:01, Inada Naoki wrote: > On Tue, Jul 21, 2020 at 5:07 AM Marco Sulla wrote: > > > > I just finished to improve the performance of frozendict creation. The > result is very promising. > > > > The speedup is about 30% for small dicts (8 items). For large dicts (1k > items

Re: frozendict: an experiment

2020-07-20 Thread Inada Naoki
On Tue, Jul 21, 2020 at 5:07 AM Marco Sulla wrote: > > I just finished to improve the performance of frozendict creation. The result > is very promising. > > The speedup is about 30% for small dicts (8 items). For large dicts (1k > items) is about 38% for dicts with only integers as keys and val

Re: frozendict: an experiment

2020-07-20 Thread Marco Sulla
I just finished to improve the performance of frozendict creation. The result is very promising. The speedup is about 30% for small dicts (8 items). For large dicts (1k items) is about 38% for dicts with only integers as keys and values, and 45% for dicts with only strings. There's also a little

Re: frozendict: an experiment

2020-07-18 Thread Inada Naoki
On Sun, Jul 19, 2020 at 6:38 AM Marco Sulla wrote: > > On Sat, 18 Jul 2020 at 10:02, Inada Naoki wrote: >> >> On Sat, Jul 18, 2020 at 7:05 AM Marco Sulla >> wrote: >> > For what I know, CPython uses PyDictObject for kwargs. Since dicts are >> > mutable, it's a problem to cache them properly. >>

Re: frozendict: an experiment

2020-07-18 Thread Marco Sulla
On Sat, 18 Jul 2020 at 10:02, Inada Naoki wrote: > On Sat, Jul 18, 2020 at 7:05 AM Marco Sulla > wrote: > > For what I know, CPython uses PyDictObject for kwargs. Since dicts are > > mutable, it's a problem to cache them properly. > > On caller side, Python doesn't use dict at all. > On callee s

Re: frozendict: an experiment

2020-07-18 Thread Inada Naoki
On Sat, Jul 18, 2020 at 7:05 AM Marco Sulla wrote: > > > > > But when frozendicts are merged? > > I think there is a very little chance. > > frozendicts could be used for kwargs: > > f(a=1, b=2) > # some code > f(a=1, b=2) > > For what I know, CPython uses PyDictObject for kwargs. Since dicts are

Re: frozendict: an experiment

2020-07-17 Thread Marco Sulla
On Fri, 17 Jul 2020 at 04:13, Inada Naoki wrote: > > 3. many python internals uses a mapping proxy to a dict, to avoid its > > modification. A frozendict can be used instead. > > Are they used frequently in performance critical path? > Could you point some concrete examples? I searched a little i

Re: frozendict: an experiment

2020-07-16 Thread Inada Naoki
On Fri, Jul 17, 2020 at 2:16 AM Marco Sulla wrote: > > On Thu, 16 Jul 2020 at 06:11, Inada Naoki wrote: > > On Thu, Jul 16, 2020 at 2:32 AM Marco Sulla > > wrote: > > > Yes, but, instead of creating a view, you can create and cache the > > > pointer of a "real" object, that implements the dict v

Re: frozendict: an experiment

2020-07-16 Thread Marco Sulla
On Thu, 16 Jul 2020 at 06:11, Inada Naoki wrote: > On Thu, Jul 16, 2020 at 2:32 AM Marco Sulla > wrote: > > Yes, but, instead of creating a view, you can create and cache the > > pointer of a "real" object, that implements the dict view API. > > For example, keys for a frozendict could be an "ord

Re: frozendict: an experiment

2020-07-16 Thread Marco Sulla
On Wed, 15 Jul 2020 at 08:07, Inada Naoki wrote: > I don't think so. The view objects are useful when we need a set-like > operation. (e.g. `assert d.keys() == {"spam", "egg"}`) Yes, but, instead of creating a view, you can create and cache the pointer of a "real" object, that implements the dic

Re: frozendict: an experiment

2020-07-15 Thread Inada Naoki
On Thu, Jul 16, 2020 at 2:32 AM Marco Sulla wrote: > > On Wed, 15 Jul 2020 at 08:07, Inada Naoki wrote: > > I don't think so. The view objects are useful when we need a set-like > > operation. (e.g. `assert d.keys() == {"spam", "egg"}`) > > Yes, but, instead of creating a view, you can create an

Re: frozendict: an experiment

2020-07-14 Thread Inada Naoki
On Wed, Jul 15, 2020 at 2:01 AM Marco Sulla wrote: > > > Why do you think I do not need views to use the frozendict? > > I thought that is what make d.key(), d.items() etc work? > > Views for dict exist because dict is mutable. See this example: > > >>> d = {1: 2} > >>> keys = d.keys() > >>> d[2]

Re: frozendict: an experiment

2020-07-14 Thread Marco Sulla
On Mon, 13 Jul 2020 at 19:28, Barry Scott wrote: > > On 13 Jul 2020, at 03:20, Marco Sulla wrote: > > So why did I try to implement it? IMO, apart the considerations in PEP > > 416, a frozendict can be useful: > > > > - as a faster base for types.MutableMappingProxy > > - as a substitute of named

Re: frozendict: an experiment

2020-07-13 Thread Barry Scott
> On 13 Jul 2020, at 03:20, Marco Sulla wrote: > > TL;DR: I tried to implement in CPython a frozendict here: > https://github.com/Marco-Sulla/cpython > > Long explaining: > > What is a frozendict? It's an immutable dict. The type was proposed in > the past but rejected: https://www.python.or

Re: frozendict

2012-02-13 Thread John Nagle
On 2/10/2012 9:52 PM, 8 Dihedral wrote: 在 2012年2月11日星期六UTC+8上午2时57分34秒,John Nagle写道: On 2/10/2012 10:14 AM, Nathan Rice wrote: Lets also not forget that knowing an object is immutable lets you do a lot of optimizations; it can be inlined, it is safe to convert to a contiguous block of memor

Re: frozendict

2012-02-10 Thread 88888 Dihedral
在 2012年2月11日星期六UTC+8上午2时57分34秒,John Nagle写道: > On 2/10/2012 10:14 AM, Nathan Rice wrote: > >>> Lets also not forget that knowing an object is immutable lets you do a > >>> lot of optimizations; it can be inlined, it is safe to convert to a > >>> contiguous block of memory and stuff in cache, etc.

Re: frozendict

2012-02-10 Thread John Nagle
On 2/10/2012 10:14 AM, Nathan Rice wrote: Lets also not forget that knowing an object is immutable lets you do a lot of optimizations; it can be inlined, it is safe to convert to a contiguous block of memory and stuff in cache, etc. If you know the input to a function is guaranteed to be frozen

Re: frozendict

2012-02-10 Thread Nathan Rice
>> Lets also not forget that knowing an object is immutable lets you do a >> lot of optimizations; it can be inlined, it is safe to convert to a >> contiguous block of memory and stuff in cache, etc.  If you know the >> input to a function is guaranteed to be frozen you can just go crazy. >> Being

Re: frozendict

2012-02-10 Thread Chris Rebert
On Fri, Feb 10, 2012 at 8:53 AM, Nathan Rice wrote: > Lets also not forget that knowing an object is immutable lets you do a > lot of optimizations; it can be inlined, it is safe to convert to a > contiguous block of memory and stuff in cache, etc.  If you know the > input to a function is guaran

Re: frozendict

2012-02-10 Thread Nathan Rice
On Fri, Feb 10, 2012 at 5:08 AM, Chris Angelico wrote: > On Fri, Feb 10, 2012 at 1:30 PM, Nathan Rice > wrote: >> The only thing needed to avoid the hash collision is that your hash >> function is not not 100% predictable just by looking at the python >> source code.  I don't see why every dict w

Re: frozendict

2012-02-10 Thread Chris Angelico
On Fri, Feb 10, 2012 at 1:30 PM, Nathan Rice wrote: > The only thing needed to avoid the hash collision is that your hash > function is not not 100% predictable just by looking at the python > source code.  I don't see why every dict would have to be created > differently.  I would think having th

Re: frozendict

2012-02-09 Thread anon hung
> I've been trying for a few days (only a little bit at a time) to come up > with a way of implementing a frozendict that doesn't suck. I'm gradually > converging to a solution, but I can't help but think that there's some > subtlety that I'm probably missing which is why it's not already provided.

Re: frozendict

2012-02-09 Thread Terry Reedy
On 2/9/2012 9:30 PM, Nathan Rice wrote: That day may be sooner than you think. It is very likely that in Python 3.3, dict order will be randomized on creation as a side-effect of adding a random salt to hashes to prevent a serious vulnerability in dicts. http://securitytracker.com/id/1026478 h

Re: frozendict

2012-02-09 Thread Nathan Rice
On Thu, Feb 9, 2012 at 8:24 PM, Steven D'Aprano wrote: > On Thu, 09 Feb 2012 09:35:52 -0700, Ian Kelly wrote: > >> On Thu, Feb 9, 2012 at 8:19 AM, Nathan Rice >> wrote: >>> As I said, two dictionaries created from the same input will be the >>> same... >> >> That's an implementation detail, not a

Re: frozendict

2012-02-09 Thread Steven D'Aprano
On Thu, 09 Feb 2012 09:35:52 -0700, Ian Kelly wrote: > On Thu, Feb 9, 2012 at 8:19 AM, Nathan Rice > wrote: >> As I said, two dictionaries created from the same input will be the >> same... > > That's an implementation detail, not a guarantee. It will hold for > current versions of CPython but

Re: frozendict

2012-02-09 Thread Duncan Booth
Nathan Rice wrote: > As I said, two dictionaries created from the same input will be the > same... 'ai' != 'ia'. If I need to hash a dict that I don't know was > created in a deterministic order, I'd frozenset(thedict.items()). > Fair enough, the idea scares me, but it's your code and your ris

Re: frozendict

2012-02-09 Thread Nathan Rice
On Thu, Feb 9, 2012 at 11:35 AM, Ian Kelly wrote: > On Thu, Feb 9, 2012 at 8:19 AM, Nathan Rice > wrote: >> As I said, two dictionaries created from the same input will be the >> same... > > That's an implementation detail, not a guarantee.  It will hold for > current versions of CPython but not

Re: frozendict

2012-02-09 Thread Ian Kelly
On Thu, Feb 9, 2012 at 8:19 AM, Nathan Rice wrote: > As I said, two dictionaries created from the same input will be the > same... That's an implementation detail, not a guarantee. It will hold for current versions of CPython but not necessarily for other Python implementations. -- http://mail.

Re: frozendict

2012-02-09 Thread Nathan Rice
>> Two dicts created from the same inputs will return items in the same >> arbitrary order.  As long as you don't insert or delete a key you're >> fine. >> > Two dicts that contain the same keys and values may or may not return them > in the same order: > dict.fromkeys('ia') > {'i': None, 'a':

Re: frozendict

2012-02-09 Thread Duncan Booth
Nathan Rice wrote: > On Thu, Feb 9, 2012 at 5:33 AM, Duncan Booth > wrote: >> Nathan Rice wrote: >> >>> I put dicts in sets all the time.  I just tuple the items, but that >>> means you have to re-dict it on the way out to do anything useful >>> with it.  I am too lazy to write a frozendict or i

Re: frozendict

2012-02-09 Thread Nathan Rice
On Thu, Feb 9, 2012 at 5:33 AM, Duncan Booth wrote: > Nathan Rice wrote: > >> I put dicts in sets all the time.  I just tuple the items, but that >> means you have to re-dict it on the way out to do anything useful with >> it.  I am too lazy to write a frozendict or import one, but I would >> use

Re: frozendict

2012-02-09 Thread Duncan Booth
Nathan Rice wrote: > I put dicts in sets all the time. I just tuple the items, but that > means you have to re-dict it on the way out to do anything useful with > it. I am too lazy to write a frozendict or import one, but I would > use it if it was a builtin. > I hope you sort the items before

Re: Re: frozendict

2012-02-08 Thread Evan Driscoll
On 13:59, Ian Kelly wrote: > > Define "doesn't suck". If I were to hack one up, it would look > something like this: > > > from collections import Mapping, Hashable So part of my objection was that I wanted to make sure I got all of the expected functionality, and that takes a bunch of functions.

Re: Re: frozendict

2012-02-08 Thread Evan Driscoll
On 13:59, Nathan Rice wrote: >> Turn the question around: why should there be? >> Python is intentionally parsimonious in adding builtins. > > For the same reason there are frozensets? > > I put dicts in sets all the time. I just tuple the items, but that > means you have to re-dict it on the wa

Re: frozendict

2012-02-08 Thread Nathan Rice
> Turn the question around: why should there be? > Python is intentionally parsimonious in adding builtins. For the same reason there are frozensets? I put dicts in sets all the time. I just tuple the items, but that means you have to re-dict it on the way out to do anything useful with it. I a

Re: frozendict

2012-02-08 Thread Ian Kelly
On Wed, Feb 8, 2012 at 6:23 PM, Evan Driscoll wrote: > Hi all, > > I've been trying for a few days (only a little bit at a time) to come up > with a way of implementing a frozendict that doesn't suck. I'm gradually > converging to a solution, but I can't help but think that there's some > subtlety

Re: frozendict

2012-02-08 Thread Terry Reedy
On 2/8/2012 8:23 PM, Evan Driscoll wrote: Hi all, I've been trying for a few days (only a little bit at a time) to come up with a way of implementing a frozendict that doesn't suck. I'm gradually converging to a solution, but I can't help but think that there's some subtlety that I'm probably mi

Re: frozendict

2012-02-08 Thread Steven D'Aprano
On Wed, 08 Feb 2012 19:23:19 -0600, Evan Driscoll wrote: > Hi all, > > I've been trying for a few days (only a little bit at a time) to come up > with a way of implementing a frozendict that doesn't suck. I'm gradually > converging to a solution, but I can't help but think that there's some > sub

Re: frozendict (v0.1)

2010-10-09 Thread John Nagle
On 10/7/2010 2:39 PM, kj wrote: Following a suggestion from MRAB, I attempted to implement a frozendict class. That really should be built into the language. "dict" is the last built-in type that doesn't have an immutable form. John Nagle -- http://mail.pyth

Re: frozendict (v0.1)

2010-10-08 Thread Arnaud Delobelle
kj wrote: > In <878w29kxjp@gmail.com> Arnaud Delobelle writes: > > >E.g., try with {1:'a', 1j:'b'} > > I see. Thanks for this clarification. I learned a lot from it. > > I guess that frozenset must have some way of canonicalizing the > order of its elements that is dependent on their Pytho

Re: frozendict (v0.1)

2010-10-08 Thread Steven D'Aprano
On Fri, 08 Oct 2010 14:00:17 +, kj wrote: > In kj writes: > >>At any rate, using your [i.e. Arnaud's] suggestions in this and your >>other post, the current implementation of frozendict stands at: > >>class frozendict(dict): >>for method in ('__delitem__ __setitem__ clear pop popitem'

Re: frozendict (v0.1)

2010-10-08 Thread Steven D'Aprano
On Fri, 08 Oct 2010 12:10:50 +, kj wrote: > In <4cae667c$0$29993$c3e8da3$54964...@news.astraweb.com> Steven D'Aprano > writes: > >>On Fri, 08 Oct 2010 00:23:30 +, kj wrote: > >>Because it's always better to use a well-written, fast, efficient, >>correct, well-tested wheel than to invent

Re: frozendict (v0.1)

2010-10-08 Thread kj
In "Jonas H." writes: >Hope this helps :-) It did! Thanks! For one thing now I see that I was barking up the wrong tree in focusing on a canonical order, when, as the code you posted shows, it is actually not required for hashing. In fact, I'd come to the conclusion that frozensets had a c

Re: frozendict (v0.1)

2010-10-08 Thread kj
In "Jonas H." writes: >On 10/08/2010 02:23 AM, kj wrote: >Here's my implementation suggestion: >class frozendict(dict): > def _immutable_error(self, *args, **kwargs): > raise TypeError("%r object is immutable" % self.__class__.__name__) > __setitem__ = __delitem__ = clear = p

Re: frozendict (v0.1)

2010-10-08 Thread kj
In kj writes: >At any rate, using your [i.e. Arnaud's] suggestions in this and >your other post, the current implementation of frozendict stands >at: >class frozendict(dict): >for method in ('__delitem__ __setitem__ clear pop popitem setdefault ' > 'update').split(): >

Re: frozendict (v0.1)

2010-10-08 Thread Jonas H.
On 10/08/2010 03:27 PM, kj wrote: I tried to understand this by looking at the C source but I gave up after 10 fruitless minutes. (This has been invariably the outcome of all my attempts at finding my way through the Python C source.) It's not you. CPython's code is ... [censored] Anyway, you

Re: frozendict (v0.1)

2010-10-08 Thread kj
In <878w29kxjp@gmail.com> Arnaud Delobelle writes: >E.g., try with {1:'a', 1j:'b'} I see. Thanks for this clarification. I learned a lot from it. I guess that frozenset must have some way of canonicalizing the order of its elements that is dependent on their Python values but not on their

Re: frozendict (v0.1)

2010-10-08 Thread kj
In <4cae667c$0$29993$c3e8da3$54964...@news.astraweb.com> Steven D'Aprano writes: >On Fri, 08 Oct 2010 00:23:30 +, kj wrote: >Because it's always better to use a well-written, fast, efficient, >correct, well-tested wheel than to invent your own slow, incorrect >wheel :) IOW, "don't you wo

Re: frozendict (v0.1)

2010-10-08 Thread Jonas H.
On 10/08/2010 02:23 AM, kj wrote: I imagine that frozenset is better than sorted(tuple(...)) here, but it's not obvious to me why. dicts are unsorted. That means their item-order is undefined. So are sets. If you want a hash that is independent from the order of items, you could ensure the it

Re: frozendict (v0.1)

2010-10-07 Thread Arnaud Delobelle
kj writes: > In <87hbgxlk67@gmail.com> Arnaud Delobelle writes: > >>A simple fix is to use hash(frozenset(self.items())) instead. > > Thanks for pointing out the hash bug. It was an oversight: I meant > to write > > def __hash__(self): > return hash(sorted(tuple(self.items(

Re: frozendict (v0.1)

2010-10-07 Thread Steven D'Aprano
On Fri, 08 Oct 2010 00:23:30 +, kj wrote: > In <87hbgxlk67@gmail.com> Arnaud Delobelle > writes: > >>A simple fix is to use hash(frozenset(self.items())) instead. > > Thanks for pointing out the hash bug. It was an oversight: I meant to > write > > def __hash__(self): > re

Re: frozendict (v0.1)

2010-10-07 Thread kj
In <87hbgxlk67@gmail.com> Arnaud Delobelle writes: >A simple fix is to use hash(frozenset(self.items())) instead. Thanks for pointing out the hash bug. It was an oversight: I meant to write def __hash__(self): return hash(sorted(tuple(self.items( I imagine that frozenset i

Re: frozendict (v0.1)

2010-10-07 Thread Arnaud Delobelle
Oops I sent off my reply before I had finished! kj writes: > Following a suggestion from MRAB, I attempted to implement a > frozendict class. My implementation took a lot more work than > something this simple should take, and it still sucks. So I'm > hoping someone can show me a better way.

Re: frozendict (v0.1)

2010-10-07 Thread Arnaud Delobelle
kj writes: > Following a suggestion from MRAB, I attempted to implement a > frozendict class. My implementation took a lot more work than > something this simple should take, and it still sucks. So I'm > hoping someone can show me a better way. Specifically, I'm hoping > that there is a "recip