```python:inference/core/workflows/core_steps/classical_cv/mask_area_measurement/v1.py
from typing import List

import cv2 as cv
import numpy as np
import supervision as sv


def compute_detection_areas(detections: sv.Detections) -> List[float]:
    """Compute the area of all detections in square pixels.

    For bounding-box-only detections, areas are computed in a single vectorized
    operation. For detections with segmentation masks, the area is the count of
    non-zero mask pixels (via ``cv2.countNonZero``). This correctly handles masks
    with holes — hole pixels are zero and are not counted. Falls back to the
    bounding box area when the mask pixel count is zero.

    Args:
        detections: A supervision Detections object.

    Returns:
        List of areas in square pixels, one per detection.
    """
    n = len(detections)
    if n == 0:
        return []


    # Pre-compute bounding box areas for all detections
    xyxy = detections.xyxy
    bbox_areas = (xyxy[:, 2] - xyxy[:, 0]) * (xyxy[:, 3] - xyxy[:, 1])
    
    if detections.mask is None:
        return bbox_areas.tolist()
    
    areas = []
    for i in range(n):
        # Use np.count_nonzero which works directly on boolean arrays
        count = np.count_nonzero(detections.mask[i])
        if count > 0:
            areas.append(float(count))
        else:
            areas.append(float(bbox_areas[i]))
    
    return areas
```