Skip to content

[performance] On Unixes, call the command directly instead of going through sh -c if it doesn't contain any special shell characters #2665

@kit-ty-kate

Description

@kit-ty-kate

Just a thought for an easy speed boost. The cost of calling sh -c isn't negligible on very large builds:

$ time sh -c 'for i in $(seq 0 10000); do /bin/true; done'
 'for i in $(seq 0 10000); do /bin/true; done'  2.53s user 2.55s system 113% cpu 4.482 total
---------------------------------------------------------------------------------------------------
$ time sh -c 'for i in $(seq 0 10000); do sh -c /bin/true; done'
 'for i in $(seq 0 10000); do sh -c /bin/true; done'  6.03s user 7.70s system 104% cpu 13.210 total

The number of special characters in POSIX shell is well defined so such check should be fairly simple.
The cost of character comparison shouldn't be greater than the cost of the exec indirection but if that's a worry for you, you can simply bypass the comparison when the command is above a certain length threshold

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions