bash: Don't load user32.dll for every shell process
[msysgit.git] / src / rt / patches / 0012-bash-Don-t-load-user32.dll-for-every-shell-process.patch
blob2c682784942b6b4894ded43963369a5206df8ce6
1 From c8ec70daf6c29525077ecd691224600803527786 Mon Sep 17 00:00:00 2001
2 From: Kirill Smelkov <kirr@landau.phys.spbu.ru>
3 Date: Wed, 4 May 2011 16:05:14 +0400
4 Subject: [PATCH] bash: Don't load user32.dll for every shell process
6 I've discovered that like msys.dll, bash too pulls user32.dll in on every
7 startup, which slows things down considerably.
9 It turned out that user32 bits are used in only one place to teach readline how
10 to paste from Windows clipboard.
12 Since this stuff is only occasionally used in interactive shells, and not used
13 at all in non-interactive scripts, let's load user32.dll dynamicaly, only when
14 needed.
16 Together with previous patch for msys.dll, this gains significant speedup for
17 shell scripts:
19 bash's
20 configure --help >/dev/null
22 old: ~2.35s
23 new: ~1.95s
25 i.e. 17% improvement.
27 Under wine the difference is much more dramatic, since loading user32.dll there
28 is slower:
30 old: ~56s
31 new: ~31a
33 i.e. 44% speedup.
35 Signed-off-by: Kirill Smelkov <kirr@landau.phys.spbu.ru>
36 ---
37 msys/packages/bash/3.1/lib/readline/kill.c | 47 ++++++++++++++++++++++++++--
38 1 files changed, 44 insertions(+), 3 deletions(-)
40 diff --git a/msys/packages/bash/3.1/lib/readline/kill.c b/msys/packages/bash/3.1/lib/readline/kill.c
41 index 2a74902..92c18e9 100644
42 --- a/msys/packages/bash/3.1/lib/readline/kill.c
43 +++ b/msys/packages/bash/3.1/lib/readline/kill.c
44 @@ -659,6 +659,45 @@ rl_yank_last_arg (count, key)
45 #if defined (__CYGWIN__) || __MSYS__
46 #include <windows.h>
48 +/* user32.dll symbols are lazily imported at runtime, in order not to slow-down
49 + * common script scenarios where clipboard stuff is not needed.
50 + *
51 + * This is big deal, because loading user32.dll for every sh.exe slows down
52 + * shell startup considerably.
53 + */
55 +static HMODULE user32;
56 +static BOOL WINAPI (*pOpenClipboard) (HWND hWndNewOwner);
57 +static BOOL WINAPI (*pCloseClipboard) (void);
58 +static HANDLE WINAPI (*pGetClipboardData) (UINT uFormat);
60 +static FARPROC xGetProcAddress(LPCSTR function)
62 + FARPROC pfunc = GetProcAddress(user32, function);
63 + if (!pfunc) {
64 + fprintf(stderr, "E: cannot import %s from user32.dll\n", function);
65 + abort();
66 + }
68 + return pfunc;
71 +static void import_user32()
73 + if (user32)
74 + return;
76 + user32 = LoadLibrary("user32.dll");
77 + if (!user32) {
78 + fprintf(stderr, "E: cannot load user32.dll\n");
79 + abort();
80 + }
82 + pOpenClipboard = xGetProcAddress("OpenClipboard");
83 + pCloseClipboard = xGetProcAddress("CloseClipboard");
84 + pGetClipboardData = xGetProcAddress("GetClipboardData");
87 int
88 rl_paste_from_clipboard (count, key)
89 int count, key;
90 @@ -666,10 +705,12 @@ rl_paste_from_clipboard (count, key)
91 char *data, *ptr;
92 int len;
94 - if (OpenClipboard (NULL) == 0)
95 + import_user32();
97 + if (pOpenClipboard (NULL) == 0)
98 return (0);
100 - data = (char *)GetClipboardData (CF_TEXT);
101 + data = (char *)pGetClipboardData (CF_TEXT);
102 if (data)
104 ptr = strchr (data, '\r');
105 @@ -686,7 +727,7 @@ rl_paste_from_clipboard (count, key)
106 rl_insert_text (ptr);
107 if (ptr != data)
108 free (ptr);
109 - CloseClipboard ();
110 + pCloseClipboard ();
112 return (0);
115 1.7.5