Repository: JetBrains/kotlin
File: core/reflection.jvm/src/kotlin/reflect/jvm/internal/KCallableImpl.kt (Kotlin)

Code before:
            } else if (parameter.isVararg) {
                arguments[parameter.index] = defaultEmptyArray(parameter.type)
            }
        }

        for (i in 0 until maskSize) {
            arguments[parameterSize + i] = 0
        }

        arguments
    }

    private fun getAbsentArguments(): Array<Any?> = _absentArguments().clone()

    // See ArgumentGenerator#generate
    internal fun callDefaultMethod(args: Map<KParameter, Any?>, continuationArgument: Continuation<*>?): R {
        val parameters = parameters
        val parameterSize = parameters.size + (if (isSuspend) 1 else 0)

        val arguments = getAbsentArguments().apply {
            if (isSuspend) {
                // continuationArgument is tail of arguments
                this[parameters.size] = continuationArgument
            }
        }

        // @see _parameters
        val valueParameterOffset = when {
            parameters.getOrNull(1)?.let { it.kind != KParameter.Kind.VALUE } ?: false -> 2
            parameters.getOrNull(0)?.let { it.kind != KParameter.Kind.VALUE } ?: false -> 1
            else -> 0
        }
        var anyOptional = false

        for (parameter in parameters) {
            when {
                args.containsKey(parameter) -> {
                    arguments[parameter.index] = args[parameter]
                }
                // Absent value is already set at _absentArguments
                parameter.isOptional -> {
                    val valueParameterIndex = parameter.index - valueParameterOffset
                    val maskIndex = parameterSize + (valueParameterIndex / Integer.SIZE)
                    arguments[maskIndex] = (arguments[maskIndex] as Int) or (1 shl (valueParameterIndex % Integer.SIZE))
                    anyOptional = true
                }
                parameter.isVararg -> {}
                else -> {
                    throw IllegalArgumentException("No argument provided for a required parameter: $parameter")
                }
            }

Review comment (suggestion):
The following optimization to no-argument functions was found to be nearly twice as fast as 638d89c.
Does this optimization look like it should be incorporated?

```
Benchmark                          Mode  Cnt         Score         Error  Units
Measurement.zero                  thrpt    4  22299885.316 ±  295995.533  ops/s
Measurement.fiveWithDefault       thrpt    4   1927106.356 ±  225032.224  ops/s
Measurement.fiveWithoutDefault    thrpt    4   2460623.502 ±   96649.714  ops/s
Measurement.oneWithDefault        thrpt    4   5384826.666 ±  340254.100  ops/s
Measurement.oneWithoutDefault     thrpt    4   5654229.984 ± 5914104.417  ops/s
Measurement.twentyWithDefault     thrpt    4    510341.805 ±    2940.113  ops/s
Measurement.twentyWithoutDefault  thrpt    4    739231.653 ±   21298.002  ops/s
```

```suggestion
        val parameterSize = parameters.size + (if (isSuspend) 1 else 0)

        // Optimization for general no-argument functions.        
        if (parameters.isEmpty()) {
            @Suppress("UNCHECKED_CAST")
            return reflectionCall {
                caller.call(if (isSuspend) arrayOf(continuationArgument) else emptyArray()) as R
            }
        } else if (parameters.size == 1) {
            parameters.first().takeIf { it.kind != KParameter.Kind.VALUE }?.let { parameter ->
                @Suppress("UNCHECKED_CAST")
                return reflectionCall {
                    caller.call(if (isSuspend) arrayOf(args[parameter], continuationArgument) else arrayOf(args[parameter])) as R
                }
            }
        }
```

Code after:
                arguments[parameter.index] = defaultEmptyArray(parameter.type)
            }
        }

        for (i in 0 until maskSize) {
            arguments[parameterSize + i] = 0
        }

        arguments
    }

    private fun getAbsentArguments(): Array<Any?> = _absentArguments().clone()

    // See ArgumentGenerator#generate
    internal fun callDefaultMethod(args: Map<KParameter, Any?>, continuationArgument: Continuation<*>?): R {
        val parameters = parameters

        // Optimization for functions without value/receiver parameters.
        if (parameters.isEmpty()) {
            @Suppress("UNCHECKED_CAST")
            return reflectionCall {
                caller.call(if (isSuspend) arrayOf(continuationArgument) else emptyArray()) as R
            }
        }

        val parameterSize = parameters.size + (if (isSuspend) 1 else 0)

        val arguments = getAbsentArguments().apply {
            if (isSuspend) {
                this[parameters.size] = continuationArgument
            }
        }

        var valueParameterIndex = 0
        var anyOptional = false

        for (parameter in parameters) {
            when {
                args.containsKey(parameter) -> {
                    arguments[parameter.index] = args[parameter]
                }
                parameter.isOptional -> {
                    val maskIndex = parameterSize + (valueParameterIndex / Integer.SIZE)
                    arguments[maskIndex] = (arguments[maskIndex] as Int) or (1 shl (valueParameterIndex % Integer.SIZE))
                    anyOptional = true
                }
                parameter.isVararg -> {}
                else -> {
                    throw IllegalArgumentException("No argument provided for a required parameter: $parameter")
                }
            }