```python:inference/core/workflows/core_steps/common/utils.py
from typing import List

import numpy as np
import supervision as sv
from inference.core.workflows.execution_engine.constants import (
    KEYPOINTS_CLASS_ID_KEY_IN_INFERENCE_RESPONSE,
    KEYPOINTS_CLASS_ID_KEY_IN_SV_DETECTIONS,
    KEYPOINTS_CLASS_NAME_KEY_IN_INFERENCE_RESPONSE,
    KEYPOINTS_CLASS_NAME_KEY_IN_SV_DETECTIONS,
    KEYPOINTS_CONFIDENCE_KEY_IN_INFERENCE_RESPONSE,
    KEYPOINTS_CONFIDENCE_KEY_IN_SV_DETECTIONS,
    KEYPOINTS_KEY_IN_INFERENCE_RESPONSE, KEYPOINTS_XY_KEY_IN_SV_DETECTIONS,
    X_KEY, Y_KEY)


def add_inference_keypoints_to_sv_detections(
    inference_prediction: List[dict],
    detections: sv.Detections,
) -> sv.Detections:
    if len(inference_prediction) != len(detections):
        raise ValueError(
            f"Detected missmatch in number of detections in sv.Detections instance ({len(detections)}) "
            f"and `inference` predictions ({len(inference_prediction)}) while attempting to add keypoints metadata."
        )
    
    n = len(inference_prediction)
    if n == 0:
        # Handle empty case early
        detections[KEYPOINTS_XY_KEY_IN_SV_DETECTIONS] = np.zeros((0, 0, 2), dtype=np.float32)
        detections[KEYPOINTS_CONFIDENCE_KEY_IN_SV_DETECTIONS] = np.zeros((0, 0), dtype=np.float32)
        detections[KEYPOINTS_CLASS_ID_KEY_IN_SV_DETECTIONS] = np.zeros((0, 0), dtype=int)
        detections[KEYPOINTS_CLASS_NAME_KEY_IN_SV_DETECTIONS] = np.full((0, 0), "", dtype=object)
        return detections
    
    # First pass: determine max keypoints count
    max_kps = 0
    keypoints_counts = []
    for inference_detection in inference_prediction:
        keypoints = inference_detection.get(KEYPOINTS_KEY_IN_INFERENCE_RESPONSE, [])
        kp_count = len(keypoints)
        keypoints_counts.append(kp_count)
        if kp_count > max_kps:
            max_kps = kp_count
    
    # Pre-allocate all arrays
    padded_xy = np.zeros((n, max_kps, 2), dtype=np.float32)
    padded_conf = np.zeros((n, max_kps), dtype=np.float32)
    padded_class_id = np.zeros((n, max_kps), dtype=int)
    padded_class_name = np.full((n, max_kps), "", dtype=object)
    
    # Second pass: fill arrays in a single iteration
    for i, inference_detection in enumerate(inference_prediction):
        k = keypoints_counts[i]
        if k > 0:
            keypoints = inference_detection[KEYPOINTS_KEY_IN_INFERENCE_RESPONSE]
            # Extract all data in one pass through keypoints
            for j, kp in enumerate(keypoints):
                padded_xy[i, j, 0] = kp[X_KEY]
                padded_xy[i, j, 1] = kp[Y_KEY]
                padded_conf[i, j] = kp[KEYPOINTS_CONFIDENCE_KEY_IN_INFERENCE_RESPONSE]
                padded_class_id[i, j] = kp[KEYPOINTS_CLASS_ID_KEY_IN_INFERENCE_RESPONSE]
                padded_class_name[i, j] = kp[KEYPOINTS_CLASS_NAME_KEY_IN_INFERENCE_RESPONSE]
    
    detections[KEYPOINTS_XY_KEY_IN_SV_DETECTIONS] = padded_xy
    detections[KEYPOINTS_CONFIDENCE_KEY_IN_SV_DETECTIONS] = padded_conf
    detections[KEYPOINTS_CLASS_ID_KEY_IN_SV_DETECTIONS] = padded_class_id
    detections[KEYPOINTS_CLASS_NAME_KEY_IN_SV_DETECTIONS] = padded_class_name
    return detections
```