Skip to content

Commit

Permalink
Define __slots__ so cut down the memory usage.
Browse files Browse the repository at this point in the history
  • Loading branch information
Leechael committed Nov 28, 2016
1 parent 1ca31b2 commit 61b616d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
4 changes: 3 additions & 1 deletion cuckoofilter/bucket.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import random


class Bucket:
class Bucket(object):
'''Bucket class for storing fingerprints.'''

__slots__ = ("size", "b")

def __init__(self, size=4):
'''
Initialize bucket.
Expand Down
11 changes: 10 additions & 1 deletion cuckoofilter/cuckoofilter.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class StreamValueError(CuckooFilterError):
pass


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

__slots__ = (
'capacity',
'fingerprint_size',
'max_kicks',
'buckets',
'size',
'bucket_size',
)

def __init__(self, capacity, fingerprint_size, bucket_size=4, max_kicks=500):
'''
Initialize Cuckoo filter parameters.
Expand Down
6 changes: 5 additions & 1 deletion example.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import cuckoofilter
import gzip

if __name__ == '__main__':
def main():
total_items = 100000
cf = cuckoofilter.CuckooFilter(total_items, 2)

Expand All @@ -29,3 +29,7 @@
print('False positive rate is {:%}'.format(false_queries / total_queries))
print("size after serialize: {:}".format(len(serialized)))
print("size after serialize + gzip: {:}".format(len(gzip.compress(serialized))))


if __name__ == '__main__':
main()

0 comments on commit 61b616d

Please sign in to comment.