Skip to content

Performance problem: (implicitly) dot-sourced code is slowed down by variable lookups #8911

@mklement0

Description

@mklement0

Summary of the performance problem

Note: This SO question inspired this suggestion.

Updated based on feedback from @lzybkr below.

# REGULAR, direct invocation of an expression (a `foreach` statement 
# that loops 1 million (1e6) times, in this case) - implicitly DOT-SOURCED
(Measure-Command { $result = foreach ($n in 1..1e6) { $n } }).TotalSeconds

# Invocation in CHILD SCOPE, using & { ... }
# 10+ TIMES FASTER.
(Measure-Command { $result = & { foreach ($n in 1..1e6) { $n } } }).TotalSeconds

Sample timings (note the ratio) on macOS:

2.5900995  # DIRECT invocation (implicitly DOT-SOURCED)
0.2580877  # in CHILD SCOPE, via & { ... }

That is, direct invocation was about 10 times slower than invocation via &.

Possible implementation

See @lzybkr's comments below, which also explains the root cause.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Enhancementthe issue is more of a feature request than a bugResolution-No ActivityIssue has had no activity for 6 months or moreWG-Enginecore PowerShell engine, interpreter, and runtimeWG-Engine-Performancecore PowerShell engine, interpreter, and runtime performance

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions