Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
d6fc72a5
Commit
d6fc72a5
authored
Feb 19, 2007
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
parent
f7ccc101
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
73 additions
and
7 deletions
+73
-7
Include/dictobject.h
Include/dictobject.h
+3
-0
Lib/test/test_set.py
Lib/test/test_set.py
+20
-0
Objects/dictobject.c
Objects/dictobject.c
+39
-0
Objects/setobject.c
Objects/setobject.c
+11
-7
No files found.
Include/dictobject.h
View file @
d6fc72a5
...
@@ -100,12 +100,15 @@ PyAPI_FUNC(int) PyDict_DelItem(PyObject *mp, PyObject *key);
...
@@ -100,12 +100,15 @@ PyAPI_FUNC(int) PyDict_DelItem(PyObject *mp, PyObject *key);
PyAPI_FUNC
(
void
)
PyDict_Clear
(
PyObject
*
mp
);
PyAPI_FUNC
(
void
)
PyDict_Clear
(
PyObject
*
mp
);
PyAPI_FUNC
(
int
)
PyDict_Next
(
PyAPI_FUNC
(
int
)
PyDict_Next
(
PyObject
*
mp
,
Py_ssize_t
*
pos
,
PyObject
**
key
,
PyObject
**
value
);
PyObject
*
mp
,
Py_ssize_t
*
pos
,
PyObject
**
key
,
PyObject
**
value
);
PyAPI_FUNC
(
int
)
_PyDict_Next
(
PyObject
*
mp
,
Py_ssize_t
*
pos
,
PyObject
**
key
,
PyObject
**
value
,
long
*
hash
);
PyAPI_FUNC
(
PyObject
*
)
PyDict_Keys
(
PyObject
*
mp
);
PyAPI_FUNC
(
PyObject
*
)
PyDict_Keys
(
PyObject
*
mp
);
PyAPI_FUNC
(
PyObject
*
)
PyDict_Values
(
PyObject
*
mp
);
PyAPI_FUNC
(
PyObject
*
)
PyDict_Values
(
PyObject
*
mp
);
PyAPI_FUNC
(
PyObject
*
)
PyDict_Items
(
PyObject
*
mp
);
PyAPI_FUNC
(
PyObject
*
)
PyDict_Items
(
PyObject
*
mp
);
PyAPI_FUNC
(
Py_ssize_t
)
PyDict_Size
(
PyObject
*
mp
);
PyAPI_FUNC
(
Py_ssize_t
)
PyDict_Size
(
PyObject
*
mp
);
PyAPI_FUNC
(
PyObject
*
)
PyDict_Copy
(
PyObject
*
mp
);
PyAPI_FUNC
(
PyObject
*
)
PyDict_Copy
(
PyObject
*
mp
);
PyAPI_FUNC
(
int
)
PyDict_Contains
(
PyObject
*
mp
,
PyObject
*
key
);
PyAPI_FUNC
(
int
)
PyDict_Contains
(
PyObject
*
mp
,
PyObject
*
key
);
PyAPI_FUNC
(
int
)
_PyDict_Contains
(
PyObject
*
mp
,
PyObject
*
key
,
long
hash
);
/* PyDict_Update(mp, other) is equivalent to PyDict_Merge(mp, other, 1). */
/* PyDict_Update(mp, other) is equivalent to PyDict_Merge(mp, other, 1). */
PyAPI_FUNC
(
int
)
PyDict_Update
(
PyObject
*
mp
,
PyObject
*
other
);
PyAPI_FUNC
(
int
)
PyDict_Update
(
PyObject
*
mp
,
PyObject
*
other
);
...
...
Lib/test/test_set.py
View file @
d6fc72a5
...
@@ -26,6 +26,14 @@ class ReprWrapper:
...
@@ -26,6 +26,14 @@ class ReprWrapper:
def
__repr__
(
self
):
def
__repr__
(
self
):
return
repr
(
self
.
value
)
return
repr
(
self
.
value
)
class
HashCountingInt
(
int
):
'int-like object that counts the number of times __hash__ is called'
def
__init__
(
self
,
*
args
):
self
.
hash_count
=
0
def
__hash__
(
self
):
self
.
hash_count
+=
1
return
int
.
__hash__
(
self
)
class
TestJointOps
(
unittest
.
TestCase
):
class
TestJointOps
(
unittest
.
TestCase
):
# Tests common to both set and frozenset
# Tests common to both set and frozenset
...
@@ -270,6 +278,18 @@ class TestJointOps(unittest.TestCase):
...
@@ -270,6 +278,18 @@ class TestJointOps(unittest.TestCase):
fo
.
close
()
fo
.
close
()
os
.
remove
(
test_support
.
TESTFN
)
os
.
remove
(
test_support
.
TESTFN
)
def
test_do_not_rehash_dict_keys
(
self
):
n
=
10
d
=
dict
.
fromkeys
(
map
(
HashCountingInt
,
xrange
(
n
)))
self
.
assertEqual
(
sum
(
elem
.
hash_count
for
elem
in
d
),
n
)
s
=
self
.
thetype
(
d
)
self
.
assertEqual
(
sum
(
elem
.
hash_count
for
elem
in
d
),
n
)
s
.
difference
(
d
)
self
.
assertEqual
(
sum
(
elem
.
hash_count
for
elem
in
d
),
n
)
if
hasattr
(
s
,
'symmetric_difference_update'
):
s
.
symmetric_difference_update
(
d
)
self
.
assertEqual
(
sum
(
elem
.
hash_count
for
elem
in
d
),
n
)
class
TestSet
(
TestJointOps
):
class
TestSet
(
TestJointOps
):
thetype
=
set
thetype
=
set
...
...
Objects/dictobject.c
View file @
d6fc72a5
...
@@ -803,6 +803,34 @@ PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
...
@@ -803,6 +803,34 @@ PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
return
1
;
return
1
;
}
}
/* Internal version of PyDict_Next that returns a hash value in addition to the key and value.*/
int
_PyDict_Next
(
PyObject
*
op
,
Py_ssize_t
*
ppos
,
PyObject
**
pkey
,
PyObject
**
pvalue
,
long
*
phash
)
{
register
Py_ssize_t
i
;
register
Py_ssize_t
mask
;
register
dictentry
*
ep
;
if
(
!
PyDict_Check
(
op
))
return
0
;
i
=
*
ppos
;
if
(
i
<
0
)
return
0
;
ep
=
((
dictobject
*
)
op
)
->
ma_table
;
mask
=
((
dictobject
*
)
op
)
->
ma_mask
;
while
(
i
<=
mask
&&
ep
[
i
].
me_value
==
NULL
)
i
++
;
*
ppos
=
i
+
1
;
if
(
i
>
mask
)
return
0
;
*
phash
=
(
long
)(
ep
[
i
].
me_hash
);
if
(
pkey
)
*
pkey
=
ep
[
i
].
me_key
;
if
(
pvalue
)
*
pvalue
=
ep
[
i
].
me_value
;
return
1
;
}
/* Methods */
/* Methods */
static
void
static
void
...
@@ -1987,6 +2015,17 @@ PyDict_Contains(PyObject *op, PyObject *key)
...
@@ -1987,6 +2015,17 @@ PyDict_Contains(PyObject *op, PyObject *key)
return
ep
==
NULL
?
-
1
:
(
ep
->
me_value
!=
NULL
);
return
ep
==
NULL
?
-
1
:
(
ep
->
me_value
!=
NULL
);
}
}
/* Internal version of PyDict_Contains used when the hash value is already known */
int
_PyDict_Contains
(
PyObject
*
op
,
PyObject
*
key
,
long
hash
)
{
dictobject
*
mp
=
(
dictobject
*
)
op
;
dictentry
*
ep
;
ep
=
(
mp
->
ma_lookup
)(
mp
,
key
,
hash
);
return
ep
==
NULL
?
-
1
:
(
ep
->
me_value
!=
NULL
);
}
/* Hack to implement "key in dict" */
/* Hack to implement "key in dict" */
static
PySequenceMethods
dict_as_sequence
=
{
static
PySequenceMethods
dict_as_sequence
=
{
0
,
/* sq_length */
0
,
/* sq_length */
...
...
Objects/setobject.c
View file @
d6fc72a5
...
@@ -918,8 +918,14 @@ set_update_internal(PySetObject *so, PyObject *other)
...
@@ -918,8 +918,14 @@ set_update_internal(PySetObject *so, PyObject *other)
if
(
PyDict_CheckExact
(
other
))
{
if
(
PyDict_CheckExact
(
other
))
{
PyObject
*
value
;
PyObject
*
value
;
Py_ssize_t
pos
=
0
;
Py_ssize_t
pos
=
0
;
while
(
PyDict_Next
(
other
,
&
pos
,
&
key
,
&
value
))
{
long
hash
;
if
(
set_add_key
(
so
,
key
)
==
-
1
)
while
(
_PyDict_Next
(
other
,
&
pos
,
&
key
,
&
value
,
&
hash
))
{
setentry
an_entry
;
an_entry
.
hash
=
hash
;
an_entry
.
key
=
key
;
if
(
set_add_entry
(
so
,
&
an_entry
)
==
-
1
)
return
-
1
;
return
-
1
;
}
}
return
0
;
return
0
;
...
@@ -1382,7 +1388,7 @@ set_difference(PySetObject *so, PyObject *other)
...
@@ -1382,7 +1388,7 @@ set_difference(PySetObject *so, PyObject *other)
setentry
entrycopy
;
setentry
entrycopy
;
entrycopy
.
hash
=
entry
->
hash
;
entrycopy
.
hash
=
entry
->
hash
;
entrycopy
.
key
=
entry
->
key
;
entrycopy
.
key
=
entry
->
key
;
if
(
!
PyDict_Contains
(
other
,
entry
->
key
))
{
if
(
!
_PyDict_Contains
(
other
,
entry
->
key
,
entry
->
hash
))
{
if
(
set_add_entry
((
PySetObject
*
)
result
,
&
entrycopy
)
==
-
1
)
{
if
(
set_add_entry
((
PySetObject
*
)
result
,
&
entrycopy
)
==
-
1
)
{
Py_DECREF
(
result
);
Py_DECREF
(
result
);
return
NULL
;
return
NULL
;
...
@@ -1453,12 +1459,10 @@ set_symmetric_difference_update(PySetObject *so, PyObject *other)
...
@@ -1453,12 +1459,10 @@ set_symmetric_difference_update(PySetObject *so, PyObject *other)
if
(
PyDict_CheckExact
(
other
))
{
if
(
PyDict_CheckExact
(
other
))
{
PyObject
*
value
;
PyObject
*
value
;
int
rv
;
int
rv
;
while
(
PyDict_Next
(
other
,
&
pos
,
&
key
,
&
value
))
{
long
hash
;
while
(
_PyDict_Next
(
other
,
&
pos
,
&
key
,
&
value
,
&
hash
))
{
setentry
an_entry
;
setentry
an_entry
;
long
hash
=
PyObject_Hash
(
key
);
if
(
hash
==
-
1
)
return
NULL
;
an_entry
.
hash
=
hash
;
an_entry
.
hash
=
hash
;
an_entry
.
key
=
key
;
an_entry
.
key
=
key
;
rv
=
set_discard_entry
(
so
,
&
an_entry
);
rv
=
set_discard_entry
(
so
,
&
an_entry
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment