796-vc-mtime-less-read.patch 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. From 60cd34886c2c9f509974239fcf64a61f9a507d14 Mon Sep 17 00:00:00 2001
  2. From: Bruno Haible <[email protected]>
  3. Date: Tue, 25 Feb 2025 09:04:28 +0100
  4. Subject: [PATCH] vc-mtime: Reduce number of read() system calls.
  5. * lib/vc-mtime.c: Include <stddef.h>.
  6. (git_vc_controlled): Read bytes into a buffer, not one-by-one.
  7. ---
  8. ChangeLog | 6 ++++++
  9. lib/vc-mtime.c | 15 +++++++++++----
  10. 2 files changed, 17 insertions(+), 4 deletions(-)
  11. --- a/lib/vc-mtime.c
  12. +++ b/lib/vc-mtime.c
  13. @@ -21,6 +21,7 @@
  14. /* Specification. */
  15. #include "vc-mtime.h"
  16. +#include <stddef.h>
  17. #include <stdlib.h>
  18. #include <unistd.h>
  19. @@ -56,11 +57,17 @@ git_vc_controlled (const char *filename)
  20. return false;
  21. /* Read the subprocess output, and test whether it is non-empty. */
  22. - size_t count = 0;
  23. - char c;
  24. + ptrdiff_t count = 0;
  25. - while (safe_read (fd[0], &c, 1) > 0)
  26. - count++;
  27. + for (;;)
  28. + {
  29. + char buf[1024];
  30. + ptrdiff_t n = safe_read (fd[0], buf, sizeof (buf));
  31. + if (n > 0)
  32. + count += n;
  33. + else
  34. + break;
  35. + }
  36. close (fd[0]);