Skip to content

Commit 61b616d

Browse files
committed
Define __slots__ so cut down the memory usage.
1 parent 1ca31b2 commit 61b616d

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

cuckoofilter/bucket.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import random
22

33

4-
class Bucket:
4+
class Bucket(object):
55
'''Bucket class for storing fingerprints.'''
66

7+
__slots__ = ("size", "b")
8+
79
def __init__(self, size=4):
810
'''
911
Initialize bucket.

cuckoofilter/cuckoofilter.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class StreamValueError(CuckooFilterError):
1616
pass
1717

1818

19-
class CuckooFilter:
19+
class CuckooFilter(object):
2020
'''
2121
A Cuckoo filter is a data structure for probablistic set-membership queries.
2222
We can insert items into the filter and, with a very low false positive probability (FPP), ask whether it contains an item or not.
@@ -30,6 +30,15 @@ class CuckooFilter:
3030
Their reference implementation in C++ can be found here: https://github.com/efficient/cuckoofilter
3131
'''
3232

33+
__slots__ = (
34+
'capacity',
35+
'fingerprint_size',
36+
'max_kicks',
37+
'buckets',
38+
'size',
39+
'bucket_size',
40+
)
41+
3342
def __init__(self, capacity, fingerprint_size, bucket_size=4, max_kicks=500):
3443
'''
3544
Initialize Cuckoo filter parameters.

example.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import cuckoofilter
66
import gzip
77

8-
if __name__ == '__main__':
8+
def main():
99
total_items = 100000
1010
cf = cuckoofilter.CuckooFilter(total_items, 2)
1111

@@ -29,3 +29,7 @@
2929
print('False positive rate is {:%}'.format(false_queries / total_queries))
3030
print("size after serialize: {:}".format(len(serialized)))
3131
print("size after serialize + gzip: {:}".format(len(gzip.compress(serialized))))
32+
33+
34+
if __name__ == '__main__':
35+
main()

0 commit comments

Comments
 (0)