You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
(PUP-9561) Optimize parameter validation for blocks
Puppet used to infer types for arguments passed to a block. This was slow for
several reasons:
* Type inference is generally slow for large collections (hash/array)
* Iterable functions like `reduce` pass an accumulator to the block for each
iteration, so we inferred the accumulator's type multiple times.
* When building a collection using `reduce`, the accumulator increases in size
each time. So constructions like the following infer the type of an
accumulator of size 0, 1, ..., N-1, which is equivalent to inferring the type
of an accumulator of size N(N-1)/2:
Integer[1, N].reduce([]) |$memo, $value| { $memo << $value}
However, block arguments are often defined without a type, e.g. `$memo`, so the
type inference was wasted effort.
This commit switches from `callable?` to `callable_with?`. So rather than
inferring the type of all arguments, and checking if those types are assignable
to the parameter's type, we instead check if each argument is an instance of the
parameter's type. For example, we check if the Ruby Hash {} is an instance of
the `PAnyType`, which is always true[1]. The `callable_with?` method also checks
required and optional parameters and return types, like the `callable?` method
does.
Note the `callable_with?` method was introduced in 2edd30e to address similar
type inference slowness.
[1] https://github.com/puppetlabs/puppet/blob/7.11.0/lib/puppet/pops/types/types.rb#L268
0 commit comments