[GH-ISSUE #3886] perf: gen_random_string causes high CPU usage due to 75% byte rejection in tr #2627

Closed
opened 2026-10-31 20:26:49 +07:00 by bsakaguchi · 2 comments
Owner

Originally created by @Kookiejarz on GitHub (Mar 5, 2026).
Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/3886

During installation, gen_random_string pipes /dev/urandom directly into
tr -dc 'a-zA-Z0-9'. Since only ~24% of random bytes pass the filter,
tr discards ~75% of all input and busy-loops at high CPU until the pipe closes.

This causes a noticeable CPU spike on every fresh install or credential regeneration, especially noticeable on low-spec VPS ( less than 1 vCPU / 512MB RAM), where tr can saturate the single core for several seconds during installation. An example is on Google Cloud Platform's e2-Micro Instance.

Describe the solution you'd like

Replace the current /dev/urandom | tr pipeline with openssl rand -base64,
which outputs A-Za-z0-9+/= only, reducing tr's rejection rate from ~75% to ~2%:

gen_random_string() {
    local length="$1"
    openssl rand -base64 $(( length * 2 )) \
        | tr -dc 'a-zA-Z0-9' \
        | head -c "$length"
}

Update: after reviewing the source, openssl is not included in install_base for any distro. The PR therefore also adds openssl to all package manager install lists as a new explicit dependency.

Describe alternatives you've considered

Here is a python alternative, cleanest but requires python pre-installed:

gen_random_string() {
    python3 -c "
import secrets, string
chars = string.ascii_letters + string.digits
print(''.join(secrets.choice(chars) for _ in range($1)), end='')
"
}

No CPU idle time issues, with guaranteed length precision. Recommended for use when scripts can rely on Python.

I didn't apply this method because this is a shell installation script and the goal is to keep it self-contained. Adding Python3 as a dependency would pull in a full runtime or a single use case, which is disproportionate for a lightweight install script.

So, OpenSSL is the pragmatic choice: one explicit new dependency across all distros, but a minimal and justified one — and far safer than relying on Python being present. :)

Checklist

  • This feature report is written entirely in English.
Originally created by @Kookiejarz on GitHub (Mar 5, 2026). Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/3886 ### Is your feature request related to a problem? During installation, gen_random_string pipes /dev/urandom directly into tr -dc 'a-zA-Z0-9'. Since only ~24% of random bytes pass the filter, tr discards ~75% of all input and busy-loops at high CPU until the pipe closes. This causes a noticeable CPU spike on every fresh install or credential regeneration, especially noticeable on low-spec VPS ( less than 1 vCPU / 512MB RAM), where tr can saturate the single core for several seconds during installation. An example is on Google Cloud Platform's e2-Micro Instance. ### Describe the solution you'd like Replace the current /dev/urandom | tr pipeline with openssl rand -base64, which outputs A-Za-z0-9+/= only, reducing tr's rejection rate from ~75% to ~2%: ``` gen_random_string() { local length="$1" openssl rand -base64 $(( length * 2 )) \ | tr -dc 'a-zA-Z0-9' \ | head -c "$length" } ``` > Update: after reviewing the source, openssl is not included in install_base for any distro. The PR therefore also adds openssl to all package manager install lists as a new explicit dependency. ### Describe alternatives you've considered Here is a python alternative, cleanest but requires python pre-installed: ``` gen_random_string() { python3 -c " import secrets, string chars = string.ascii_letters + string.digits print(''.join(secrets.choice(chars) for _ in range($1)), end='') " } ``` No CPU idle time issues, with guaranteed length precision. Recommended for use when scripts can rely on Python. I didn't apply this method because this is a shell installation script and the goal is to keep it self-contained. Adding Python3 as a dependency would pull in a full runtime or a single use case, which is disproportionate for a lightweight install script. So, OpenSSL is the pragmatic choice: one explicit new dependency across all distros, but a minimal and justified one — and far safer than relying on Python being present. :) ### Checklist - [x] This feature report is written entirely in English.
bsakaguchi 2026-10-31 20:26:49 +07:00
Author
Owner

@nebulosa2007 commented on GitHub (Mar 16, 2026):

gen_random() {
    local chars='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
    local len=20
    local str=''
    for ((i=0; i<len; i++)); do
        str+="${chars:$((RANDOM % ${#chars})):1}"
    done
    echo "$str"
}

without new dependencies

<!-- gh-comment-id:4070523017 --> @nebulosa2007 commented on GitHub (Mar 16, 2026): ``` gen_random() { local chars='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' local len=20 local str='' for ((i=0; i<len; i++)); do str+="${chars:$((RANDOM % ${#chars})):1}" done echo "$str" } ``` without new dependencies
Author
Owner

@Kookiejarz commented on GitHub (Mar 16, 2026):

Fair point on readability! The tradeoff I was considering: $RANDOM is a
15-bit PRNG seeded from PID+time, which could be a concern if these strings
ever end up in auth contexts. Since openssl is already a hard dep here
(via SSL cert management), I went with the cryptographic backing to be safe
without introducing anything new. :D

<!-- gh-comment-id:4070692062 --> @Kookiejarz commented on GitHub (Mar 16, 2026): Fair point on readability! The tradeoff I was considering: $RANDOM is a 15-bit PRNG seeded from PID+time, which could be a concern if these strings ever end up in auth contexts. Since openssl is already a hard dep here (via SSL cert management), I went with the cryptographic backing to be safe without introducing anything new. :D
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
MHSanaei/3x-ui#2627
No description provided.