bash 如何加速 Cygwin?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2512892/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 21:52:09  来源:igfitidea点击:

How to speed up Cygwin?

bashdrupalcygwindrush

提问by tester

I have been running drushscripts (for Drupal) with Cygwinon my relatively fast windows machine, but I still have to wait about a minutefor any drush command (specifically drush cache clearto execute).

我一直在我相对较快的 Windows 机器上使用Cygwin运行drush脚本(用于Drupal),但我仍然需要等待大约一分钟才能执行任何 drush 命令(特别是清除 drush 缓存以执行)。

I'm quite sure it has something to do with the speed of Cygwin since my fellow developers (who are running Linux) can run these scripts in about 5 seconds.

我很确定这与 Cygwin 的速度有关,因为我的开发人员(正在运行 Linux)可以在大约 5 秒内运行这些脚本。

Is there a way to make Cygwin use more memory and/or CPU per terminal?

有没有办法让 Cygwin 每个终端使用更多的内存和/或 CPU?

回答by Warren Young

The problem you're running into is not some arbitrary limit in Cygwin that you can make go away with a settings change. It's an inherent aspect of the way Cygwin has to work to get the POSIX semantics programs built under it expect.

您遇到的问题不是 Cygwin 中的一些任意限制,您可以通过设置更改来消除。这是 Cygwin 必须工作以获取在其下构建的 POSIX 语义程序的固有方式。

The POSIX fork()system call has no native equivalent on Windows, so Cygwin is forced to emulate it in a very inefficient way. Shell scripts cause a call to fork()every time they execute an external process, which happens quite a lot since the shell script languages are so impoverished relative to what we'd normally call a programming language. External programs are how shell scripts get anything of consequence done.

POSIXfork()系统调用在 Windows 上没有本地等效项,因此 Cygwin 被迫以非常低效的方式模拟它。Shell 脚本fork()每次执行外部进程时都会导致调用,这种情况经常发生,因为与我们通常所说的编程语言相比,Shell 脚本语言非常贫乏。外部程序是 shell 脚本完成任何结果的方式。

There are other inefficiencies in Cygwin, though if you profiled it, you'd probably find that that's the number one speed hit. In most places, the Cygwin layer between a program built using it and the underlying OS is pretty thin. The developers of Cygwin take a lot of pains to keep the layer as thin as possible while still providing correct POSIX semantics. The current uncommon thickness in the fork()call emulation is unavoidable short of Microsoft adding a native fork()type facility to their OS. Their incentives to do that aren't very good.

Cygwin 中还有其他低效问题,但如果您对其进行分析,您可能会发现这是第一大速度。在大多数地方,使用它构建的程序和底层操作系统之间的 Cygwin 层非常薄。Cygwin 的开发人员煞费苦心地保持层尽可能薄,同时仍然提供正确的 POSIX 语义。fork()呼叫仿真中当前不常见的厚度是微软fork()在其操作系统中添加本机类型工具不可避免的。他们这样做的动机不是很好。

The solutions posted above as comments aren't bad.

上面作为评论发布的解决方案还不错。

Another possibility is to go through the drushscript and see if there are calls to external programs you can replace with shell intrinsics or more efficient constructs. I wouldn't expect a huge speed improvement by doing that, but it has the nice property that you'll speed things up on the Linux side as well. (fork()is efficient on Linux, but starting external programs is still a big speed hit that you may not have to pay as often as you currently do.) For instance:

另一种可能性是drush查看脚本,看看是否有对外部程序的调用,您可以用 shell 内在函数或更有效的构造来替换它。我不希望这样做会带来巨大的速度提升,但它具有很好的特性,您也可以在 Linux 方面加快速度。(fork()在 Linux 上效率很高,但是启动外部程序仍然是一个很大的速度损失,您可能不必像目前那样经常支付费用。)例如:

numlines=`grep somepattern $somefile | wc -l`
if [ $numlines -gt 0 ] ; then ...

would run faster as:

会跑得更快,因为:

if grep -q somepattern $somefile ; then ...

The first version is arguably clearer, but it requires at least three external program invocations, and with primitive shells, four. (Do you see all of them?) The replacement requires only one external program invocation.

第一个版本可以说更清晰,但它至少需要三个外部程序调用,并且使用原始 shell 需要四个. (你看到所有这些了吗?)替换只需要一个外部程序调用。

回答by studgeek

Also look at things that slow down Cygwin startup:

还要看看会减慢 Cygwin 启动速度的事情:

  • Trim down your Windows PATH (to the bare bones like %SystemRoot%\system32;%SystemRoot%)
  • Remove things you don't need from bashrc and bash_profile
  • Move things you only need in your terminal window from bashrc to bash_profile
  • One surprisingly large time suck in Cygwin is Bash completion. If you are using it (and you should because it's great), only source completion for the commands you need (rather than all of them which used to be the default). And, as mentioned above, source them from bash_profile, not bashrc.
  • 修剪你的 Windows PATH(到像 %SystemRoot%\system32;%SystemRoot% 这样的骨架)
  • 从 bashrc 和 bash_profile 中删除不需要的东西
  • 将你只需要在终端窗口中的东西从 bashrc 移动到 bash_profile
  • Cygwin 中一个令人惊讶的大量时间是 Bash 完成。如果您正在使用它(并且您应该使用它,因为它很棒),只需对您需要的命令进行源完成(而不是所有以前的默认命令)。并且,如上所述,它们来自bash_profile,而不是bashrc

回答by Soenke

You can give Cygwin a higher priority.

您可以赋予 Cygwin 更高的优先级。

Write a new batch file, for example, "cygstart.bat" with the following content:

编写一个新的批处理文件,例如“cygstart.bat”,内容如下:

start "Cygwin" /high C:\cygwin\Cygwin.bat

The /highswitch gives the shell a higher process priority.

/high开关赋予外壳更高的进程优先级。