Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
ccan
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
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
mirror
ccan
Commits
d615e534
Commit
d615e534
authored
Apr 02, 2019
by
Rusty Russell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
htable: reduce size of htable by storing perfect bitnum, not mask.
Signed-off-by:
Rusty Russell
<
rusty@rustcorp.com.au
>
parent
9e92552b
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
34 additions
and
25 deletions
+34
-25
ccan/htable/htable.c
ccan/htable/htable.c
+22
-12
ccan/htable/htable.h
ccan/htable/htable.h
+2
-3
ccan/htable/test/run-debug.c
ccan/htable/test/run-debug.c
+4
-4
ccan/htable/test/run.c
ccan/htable/test/run.c
+4
-4
ccan/htable/tools/speed.c
ccan/htable/tools/speed.c
+2
-2
No files found.
ccan/htable/htable.c
View file @
d615e534
...
...
@@ -58,6 +58,15 @@ static inline bool entry_is_valid(uintptr_t e)
return
e
>
HTABLE_DELETED
;
}
/* We use 0 to mean we don't have a perfect bit, otherwise it's
* bit n - 1 */
static
inline
uintptr_t
ht_perfect_mask
(
const
struct
htable
*
ht
)
{
if
(
ht
->
perfect_bitnum
>
0
)
return
(
uintptr_t
)
1
<<
(
ht
->
perfect_bitnum
-
1
);
return
0
;
}
static
inline
uintptr_t
get_hash_ptr_bits
(
const
struct
htable
*
ht
,
size_t
hash
)
{
...
...
@@ -65,7 +74,7 @@ static inline uintptr_t get_hash_ptr_bits(const struct htable *ht,
* end is quite expensive. But the lower bits are redundant, so
* we fold the value first. */
return
(
hash
^
(
hash
>>
ht
->
bits
))
&
ht
->
common_mask
&
~
ht
->
perfect_bit
;
&
ht
->
common_mask
&
~
ht
_perfect_mask
(
ht
)
;
}
void
htable_init
(
struct
htable
*
ht
,
...
...
@@ -75,7 +84,7 @@ void htable_init(struct htable *ht,
*
ht
=
empty
;
ht
->
rehash
=
rehash
;
ht
->
priv
=
priv
;
ht
->
table
=
&
ht
->
perfect_bit
;
ht
->
table
=
&
ht
->
common_bits
;
}
static
inline
size_t
ht_max
(
const
struct
htable
*
ht
)
...
...
@@ -102,7 +111,7 @@ bool htable_init_sized(struct htable *ht,
ht
->
table
=
htable_alloc
(
ht
,
sizeof
(
size_t
)
<<
ht
->
bits
);
if
(
!
ht
->
table
)
{
ht
->
table
=
&
ht
->
perfect_bit
;
ht
->
table
=
&
ht
->
common_bits
;
return
false
;
}
(
void
)
htable_debug
(
ht
,
HTABLE_LOC
);
...
...
@@ -111,7 +120,7 @@ bool htable_init_sized(struct htable *ht,
void
htable_clear
(
struct
htable
*
ht
)
{
if
(
ht
->
table
!=
&
ht
->
perfect_bit
)
if
(
ht
->
table
!=
&
ht
->
common_bits
)
htable_free
(
ht
,
(
void
*
)
ht
->
table
);
htable_init
(
ht
,
ht
->
rehash
,
ht
->
priv
);
}
...
...
@@ -154,7 +163,7 @@ void *htable_firstval_(const struct htable *ht,
struct
htable_iter
*
i
,
size_t
hash
)
{
i
->
off
=
hash_bucket
(
ht
,
hash
);
return
htable_val
(
ht
,
i
,
hash
,
ht
->
perfect_bit
);
return
htable_val
(
ht
,
i
,
hash
,
ht
_perfect_mask
(
ht
)
);
}
void
*
htable_nextval_
(
const
struct
htable
*
ht
,
...
...
@@ -197,7 +206,7 @@ void *htable_prev_(const struct htable *ht, struct htable_iter *i)
static
void
ht_add
(
struct
htable
*
ht
,
const
void
*
new
,
size_t
h
)
{
size_t
i
;
uintptr_t
perfect
=
ht
->
perfect_bit
;
uintptr_t
perfect
=
ht
_perfect_mask
(
ht
)
;
i
=
hash_bucket
(
ht
,
h
);
...
...
@@ -223,16 +232,16 @@ static COLD bool double_table(struct htable *ht)
ht
->
bits
++
;
/* If we lost our "perfect bit", get it back now. */
if
(
!
ht
->
perfect_bit
&&
ht
->
common_mask
)
{
if
(
ht
->
perfect_bitnum
==
0
&&
ht
->
common_mask
)
{
for
(
i
=
0
;
i
<
sizeof
(
ht
->
common_mask
)
*
CHAR_BIT
;
i
++
)
{
if
(
ht
->
common_mask
&
((
size_t
)
1
<<
i
))
{
ht
->
perfect_bit
=
(
size_t
)
1
<<
i
;
ht
->
perfect_bit
num
=
i
+
1
;
break
;
}
}
}
if
(
oldtable
!=
&
ht
->
perfect_bit
)
{
if
(
oldtable
!=
&
ht
->
common_bits
)
{
for
(
i
=
0
;
i
<
oldnum
;
i
++
)
{
if
(
entry_is_valid
(
e
=
oldtable
[
i
]))
{
void
*
p
=
get_raw_ptr
(
ht
,
e
);
...
...
@@ -262,7 +271,7 @@ static COLD void rehash_table(struct htable *ht)
continue
;
if
(
e
==
HTABLE_DELETED
)
ht
->
table
[
h
]
=
0
;
else
if
(
!
(
e
&
ht
->
perfect_bit
))
{
else
if
(
!
(
e
&
ht
_perfect_mask
(
ht
)
))
{
void
*
p
=
get_raw_ptr
(
ht
,
e
);
ht
->
table
[
h
]
=
0
;
ht_add
(
ht
,
p
,
ht
->
rehash
(
p
,
ht
->
priv
));
...
...
@@ -290,7 +299,7 @@ static COLD void update_common(struct htable *ht, const void *p)
ht
->
common_mask
=
~
((
uintptr_t
)
1
<<
i
);
ht
->
common_bits
=
((
uintptr_t
)
p
&
ht
->
common_mask
);
ht
->
perfect_bit
=
1
;
ht
->
perfect_bit
num
=
1
;
(
void
)
htable_debug
(
ht
,
HTABLE_LOC
);
return
;
}
...
...
@@ -313,7 +322,8 @@ static COLD void update_common(struct htable *ht, const void *p)
/* Take away those bits from our mask, bits and perfect bit. */
ht
->
common_mask
&=
~
maskdiff
;
ht
->
common_bits
&=
~
maskdiff
;
ht
->
perfect_bit
&=
~
maskdiff
;
if
(
ht_perfect_mask
(
ht
)
&
maskdiff
)
ht
->
perfect_bitnum
=
0
;
(
void
)
htable_debug
(
ht
,
HTABLE_LOC
);
}
...
...
ccan/htable/htable.h
View file @
d615e534
...
...
@@ -24,11 +24,10 @@
struct
htable
{
size_t
(
*
rehash
)(
const
void
*
elem
,
void
*
priv
);
void
*
priv
;
unsigned
int
bits
;
unsigned
int
bits
,
perfect_bitnum
;
size_t
elems
,
deleted
;
/* These are the bits which are the same in all pointers. */
uintptr_t
common_mask
,
common_bits
;
uintptr_t
perfect_bit
;
uintptr_t
*
table
;
};
...
...
@@ -50,7 +49,7 @@ struct htable {
* static struct htable ht = HTABLE_INITIALIZER(ht, rehash, NULL);
*/
#define HTABLE_INITIALIZER(name, rehash, priv) \
{ rehash, priv, 0, 0, 0,
-1, 0, 0, &name.perfect_bit
}
{ rehash, priv, 0, 0, 0,
0, -1, 0, &name.common_bits
}
/**
* htable_init - initialize an empty hash table.
...
...
ccan/htable/test/run-debug.c
View file @
d615e534
...
...
@@ -194,17 +194,17 @@ int main(void)
/* Corner cases: wipe out the perfect bit using bogus pointer. */
htable_clear
(
&
ht
);
htable_add
(
&
ht
,
hash
(
&
val
[
NUM_VALS
-
1
],
NULL
),
&
val
[
NUM_VALS
-
1
]);
ok1
(
ht
.
perfect_bit
);
perfect_bit
=
ht
.
perfect_bit
;
ok1
(
ht
_perfect_mask
(
&
ht
)
);
perfect_bit
=
ht
_perfect_mask
(
&
ht
)
;
bad_pointer
=
(
void
*
)((
uintptr_t
)
&
val
[
NUM_VALS
-
1
]
|
perfect_bit
);
htable_add
(
&
ht
,
0
,
bad_pointer
);
ok1
(
ht
.
perfect_bit
==
0
);
ok1
(
ht
_perfect_mask
(
&
ht
)
==
0
);
htable_del
(
&
ht
,
0
,
bad_pointer
);
/* Enlarging should restore it... */
add_vals
(
&
ht
,
val
,
0
,
NUM_VALS
-
1
);
ok1
(
ht
.
perfect_bit
!=
0
);
ok1
(
ht
_perfect_mask
(
&
ht
)
!=
0
);
htable_clear
(
&
ht
);
ok1
(
htable_init_sized
(
&
ht
,
hash
,
NULL
,
1024
));
...
...
ccan/htable/test/run.c
View file @
d615e534
...
...
@@ -183,17 +183,17 @@ int main(void)
/* Corner cases: wipe out the perfect bit using bogus pointer. */
htable_clear
(
&
ht
);
htable_add
(
&
ht
,
0
,
(
void
*
)((
uintptr_t
)
&
val
[
NUM_VALS
-
1
]));
ok1
(
ht
.
perfect_bit
);
perfect_bit
=
ht
.
perfect_bit
;
ok1
(
ht
_perfect_mask
(
&
ht
)
);
perfect_bit
=
ht
_perfect_mask
(
&
ht
)
;
htable_add
(
&
ht
,
0
,
(
void
*
)((
uintptr_t
)
&
val
[
NUM_VALS
-
1
]
|
perfect_bit
));
ok1
(
ht
.
perfect_bit
==
0
);
ok1
(
ht
_perfect_mask
(
&
ht
)
==
0
);
htable_del
(
&
ht
,
0
,
(
void
*
)((
uintptr_t
)
&
val
[
NUM_VALS
-
1
]
|
perfect_bit
));
/* Enlarging should restore it... */
add_vals
(
&
ht
,
val
,
0
,
NUM_VALS
-
1
);
ok1
(
ht
.
perfect_bit
!=
0
);
ok1
(
ht
_perfect_mask
(
&
ht
)
!=
0
);
htable_clear
(
&
ht
);
ok1
(
htable_init_sized
(
&
ht
,
hash
,
NULL
,
1024
));
...
...
ccan/htable/tools/speed.c
View file @
d615e534
...
...
@@ -74,8 +74,8 @@ static size_t perfect(const struct htable *ht)
continue
;
if
(
hash_bucket
(
ht
,
ht
->
rehash
(
get_raw_ptr
(
ht
,
ht
->
table
[
i
]),
ht
->
priv
))
==
i
)
{
assert
((
ht
->
table
[
i
]
&
ht
->
perfect_bit
)
==
ht
->
perfect_bit
);
assert
((
ht
->
table
[
i
]
&
ht
_perfect_mask
(
ht
)
)
==
ht
_perfect_mask
(
ht
)
);
placed_perfect
++
;
}
}
...
...
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