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

Re: [PATCH] Fix to avoid high memory footprint

From
Jeff King <peff@peff.net>
Date
Jul 17, 2024, 06:16 UTC
Message-ID
<20240717061651.GE547635@coredump.intra.peff.net>
In-Reply-To
<pull.1744.git.git.1721117039874.gitgitgadget@gmail.com>
On Tue, Jul 16, 2024 at 08:03:59AM +0000, Haritha  via GitGitGadget wrote:
Show 20 quoted lines
> From: D Harithamma <harithamma.d@ibm.com>
> 
> This fix avoids high memory footprint when
> adding files that require conversion.
> Git has a trace_encoding routine that prints trace
> output when GIT_TRACE_WORKING_TREE_ENCODING=1 is
> set. This environment variable is used to debug
> the encoding contents.
> When a 40MB file is added, it requests close to
> 1.8GB of storage from xrealloc which can lead
> to out of memory errors.
> However, the check for
> GIT_TRACE_WORKING_TREE_ENCODING is done after
> the string is allocated. This resolves high
> memory footprints even when
> GIT_TRACE_WORKING_TREE_ENCODING is not active.
> This fix adds an early exit to avoid the
> unnecessary memory allocation.
> 
> Signed-off-by: Haritha D <harithamma.d@ibm.com>

Good find. Any trace function should verify that tracing is enabled before doing any substantial work.

Let's take a look at your patch. First, your line wrapping is unusual, making the commit message a bit hard to read. We'd usually shoot for ~72 characters per line. So more like:

Show 10 quoted lines
> This fix avoids high memory footprint when adding files that require
> conversion.  Git has a trace_encoding routine that prints trace output
> when GIT_TRACE_WORKING_TREE_ENCODING=1 is set. This environment
> variable is used to debug the encoding contents.  When a 40MB file is
> added, it requests close to 1.8GB of storage from xrealloc which can
> lead to out of memory errors.  However, the check for
> GIT_TRACE_WORKING_TREE_ENCODING is done after the string is allocated.
> This resolves high memory footprints even when
> GIT_TRACE_WORKING_TREE_ENCODING is not active.  This fix adds an early
> exit to avoid the unnecessary memory allocation.

Second, we'd like a full real name in the Signed-off-by line, as you're agreeing to the DCO. See:

  https://git-scm.com/docs/SubmittingPatches#sign-off

Likewise, the author name should match the signoff name (you can use "git commit --amend --author=..." to fix it).

For the patch itself:
Show 10 quoted lines
> --- a/convert.c
> +++ b/convert.c
> @@ -324,6 +324,11 @@ static void trace_encoding(const char *context, const char *path,
>  	struct strbuf trace = STRBUF_INIT;
>  	int i;
>  
> +	// If tracing is not on, exit early to avoid high memory footprint
> +	if (!trace_pass_fl(&coe)) {
> +		return;
> +	}

I don't think trace_pass_fl() is what you want. It will return true if the trace fd is non-zero (so tracing was requested), but also if the key has not yet been initialized (i.e., nobody has used this key to try printing anything yet).

I think you'd just use trace_want(&coe) instead.
Also, two style nits:
 - our usual style (see Documentation/CodingGuidelines) is to avoid
   braces for one-liners.
 - we only use the /* */ comment form, not //. Though IMHO you could
   skip the comment completely here, as an early-return check in a
   tracing function is pretty obvious.

It would be nice if we could test this, but besides the wasted work, I don't think there's any user-visible behavior (the problem is that we are computing things when we're _not_ tracing, so there's nothing for the user to see). And there's no provision in our test suite for measuring memory usage of a program. So I think we can live without it, and just manually verifying that it works (but it would be good to show the measurements you did manually in the commit message).

-Peff
Previous: Haritha via GitGitGadgetNext: Haritha via GitGitGadget
Message 2 of 15 in “Fix to avoid high memory footprint”
  1. Fix to avoid high memory footprintHaritha via GitGitGadget, Jul 16, 2024
  2. Jeff KingJul 17, 2024
  3. Fix to avoid high memory footprintHaritha via GitGitGadget, Jul 24, 2024
  4. Junio C HamanoJul 24, 2024
  5. Jeff KingJul 24, 2024
  6. Fix to avoid high memory footprintHaritha via GitGitGadget, Jul 26, 2024
  7. Torsten BögershausenJul 26, 2024
  8. convert: avoid high memory footprintHaritha via GitGitGadget, Jul 26, 2024
  9. convert: return early when not tracingHaritha via GitGitGadget, Jul 30, 2024
  10. Junio C HamanoJul 31, 2024
  11. Haritha DJul 31, 2024
  12. convert: return early when not tracingHaritha via GitGitGadget, Jul 31, 2024
  13. Junio C HamanoJul 26, 2024
  14. Junio C HamanoJul 26, 2024
  15. Haritha DJul 30, 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.