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

[PATCH 5/5] diff_filespec: use only 2 bits for is_binary flag

From
Jeff King <peff@peff.net>
Date
Jan 17, 2014, 01:25 UTC
Message-ID
<20140117012540.GE7249@sigill.intra.peff.net>
In-Reply-To
<20140117011844.GA6870@sigill.intra.peff.net>

The is_binary flag needs only three values: -1, 0, and 1. However, we use a whole 32-bit int for it on most systems (both 32- and 64- bit).

Instead, we can mark it to use only 2 bits. On 32-bit systems, this lets it end up as part of the bitfield above (saving 4 bytes). On 64-bit systems, we don't see any change (because the savings end up as padding), but it does leave room for another "free" 32-bit value to be added later.

Signed-off-by: Jeff King <peff@peff.net>
---
I don't typically use bit-sized integers like this for anything but
unsigned integers to be used as flags. My understanding is that using
signed integers is explicitly permitted by the standard. I don't know if
we're guaranteed a 2's-complement representation, but I can't imagine an
implementation providing any range besides -2..1, which is what we need.
 diffcore.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/diffcore.h b/diffcore.h
index d911bf0..79de8cf 100644
--- a/diffcore.h
+++ b/diffcore.h
@@ -46,7 +46,7 @@ struct diff_filespec {
 	unsigned is_stdin : 1;
 	unsigned has_more_entries : 1; /* only appear in combined diff */
 	/* data should be considered "binary"; -1 means "don't know yet" */
-	int is_binary;
+	int is_binary : 2;
 	struct userdiff_driver *driver;
 };
 
-- 
1.8.5.2.500.g8060133
Previous: Jeff KingNext: Junio C Hamano
Message 9 of 10 in “diff_filespec cleanups and optimizations”
  1. 0/5 diff_filespec cleanups and optimizationsJeff King, Jan 17, 2014
  2. 1/5 diff_filespec: reorder dirty_submodule macro definitionsJeff King, Jan 17, 2014
  3. Junio C HamanoJan 17, 2014
  4. Jeff KingJan 17, 2014
  5. Junio C HamanoJan 17, 2014
  6. 2/5 diff_filespec: drop funcname_pattern_ident fieldJeff King, Jan 17, 2014
  7. 3/5 diff_filespec: drop xfrm_flags fieldJeff King, Jan 17, 2014
  8. 4/5 diff_filespec: reorder is_binary fieldJeff King, Jan 17, 2014
  9. 5/5 diff_filespec: use only 2 bits for is_binary flagJeff King, Jan 17, 2014
  10. Junio C HamanoJan 17, 2014

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.