summaryrefslogtreecommitdiff
path: root/bugs/struct_calcsize_vs_len_struct_pack.mdwn
blob: 6f77c4cea644c673fc954e121598f9f019bade40 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
A small performance improvement on startup may be possible by replacing len(key('',0,0)) with struct.calcsize(self.fmt):

    python -m timeit -s \
      'import struct; checksum_length = 32; fmt = "!%dsQQ" % checksum_length'\
      'struct.calcsize(fmt)'
    1000000 loops, best of 3: 0.201 usec per loop

vs

    python -m timeit -s \
      'import struct; checksum_length = 32; fmt = "!%dsQQ" % checksum_length'\
      'len(struct.pack(fmt,"",0,0))'
    1000000 loops, best of 3: 0.753 usec per loop

I can't attach files, so I can't attach the bundle I think you prefer, but here's the diff, and I'll email you the bundle as an attachment.

    === modified file 'obnamlib/checksumtree.py'
    --- obnamlib/checksumtree.py        2011-07-26 13:23:55 +0000
    +++ obnamlib/checksumtree.py        2013-02-08 07:09:22 +0000
    @@ -33,7 +33,7 @@
                      upload_queue_size, lru_size, hooks):
             tracing.trace('new ChecksumTree name=%s' % name)
             self.fmt = '!%dsQQ' % checksum_length
    -        key_bytes = len(self.key('', 0, 0))
    +        key_bytes = struct.calcsize(self.fmt)
             obnamlib.RepositoryTree.__init__(self, fs, name, key_bytes, node_size, 
                                              upload_queue_size, lru_size, hooks)
             self.keep_just_one_tree = True
    
    === modified file 'obnamlib/chunklist.py'
    --- obnamlib/chunklist.py   2011-07-26 13:23:55 +0000
    +++ obnamlib/chunklist.py   2013-02-08 07:09:22 +0000
    @@ -35,14 +35,15 @@
     
         def __init__(self, fs, node_size, upload_queue_size, lru_size, hooks):
             tracing.trace('new ChunkList')
    -        self.key_bytes = len(self.key(0))
    +        self.fmt = '!Q'
    +        self.key_bytes = struct.calcsize(self.fmt)
             obnamlib.RepositoryTree.__init__(self, fs, 'chunklist', self.key_bytes, 
                                              node_size, upload_queue_size, 
                                              lru_size, hooks)
             self.keep_just_one_tree = True
     
         def key(self, chunk_id):
    -        return struct.pack('!Q', chunk_id)
    +        return struct.pack(self.fmt, chunk_id)
     
         def add(self, chunk_id, checksum):
             tracing.trace('chunk_id=%s', chunk_id)
    
    === modified file 'obnamlib/clientlist.py'
    --- obnamlib/clientlist.py  2011-07-26 13:23:55 +0000
    +++ obnamlib/clientlist.py  2013-02-08 07:09:22 +0000
    @@ -49,7 +49,7 @@
             tracing.trace('new ClientList')
             self.hash_len = len(self.hashfunc(''))
             self.fmt = '!%dsQB' % self.hash_len
    -        self.key_bytes = len(self.key('', 0, 0))
    +        self.key_bytes = struct.calcsize(self.fmt)
             self.minkey = self.hashkey('\x00' * self.hash_len, 0, 0)
             self.maxkey = self.hashkey('\xff' * self.hash_len, obnamlib.MAX_ID, 
                                        self.SUBKEY_MAX)


---

Thank you. This has been merged now. [[done]] --liw