git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 7/9] reftable/block: reuse zstream when writing log blocks

From
Junio C Hamano <gitster@pobox.com>
Date
Apr 3, 2024, 19:35 UTC
Message-ID
<xmqqplv65jet.fsf@gitster.g>
In-Reply-To
<86dab54dfe4501dfa5e50e5a01513c890b62bb4d.1712078736.git.ps@pks.im>
Patrick Steinhardt <ps@pks.im> writes:
Show 25 quoted lines
> @@ -139,39 +143,60 @@ int block_writer_finish(struct block_writer *w)
>  	w->next += 2;
>  	put_be24(w->buf + 1 + w->header_off, w->next);
>  
> +	/*
> +	 * Log records are stored zlib-compressed. Note that the compression
> +	 * also spans over the restart points we have just written.
> +	 */
>  	if (block_writer_type(w) == BLOCK_TYPE_LOG) {
>  		int block_header_skip = 4 + w->header_off;
> +		uLongf src_len = w->next - block_header_skip, compressed_len;
> +		unsigned char *compressed;
> +		int ret;
> +
> +		ret = deflateReset(w->zstream);
> +		if (ret != Z_OK)
> +			return REFTABLE_ZLIB_ERROR;
> +
> +		/*
> +		 * Precompute the upper bound of how many bytes the compressed
> +		 * data may end up with. Combined with `Z_FINISH`, `deflate()`
> +		 * is guaranteed to return `Z_STREAM_END`.
> +		 */
> +		compressed_len = deflateBound(w->zstream, src_len);
> +		REFTABLE_ALLOC_ARRAY(compressed, compressed_len);
OK.
Show 14 quoted lines
> +		w->zstream->next_out = compressed;
> +		w->zstream->avail_out = compressed_len;
> +		w->zstream->next_in = w->buf + block_header_skip;
> +		w->zstream->avail_in = src_len;
> +
> +		/*
> +		 * We want to perform all decompression in a single
> +		 * step, which is why we can pass Z_FINISH here. Note
> +		 * that both `Z_OK` and `Z_BUF_ERROR` indicate that we
> +		 * need to retry according to documentation.
> +		 *
> +		 * If the call fails we retry with a bigger output
> +		 * buffer.
> +		 */
I am not sure where the retry is happening, though.

block_writer_finish() is called by writer_flush_nonempty_block() which returns a negative return to its caller, which is writer_flush_block(). writer_flush_block() in turn returns a negative return to its callers from writer_add_record(), write_finish_section(), and write_object_record(). Nobody seems to react to REFTABLE_ZLIB_ERROR (other than the reftable/error.c that stringifies the error for messages).

But we have asked deflateBound() so if we did not get Z_STREAM_END, wouldn't it mean some data corruption that retrying would not help?

Show 21 quoted lines
> +		ret = deflate(w->zstream, Z_FINISH);
> +		if (ret != Z_STREAM_END) {
>  			reftable_free(compressed);
> -			break;
> +			return REFTABLE_ZLIB_ERROR;
>  		}
> +
> +		/*
> +		 * Overwrite the uncompressed data we have already written and
> +		 * adjust the `next` pointer to point right after the
> +		 * compressed data.
> +		 */
> +		memcpy(w->buf + block_header_skip, compressed,
> +		       w->zstream->total_out);
> +		w->next = w->zstream->total_out + block_header_skip;
> +
> +		reftable_free(compressed);
>  	}
> +
>  	return w->next;
>  }
OK.
Show 33 quoted lines
> @@ -425,6 +450,8 @@ int block_reader_seek(struct block_reader *br, struct block_iter *it,
>  
>  void block_writer_release(struct block_writer *bw)
>  {
> +	deflateEnd(bw->zstream);
> +	FREE_AND_NULL(bw->zstream);
>  	FREE_AND_NULL(bw->restarts);
>  	strbuf_release(&bw->last_key);
>  	/* the block is not owned. */
> diff --git a/reftable/block.h b/reftable/block.h
> index 47acc62c0a..1375957fc8 100644
> --- a/reftable/block.h
> +++ b/reftable/block.h
> @@ -18,6 +18,7 @@ license that can be found in the LICENSE file or at
>   * allocation overhead.
>   */
>  struct block_writer {
> +	z_stream *zstream;
>  	uint8_t *buf;
>  	uint32_t block_size;
>  
> diff --git a/reftable/writer.c b/reftable/writer.c
> index d347ec4cc6..51e663bb19 100644
> --- a/reftable/writer.c
> +++ b/reftable/writer.c
> @@ -153,6 +153,10 @@ void reftable_writer_free(struct reftable_writer *w)
>  {
>  	if (!w)
>  		return;
> +	if (w->block_writer) {
> +		block_writer_release(w->block_writer);
> +		w->block_writer = NULL;
> +	}
This smells like an orthogonal fix to an unrelated resource leakage?
>  	reftable_free(w->block);
>  	reftable_free(w);
>  }
Thanks.
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 12 of 49 in “reftable: optimize write performance”
  1. 0/9 reftable: optimize write performancePatrick Steinhardt, Apr 2, 2024
  2. 1/9 refs/reftable: fix D/F conflict error message on ref copyPatrick Steinhardt, Apr 2, 2024
  3. Junio C HamanoApr 3, 2024
  4. 2/9 refs/reftable: perform explicit D/F check when writing symrefsPatrick Steinhardt, Apr 2, 2024
  5. 3/9 refs/reftable: skip duplicate name checksPatrick Steinhardt, Apr 2, 2024
  6. 4/9 refs/reftable: don't recompute committer identPatrick Steinhardt, Apr 2, 2024
  7. Junio C HamanoApr 3, 2024
  8. Patrick SteinhardtApr 4, 2024
  9. 5/9 reftable/writer: refactorings for `writer_add_record()`Patrick Steinhardt, Apr 2, 2024
  10. 6/9 reftable/writer: refactorings for `writer_flush_nonempty_block()`Patrick Steinhardt, Apr 2, 2024
  11. 7/9 reftable/block: reuse zstream when writing log blocksPatrick Steinhardt, Apr 2, 2024
  12. Junio C HamanoApr 3, 2024
  13. Patrick SteinhardtApr 4, 2024
  14. 8/9 reftable/block: reuse compressed arrayPatrick Steinhardt, Apr 2, 2024
  15. 9/9 reftable/writer: reset `last_key` instead of releasing itPatrick Steinhardt, Apr 2, 2024
  16. 00/11 reftable: optimize write performancePatrick Steinhardt, Apr 4, 2024
  17. 01/11 refs/reftable: fix D/F conflict error message on ref copyPatrick Steinhardt, Apr 4, 2024
  18. 02/11 refs/reftable: perform explicit D/F check when writing symrefsPatrick Steinhardt, Apr 4, 2024
  19. 03/11 refs/reftable: skip duplicate name checksPatrick Steinhardt, Apr 4, 2024
  20. 04/11 reftable: remove name checksPatrick Steinhardt, Apr 4, 2024
  21. 05/11 refs/reftable: don't recompute committer identPatrick Steinhardt, Apr 4, 2024
  22. 06/11 reftable/writer: refactorings for `writer_add_record()`Patrick Steinhardt, Apr 4, 2024
  23. Han-Wen NienhuysApr 4, 2024
  24. Patrick SteinhardtApr 4, 2024
  25. 07/11 reftable/writer: refactorings for `writer_flush_nonempty_block()`Patrick Steinhardt, Apr 4, 2024
  26. 08/11 reftable/writer: unify releasing memoryPatrick Steinhardt, Apr 4, 2024
  27. Han-Wen NienhuysApr 4, 2024
  28. Patrick SteinhardtApr 4, 2024
  29. Han-Wen NienhuysApr 4, 2024
  30. Patrick SteinhardtApr 4, 2024
  31. 09/11 reftable/writer: reset `last_key` instead of releasing itPatrick Steinhardt, Apr 4, 2024
  32. 10/11 reftable/block: reuse zstream when writing log blocksPatrick Steinhardt, Apr 4, 2024
  33. 11/11 reftable/block: reuse compressed arrayPatrick Steinhardt, Apr 4, 2024
  34. Han-Wen NienhuysApr 4, 2024
  35. Patrick SteinhardtApr 4, 2024
  36. 00/11 reftable: optimize write performancePatrick Steinhardt, Apr 8, 2024
  37. 01/11 refs/reftable: fix D/F conflict error message on ref copyPatrick Steinhardt, Apr 8, 2024
  38. 02/11 refs/reftable: perform explicit D/F check when writing symrefsPatrick Steinhardt, Apr 8, 2024
  39. 03/11 refs/reftable: skip duplicate name checksPatrick Steinhardt, Apr 8, 2024
  40. 04/11 reftable: remove name checksPatrick Steinhardt, Apr 8, 2024
  41. 05/11 refs/reftable: don't recompute committer identPatrick Steinhardt, Apr 8, 2024
  42. 06/11 reftable/writer: refactorings for `writer_add_record()`Patrick Steinhardt, Apr 8, 2024
  43. 07/11 reftable/writer: refactorings for `writer_flush_nonempty_block()`Patrick Steinhardt, Apr 8, 2024
  44. 08/11 reftable/writer: unify releasing memoryPatrick Steinhardt, Apr 8, 2024
  45. 09/11 reftable/writer: reset `last_key` instead of releasing itPatrick Steinhardt, Apr 8, 2024
  46. 10/11 reftable/block: reuse zstream when writing log blocksPatrick Steinhardt, Apr 8, 2024
  47. 11/11 reftable/block: reuse compressed arrayPatrick Steinhardt, Apr 8, 2024
  48. Junio C HamanoApr 9, 2024
  49. Patrick SteinhardtApr 9, 2024

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.