Changeset 3536 for trunk/src/grep


Ignore:
Timestamp:
Dec 21, 2021, 12:32:59 AM (4 years ago)
Author:
bird
Message:

grep: Windows console output optimizations.

Location:
trunk/src/grep
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/grep/Makefile.kmk

    r3532 r3536  
    144144       lib/strerror.c \
    145145       \
     146        ../lib/maybe_con_fwrite.c \
     147        ../lib/is_console.c \
    146148        ../lib/nt/ntstat.c \
    147149        ../lib/nt/ntdir.c \
  • trunk/src/grep/config.win.h

    r3532 r3536  
    25872587/* Override initialize_main to do wildcard expansion. */
    25882588#define initialize_main w32_initialize_main
     2589void w32_initialize_main(int *pcArgs, char ***ppapszArgs);
    25892590
    25902591#endif /* !INCLUDED_CONFIG_WIN_H */
  • trunk/src/grep/src/grep.c

    r3532 r3536  
    5757#include "xbinary-io.h"
    5858#include "xstrtol.h"
     59
     60#if defined(KMK_GREP) && defined(KBUILD_OS_WINDOWS)
     61# include "console.h"
     62#endif
    5963
    6064enum { SEP_CHAR_SELECTED = ':' };
     
    439443static int stdout_errno;
    440444
     445#if defined(KMK_GREP) && defined(KBUILD_OS_WINDOWS)
     446# include <assert.h>
     447static void fwrite_errno (void const *, size_t, size_t);
     448#endif
     449
    441450static void
    442451putchar_errno (int c)
    443452{
     453#if defined(KMK_GREP) && defined(KBUILD_OS_WINDOWS)
     454  char ch = (char)c;
     455  fwrite_errno (&ch, 1, 1);
     456#else
    444457  if (putchar (c) < 0)
    445458    stdout_errno = errno;
     459#endif
    446460}
    447461
     
    449463fputs_errno (char const *s)
    450464{
     465#if defined(KMK_GREP) && defined(KBUILD_OS_WINDOWS)
     466  fwrite_errno (s, 1, strlen (s));
     467#else
    451468  if (fputs (s, stdout) < 0)
    452469    stdout_errno = errno;
     470#endif
    453471}
    454472
     
    458476  va_list ap;
    459477  va_start (ap, format);
     478#if defined(KMK_GREP) && defined(KBUILD_OS_WINDOWS)
     479  char szBuf[1024]; /* Only really used for a PRIuMAX number and maybe a newline.  */
     480  int cch = vsnprintf (szBuf, sizeof (szBuf), format, ap);
     481  assert (cch < sizeof(szBuf));
     482  fwrite_errno (szBuf, 1, cch);
     483#else
    460484  if (vfprintf (stdout, format, ap) < 0)
    461485    stdout_errno = errno;
     486#endif
    462487  va_end (ap);
    463488}
     
    466491fwrite_errno (void const *ptr, size_t size, size_t nmemb)
    467492{
     493#if defined(KMK_GREP) && defined(KBUILD_OS_WINDOWS)
     494  /*
     495   * This trick reduces the runtime of 'grep -r GNU .' in the grep source dir
     496   * from just above 11 seconds to just below 1.2 seconds.
     497   * Note! s_is_console is for keeping output to file speedy.
     498   */
     499  static int s_is_console = -1;
     500  if (s_is_console != -1)
     501    { /* likely*/ }
     502  else
     503    s_is_console = is_console (fileno (stdout));
     504  if (  s_is_console
     505      ? maybe_con_fwrite (ptr, size, nmemb, stdout) != nmemb
     506      : fwrite (ptr, size, nmemb, stdout) != nmemb)
     507#else
    468508  if (fwrite (ptr, size, nmemb, stdout) != nmemb)
     509#endif
    469510    stdout_errno = errno;
    470511}
Note: See TracChangeset for help on using the changeset viewer.