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

Code before:
            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")
                }
            }
        }

        if (!anyOptional) {
            // The process is the same as call,
            // but it is called directly to avoid the processing cost of spread operator.
            @Suppress("UNCHECKED_CAST")

Review comment (suggestion):
The `Offset` is calculated first, and then subtracted from `parameter.index` only when the default argument is used, thereby reducing the amount of calculation.
[32cbe68](https://github.com/JetBrains/kotlin/pull/4840/commits/32cbe68ed38a0ff3f91f600d69dde94d1c0171c2)

Code after:
        }

        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")
                }
            }

            if (parameter.kind == KParameter.Kind.VALUE) {
                valueParameterIndex++
            }
        }