/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/chunk_writer.py

  • Committer: John Arbash Meinel
  • Date: 2008-08-25 21:50:11 UTC
  • mfrom: (0.11.3 tools)
  • mto: This revision was merged to the branch mainline in revision 3659.
  • Revision ID: john@arbash-meinel.com-20080825215011-de9esmzgkue3e522
Merge in Lukáš's helper scripts.
Update the packaging documents to describe how to do the releases
using bzr-builddeb to package all distro platforms
simultaneously.

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
#
17
17
 
18
18
"""ChunkWriter: write compressed data out with a fixed upper bound."""
36
36
        will sometimes start over and compress the whole list to get tighter
37
37
        packing. We get diminishing returns after a while, so this limits the
38
38
        number of times we will try.
39
 
        The default is to try to avoid recompressing entirely, but setting this
40
 
        to something like 20 will give maximum compression.
41
 
 
42
 
    :cvar _max_zsync: Another tunable nob. If _max_repack is set to 0, then you
43
 
        can limit the number of times we will try to pack more data into a
44
 
        node. This allows us to do a single compression pass, rather than
45
 
        trying until we overflow, and then recompressing again.
 
39
        In testing, some values for bzr.dev::
 
40
 
 
41
                    w/o copy    w/ copy     w/ copy ins w/ copy & save
 
42
            repack  time  MB    time  MB    time  MB    time  MB
 
43
             1       8.8  5.1    8.9  5.1    9.6  4.4   12.5  4.1
 
44
             2       9.6  4.4   10.1  4.3   10.4  4.2   11.1  4.1
 
45
             3      10.6  4.2   11.1  4.1   11.2  4.1   11.3  4.1
 
46
             4      12.0  4.1
 
47
             5      12.6  4.1
 
48
            20      12.9  4.1   12.2  4.1   12.3  4.1
 
49
 
 
50
        In testing, some values for mysql-unpacked::
 
51
 
 
52
                    w/o copy    w/ copy     w/ copy ins w/ copy & save
 
53
            repack  time  MB    time  MB    time  MB    time  MB
 
54
             1      56.6  16.9              60.7  14.2
 
55
             2      59.3  14.1              62.6  13.5  64.3  13.4
 
56
             3      64.4  13.5
 
57
            20      73.4  13.4
 
58
 
 
59
    :cvar _default_min_compression_size: The expected minimum compression.
 
60
        While packing nodes into the page, we won't Z_SYNC_FLUSH until we have
 
61
        received this much input data. This saves time, because we don't bloat
 
62
        the result with SYNC entries (and then need to repack), but if it is
 
63
        set too high we will accept data that will never fit and trigger a
 
64
        fault later.
46
65
    """
47
 
    #    In testing, some values for bzr.dev::
48
 
    #        repack  time  MB   max   full
49
 
    #         1       7.5  4.6  1140  0
50
 
    #         2       8.4  4.2  1036  1
51
 
    #         3       9.8  4.1  1012  278
52
 
    #         4      10.8  4.1  728   945
53
 
    #        20      11.1  4.1  0     1012
54
 
    #        repack = 0
55
 
    #        zsync   time  MB    repack  stop_for_z
56
 
    #         0       5.0  24.7  0       6270
57
 
    #         1       4.3  13.2  0       3342
58
 
    #         2       4.9   9.6  0       2414
59
 
    #         5       4.8   6.2  0       1549
60
 
    #         6       4.8   5.8  1       1435
61
 
    #         7       4.8   5.5  19      1337
62
 
    #         8       4.4   5.3  81      1220
63
 
    #        10       5.3   5.0  260     967
64
 
    #        11       5.3   4.9  366     839
65
 
    #        12       5.1   4.8  454     731
66
 
    #        15       5.8   4.7  704     450
67
 
    #        20       5.8   4.6  1133    7
68
 
 
69
 
    #    In testing, some values for mysql-unpacked::
70
 
    #                next_bytes estim
71
 
    #        repack  time  MB    full    stop_for_repack
72
 
    #         1            15.4  0       3913
73
 
    #         2      35.4  13.7  0       346
74
 
    #        20      46.7  13.4  3380    0
75
 
    #        repack=0
76
 
    #        zsync                       stop_for_z
77
 
    #         0      29.5 116.5  0       29782
78
 
    #         1      27.8  60.2  0       15356
79
 
    #         2      27.8  42.4  0       10822
80
 
    #         5      26.8  25.5  0       6491
81
 
    #         6      27.3  23.2  13      5896
82
 
    #         7      27.5  21.6  29      5451
83
 
    #         8      27.1  20.3  52      5108
84
 
    #        10      29.4  18.6  195     4526
85
 
    #        11      29.2  18.0  421     4143
86
 
    #        12      28.0  17.5  702     3738
87
 
    #        15      28.9  16.5  1223    2969
88
 
    #        20      29.6  15.7  2182    1810
89
 
    #        30      31.4  15.4  3891    23
90
 
 
91
 
    # Tuple of (num_repack_attempts, num_zsync_attempts)
92
 
    # num_zsync_attempts only has meaning if num_repack_attempts is 0.
93
 
    _repack_opts_for_speed = (0, 8)
94
 
    _repack_opts_for_size = (20, 0)
95
 
 
96
 
    def __init__(self, chunk_size, reserved=0, optimize_for_size=False):
 
66
 
 
67
    _max_repack = 2
 
68
    _default_min_compression_size = 1.8
 
69
 
 
70
    def __init__(self, chunk_size, reserved=0):
97
71
        """Create a ChunkWriter to write chunk_size chunks.
98
72
 
99
73
        :param chunk_size: The total byte count to emit at the end of the
100
74
            chunk.
101
75
        :param reserved: How many bytes to allow for reserved data. reserved
102
 
            data space can only be written to via the write(..., reserved=True).
 
76
            data space can only be written to via the write_reserved method.
103
77
        """
104
78
        self.chunk_size = chunk_size
105
79
        self.compressor = zlib.compressobj()
106
80
        self.bytes_in = []
107
81
        self.bytes_list = []
108
82
        self.bytes_out_len = 0
109
 
        # bytes that have been seen, but not included in a flush to out yet
110
 
        self.unflushed_in_bytes = 0
 
83
        self.compressed = None
 
84
        self.seen_bytes = 0
111
85
        self.num_repack = 0
112
 
        self.num_zsync = 0
113
86
        self.unused_bytes = None
114
87
        self.reserved_size = reserved
115
 
        # Default is to make building fast rather than compact
116
 
        self.set_optimize(for_size=optimize_for_size)
 
88
        self.min_compress_size = self._default_min_compression_size
117
89
 
118
90
    def finish(self):
119
91
        """Finish the chunk.
120
92
 
121
93
        This returns the final compressed chunk, and either None, or the
122
94
        bytes that did not fit in the chunk.
123
 
 
124
 
        :return: (compressed_bytes, unused_bytes, num_nulls_needed)
125
 
            compressed_bytes    a list of bytes that were output from the
126
 
                                compressor. If the compressed length was not
127
 
                                exactly chunk_size, the final string will be a
128
 
                                string of all null bytes to pad this to
129
 
                                chunk_size
130
 
            unused_bytes        None, or the last bytes that were added, which
131
 
                                we could not fit.
132
 
            num_nulls_needed    How many nulls are padded at the end
133
95
        """
134
96
        self.bytes_in = None # Free the data cached so far, we don't need it
135
97
        out = self.compressor.flush(Z_FINISH)
136
98
        self.bytes_list.append(out)
137
99
        self.bytes_out_len += len(out)
138
 
 
139
100
        if self.bytes_out_len > self.chunk_size:
140
101
            raise AssertionError('Somehow we ended up with too much'
141
102
                                 ' compressed data, %d > %d'
142
103
                                 % (self.bytes_out_len, self.chunk_size))
143
 
        nulls_needed = self.chunk_size - self.bytes_out_len
 
104
        nulls_needed = self.chunk_size - self.bytes_out_len % self.chunk_size
144
105
        if nulls_needed:
145
106
            self.bytes_list.append("\x00" * nulls_needed)
146
107
        return self.bytes_list, self.unused_bytes, nulls_needed
147
108
 
148
 
    def set_optimize(self, for_size=True):
149
 
        """Change how we optimize our writes.
150
 
 
151
 
        :param for_size: If True, optimize for minimum space usage, otherwise
152
 
            optimize for fastest writing speed.
153
 
        :return: None
154
 
        """
155
 
        if for_size:
156
 
            opts = ChunkWriter._repack_opts_for_size
157
 
        else:
158
 
            opts = ChunkWriter._repack_opts_for_speed
159
 
        self._max_repack, self._max_zsync = opts
160
 
 
161
109
    def _recompress_all_bytes_in(self, extra_bytes=None):
162
110
        """Recompress the current bytes_in, and optionally more.
163
111
 
164
 
        :param extra_bytes: Optional, if supplied we will add it with
 
112
        :param extra_bytes: Optional, if supplied we will try to add it with
165
113
            Z_SYNC_FLUSH
166
 
        :return: (bytes_out, bytes_out_len, alt_compressed)
 
114
        :return: (bytes_out, compressor, alt_compressed)
167
115
            bytes_out   is the compressed bytes returned from the compressor
168
 
            bytes_out_len the length of the compressed output
169
116
            compressor  An object with everything packed in so far, and
170
117
                        Z_SYNC_FLUSH called.
 
118
            alt_compressed  If the compressor supports copy(), then this is a
 
119
                            snapshot just before extra_bytes is added.
 
120
                            It is (bytes_out, compressor) as well.
 
121
                            The idea is if you find you cannot fit the new
 
122
                            bytes, you don't have to start over.
 
123
                            And if you *can* you don't have to Z_SYNC_FLUSH
 
124
                            yet.
171
125
        """
172
126
        compressor = zlib.compressobj()
173
127
        bytes_out = []
180
134
        if extra_bytes:
181
135
            out = compress(extra_bytes)
182
136
            out += compressor.flush(Z_SYNC_FLUSH)
183
 
            append(out)
 
137
            if out:
 
138
                append(out)
184
139
        bytes_out_len = sum(map(len, bytes_out))
185
140
        return bytes_out, bytes_out_len, compressor
186
141
 
189
144
 
190
145
        If the bytes fit, False is returned. Otherwise True is returned
191
146
        and the bytes have not been added to the chunk.
192
 
 
193
 
        :param bytes: The bytes to include
194
 
        :param reserved: If True, we can use the space reserved in the
195
 
            constructor.
196
147
        """
197
 
        if self.num_repack > self._max_repack and not reserved:
198
 
            self.unused_bytes = bytes
199
 
            return True
200
148
        if reserved:
201
149
            capacity = self.chunk_size
202
150
        else:
203
151
            capacity = self.chunk_size - self.reserved_size
 
152
        # Check quickly to see if this is likely to put us outside of our
 
153
        # budget:
 
154
        next_seen_size = self.seen_bytes + len(bytes)
204
155
        comp = self.compressor
205
 
 
206
 
        # Check to see if the currently unflushed bytes would fit with a bit of
207
 
        # room to spare, assuming no compression.
208
 
        next_unflushed = self.unflushed_in_bytes + len(bytes)
209
 
        remaining_capacity = capacity - self.bytes_out_len - 10
210
 
        if (next_unflushed < remaining_capacity):
211
 
            # looks like it will fit
 
156
        if (next_seen_size < self.min_compress_size * capacity):
 
157
            # No need, we assume this will "just fit"
212
158
            out = comp.compress(bytes)
213
159
            if out:
214
160
                self.bytes_list.append(out)
215
161
                self.bytes_out_len += len(out)
216
162
            self.bytes_in.append(bytes)
217
 
            self.unflushed_in_bytes += len(bytes)
 
163
            self.seen_bytes = next_seen_size
218
164
        else:
 
165
            if self.num_repack >= self._max_repack and not reserved:
 
166
                # We already know we don't want to try to fit more
 
167
                return True
219
168
            # This may or may not fit, try to add it with Z_SYNC_FLUSH
220
 
            # Note: It is tempting to do this as a look-ahead pass, and to
221
 
            #       'copy()' the compressor before flushing. However, it seems
222
 
            #       that Which means that it is the same thing as increasing
223
 
            #       repack, similar cost, same benefit. And this way we still
224
 
            #       have the 'repack' knob that can be adjusted, and not depend
225
 
            #       on a platform-specific 'copy()' function.
226
 
            self.num_zsync += 1
227
 
            if self._max_repack == 0 and self.num_zsync > self._max_zsync:
228
 
                self.num_repack += 1
229
 
                self.unused_bytes = bytes
230
 
                return True
231
169
            out = comp.compress(bytes)
232
170
            out += comp.flush(Z_SYNC_FLUSH)
233
 
            self.unflushed_in_bytes = 0
234
171
            if out:
235
172
                self.bytes_list.append(out)
236
173
                self.bytes_out_len += len(out)
237
 
 
238
 
            # We are a bit extra conservative, because it seems that you *can*
239
 
            # get better compression with Z_SYNC_FLUSH than a full compress. It
240
 
            # is probably very rare, but we were able to trigger it.
241
 
            if self.num_repack == 0:
242
 
                safety_margin = 100
243
 
            else:
244
 
                safety_margin = 10
245
 
            if self.bytes_out_len + safety_margin <= capacity:
246
 
                # It fit, so mark it added
247
 
                self.bytes_in.append(bytes)
248
 
            else:
 
174
            if self.bytes_out_len + 10 > capacity:
249
175
                # We are over budget, try to squeeze this in without any
250
176
                # Z_SYNC_FLUSH calls
251
177
                self.num_repack += 1
252
 
                (bytes_out, this_len,
253
 
                 compressor) = self._recompress_all_bytes_in(bytes)
254
 
                if self.num_repack >= self._max_repack:
255
 
                    # When we get *to* _max_repack, bump over so that the
256
 
                    # earlier > _max_repack will be triggered.
257
 
                    self.num_repack += 1
 
178
                bytes_out, this_len, compressor = self._recompress_all_bytes_in(bytes)
258
179
                if this_len + 10 > capacity:
259
 
                    (bytes_out, this_len,
260
 
                     compressor) = self._recompress_all_bytes_in()
 
180
                    # No way we can add anymore, we need to re-pack because our
 
181
                    # compressor is now out of sync.
 
182
                    # This seems to be rarely triggered over
 
183
                    #   num_repack > _max_repack
 
184
                    bytes_out, this_len, compressor = self._recompress_all_bytes_in()
261
185
                    self.compressor = compressor
262
 
                    # Force us to not allow more data
263
 
                    self.num_repack = self._max_repack + 1
264
186
                    self.bytes_list = bytes_out
265
187
                    self.bytes_out_len = this_len
266
188
                    self.unused_bytes = bytes
267
189
                    return True
268
190
                else:
269
191
                    # This fits when we pack it tighter, so use the new packing
 
192
                    # There is one Z_SYNC_FLUSH call in
 
193
                    # _recompress_all_bytes_in
270
194
                    self.compressor = compressor
271
195
                    self.bytes_in.append(bytes)
272
196
                    self.bytes_list = bytes_out
273
197
                    self.bytes_out_len = this_len
 
198
            else:
 
199
                # It fit, so mark it added
 
200
                self.bytes_in.append(bytes)
 
201
                self.seen_bytes = next_seen_size
274
202
        return False
275
203