From 8c3da35f3efe99b9d9bae39705cabeb864bc96d6 Mon Sep 17 00:00:00 2001 From: musou1500 Date: Sun, 22 Sep 2024 00:33:04 +0900 Subject: [PATCH 1/6] improve performance on WSL2 #96 --- lib/core-complete.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/core-complete.sh b/lib/core-complete.sh index b487541d..f53fd447 100644 --- a/lib/core-complete.sh +++ b/lib/core-complete.sh @@ -2559,7 +2559,7 @@ function ble/complete/source:command/.print-command { # Note: cygwin では cyg,x86,i68 等で始まる場合にとても遅い。他の環境でも空 # の補完を実行すると遅くなる可能性がある。 local slow_compgen= - if [[ ! $COMPV ]]; then + if [[ ! $COMPV ]] || [[ -f /proc/sys/fs/binfmt_misc/WSLInterop ]]; then slow_compgen=1 elif [[ $OSTYPE == cygwin* ]]; then case $COMPV in @@ -2572,7 +2572,7 @@ function ble/complete/source:command/.print-command { # function はクォート除去が実行される。従って、compgen -A command には直 # 接 COMPV を渡し、compgen -A function には compv_quoted を渡す。 if [[ $slow_compgen ]]; then - shopt -q no_empty_cmd_completion && return 0 + [[ ! $COMPV ]] && shopt -q no_empty_cmd_completion && return 0 ble/util/conditional-sync \ 'builtin compgen -c -- "$COMPV"' \ '! ble/complete/check-cancel' 128 progressive-weight From 4e1dd4acbbe36c8cdc4bfc7cb86c84f37cdeab9b Mon Sep 17 00:00:00 2001 From: musou1500 Date: Sun, 22 Sep 2024 14:56:35 +0900 Subject: [PATCH 2/6] add ble/base/is-wsl to detect WSL --- ble.pp | 26 ++++++++++++++------------ lib/core-complete.sh | 2 +- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/ble.pp b/ble.pp index 7841d61c..a97588d8 100644 --- a/ble.pp +++ b/ble.pp @@ -1475,6 +1475,18 @@ function ble/base/is-msys1 { [[ $OSTYPE == msys && ! $cr ]] } +_ble_base_is_wsl_status= +function ble/base/is-wsl { + [[ $_ble_base_is_wsl_status ]] && return "$_ble_base_is_wsl_status" + _ble_base_is_wsl_status=1 + [[ -d /usr/lib/wsl/lib && -r /proc/version ]] || return 1 + local kernel_version + ble/bash/read kernel_version < /proc/version || return 1 + [[ $kernel_version == *-microsoft-* ]] || return 1 + _ble_base_is_wsl_status=0 +} + + function ble/util/mkd { local dir for dir; do @@ -1702,16 +1714,6 @@ function ble/base/initialize-base-directory { ## 2. /tmp/blesh/$UID を作成可能ならば、それを使う。 ## 3. $_ble_base/tmp/$UID を使う。 ## -_ble_base_is_wsl_status= -function ble/base/initialize-runtime-directory/.is-wsl { - [[ $_ble_base_is_wsl_status ]] && return "$_ble_base_is_wsl_status" - _ble_base_is_wsl_status=1 - [[ -d /usr/lib/wsl/lib && -r /proc/version ]] || return 1 - local kernel_version - ble/bash/read kernel_version < /proc/version || return 1 - [[ $kernel_version == *-microsoft-* ]] || return 1 - _ble_base_is_wsl_status=0 -} function ble/base/initialize-runtime-directory/.xdg { local runtime_dir= if [[ $XDG_RUNTIME_DIR ]]; then @@ -1736,7 +1738,7 @@ function ble/base/initialize-runtime-directory/.xdg { # Note: Some versions of WSL around 2023-09 seem to have an issue in the # permission of /run/user/*, so we avoid to use them in WSL. [[ $runtime_dir == /run/user/* ]] && - ble/base/initialize-runtime-directory/.is-wsl && + ble/base/is-wsl && return 1 if ! [[ -r $runtime_dir && -w $runtime_dir && -x $runtime_dir ]]; then @@ -1754,7 +1756,7 @@ function ble/base/initialize-runtime-directory/.tmp { # which causes a problem of missing /tmp after blesh's initialization. # https://github.com/microsoft/WSL/issues/8441#issuecomment-1139434972 # https://github.com/akinomyoga/ble.sh/discussions/462 - ble/base/initialize-runtime-directory/.is-wsl && return 1 + ble/base/is-wsl && return 1 local tmp_dir=/tmp/blesh if [[ ! -d $tmp_dir ]]; then diff --git a/lib/core-complete.sh b/lib/core-complete.sh index f53fd447..1c02544e 100644 --- a/lib/core-complete.sh +++ b/lib/core-complete.sh @@ -2559,7 +2559,7 @@ function ble/complete/source:command/.print-command { # Note: cygwin では cyg,x86,i68 等で始まる場合にとても遅い。他の環境でも空 # の補完を実行すると遅くなる可能性がある。 local slow_compgen= - if [[ ! $COMPV ]] || [[ -f /proc/sys/fs/binfmt_misc/WSLInterop ]]; then + if [[ ! $COMPV || ble/base/is-wsl ]]; then slow_compgen=1 elif [[ $OSTYPE == cygwin* ]]; then case $COMPV in From 119707062cbc690b0ef1ccd93b1f17c7e031eb98 Mon Sep 17 00:00:00 2001 From: musou1500 Date: Sun, 22 Sep 2024 15:09:05 +0900 Subject: [PATCH 3/6] move no_empty_cmd_completion to appropriate line --- lib/core-complete.sh | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/core-complete.sh b/lib/core-complete.sh index 1c02544e..18bcfb13 100644 --- a/lib/core-complete.sh +++ b/lib/core-complete.sh @@ -2559,7 +2559,10 @@ function ble/complete/source:command/.print-command { # Note: cygwin では cyg,x86,i68 等で始まる場合にとても遅い。他の環境でも空 # の補完を実行すると遅くなる可能性がある。 local slow_compgen= - if [[ ! $COMPV || ble/base/is-wsl ]]; then + if [[ !$COMPV ]]; then + shopt -q no_empty_cmd_completion && return 0 + slow_compgen=1 + elif [[ ble/base/is-wsl ]]; then slow_compgen=1 elif [[ $OSTYPE == cygwin* ]]; then case $COMPV in @@ -2568,11 +2571,11 @@ function ble/complete/source:command/.print-command { esac fi + # Note: 何故か compgen -A command はクォート除去が実行されない。compgen -A # function はクォート除去が実行される。従って、compgen -A command には直 # 接 COMPV を渡し、compgen -A function には compv_quoted を渡す。 if [[ $slow_compgen ]]; then - [[ ! $COMPV ]] && shopt -q no_empty_cmd_completion && return 0 ble/util/conditional-sync \ 'builtin compgen -c -- "$COMPV"' \ '! ble/complete/check-cancel' 128 progressive-weight From 97acc078ef70ae5825bc45d992c2d51dcaeb1056 Mon Sep 17 00:00:00 2001 From: musou1500 Date: Sun, 22 Sep 2024 16:40:38 +0900 Subject: [PATCH 4/6] accept suggestions --- ble.pp | 1 - lib/core-complete.sh | 5 ++--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/ble.pp b/ble.pp index a97588d8..6bfcc4f4 100644 --- a/ble.pp +++ b/ble.pp @@ -1486,7 +1486,6 @@ function ble/base/is-wsl { _ble_base_is_wsl_status=0 } - function ble/util/mkd { local dir for dir; do diff --git a/lib/core-complete.sh b/lib/core-complete.sh index 18bcfb13..f5d23236 100644 --- a/lib/core-complete.sh +++ b/lib/core-complete.sh @@ -2559,10 +2559,10 @@ function ble/complete/source:command/.print-command { # Note: cygwin では cyg,x86,i68 等で始まる場合にとても遅い。他の環境でも空 # の補完を実行すると遅くなる可能性がある。 local slow_compgen= - if [[ !$COMPV ]]; then + if [[ ! $COMPV ]]; then shopt -q no_empty_cmd_completion && return 0 slow_compgen=1 - elif [[ ble/base/is-wsl ]]; then + elif ble/base/is-wsl; then slow_compgen=1 elif [[ $OSTYPE == cygwin* ]]; then case $COMPV in @@ -2571,7 +2571,6 @@ function ble/complete/source:command/.print-command { esac fi - # Note: 何故か compgen -A command はクォート除去が実行されない。compgen -A # function はクォート除去が実行される。従って、compgen -A command には直 # 接 COMPV を渡し、compgen -A function には compv_quoted を渡す。 From e48a925c2ad37562b55ecdfbf8274475a9ff3eaf Mon Sep 17 00:00:00 2001 From: musou1500 Date: Sun, 22 Sep 2024 17:46:01 +0900 Subject: [PATCH 5/6] use slow_compgen only if $PATH includes /mnt --- lib/core-complete.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/core-complete.sh b/lib/core-complete.sh index f5d23236..d40c3dbe 100644 --- a/lib/core-complete.sh +++ b/lib/core-complete.sh @@ -2562,7 +2562,7 @@ function ble/complete/source:command/.print-command { if [[ ! $COMPV ]]; then shopt -q no_empty_cmd_completion && return 0 slow_compgen=1 - elif ble/base/is-wsl; then + elif [[ :$PATH: == *:/mnt/*:* && ble/base/is-wsl ]]; then slow_compgen=1 elif [[ $OSTYPE == cygwin* ]]; then case $COMPV in From 2ba9a80aa97866a6aa194e0a7d5a5eb23b32e898 Mon Sep 17 00:00:00 2001 From: musou1500 Date: Sun, 22 Sep 2024 18:02:47 +0900 Subject: [PATCH 6/6] fix wrong if condition for wsl detection Co-authored-by: Koichi Murase --- lib/core-complete.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/core-complete.sh b/lib/core-complete.sh index d40c3dbe..fd7048da 100644 --- a/lib/core-complete.sh +++ b/lib/core-complete.sh @@ -2562,7 +2562,7 @@ function ble/complete/source:command/.print-command { if [[ ! $COMPV ]]; then shopt -q no_empty_cmd_completion && return 0 slow_compgen=1 - elif [[ :$PATH: == *:/mnt/*:* && ble/base/is-wsl ]]; then + elif [[ :$PATH: == *:/mnt/*:* ]] && ble/base/is-wsl; then slow_compgen=1 elif [[ $OSTYPE == cygwin* ]]; then case $COMPV in