Repository: Azure/azure-sdk-for-net
File: eng/packages/http-client-csharp-mgmt/emitter/src/resource-detection.ts (TypeScript)

Code before:
  // For multiple-path resources (same model at different paths), detect parent-child relationships through path matching
  // This is needed when both parent and child use the same model (e.g., legacy-operations pattern)
  // This is also specific to legacy resource detection
  for (const [metadataKey, metadata] of resourcePathToMetadataMap) {
    if (!metadata.parentResourceId && metadata.resourceIdPattern) {
      // Find the longest matching parent path (most specific parent)
      let longestParentPath: string | undefined;
      let longestParentSegmentCount = 0;
      // Check if this resource's path is a child of another resource's path
      for (const [otherKey, otherMetadata] of resourcePathToMetadataMap) {
        if (otherKey !== metadataKey && otherMetadata.resourceIdPattern) {
          const thisPath = metadata.resourceIdPattern;
          const potentialParentPath = otherMetadata.resourceIdPattern;

          // The child path should start with the parent path followed by a "/"
          if (
            isPrefix(potentialParentPath, thisPath) &&
            !isPrefix(thisPath, potentialParentPath)
          ) {
            const segmentCount = potentialParentPath
              .split("/")
              .filter((s) => s.length > 0).length;
            if (segmentCount > longestParentSegmentCount) {
              longestParentSegmentCount = segmentCount;
              longestParentPath = potentialParentPath;
            }
          }
        }
      }
      if (longestParentPath) {
        metadata.parentResourceId = longestParentPath;
        // Note: we don't set parentResourceModelId here since they share the same model
      }
    }
  }

  // Update the model's resourceScope based on resource scope decorator if it exists or based on the Read method's scope.
  // This is specific to legacy resource detection
  for (const [metadataKey, metadata] of resourcePathToMetadataMap) {
    const modelId = metadataKey.split("|")[0];
    const model = resourceModelMap.get(modelId);
    if (model) {
      metadata.resourceScope = getResourceScope(model, metadata.methods);
    }
  }

  // Create parent lookup context for legacy resource detection
  // In this case, parent information comes from decorators and path matching (already populated above)
  const parentLookup: ParentResourceLookupContext = {
    getParentResource: (
      resource: ArmResourceSchema

Review comment (performance):
I think this could be very inefficient - the `isPrefix` is doing the split, and now to get the segment counts, we are doing the split again.
Maybe we could open an issue to optimize this in the future.

Code after:
  // For multiple-path resources (same model at different paths), detect parent-child relationships through path matching
  // This is needed when both parent and child use the same model (e.g., legacy-operations pattern)
  // This is also specific to legacy resource detection
  for (const [metadataKey, metadata] of resourcePathToMetadataMap) {
    if (!metadata.parentResourceId && metadata.resourceIdPattern) {
      // Find the longest matching parent path (most specific parent)
      let longestParentPath: string | undefined;
      let longestParentSegmentCount = 0;
      // Check if this resource's path is a child of another resource's path
      for (const [otherKey, otherMetadata] of resourcePathToMetadataMap) {
        if (otherKey !== metadataKey && otherMetadata.resourceIdPattern) {
          const thisPath = metadata.resourceIdPattern;
          const potentialParentPath = otherMetadata.resourceIdPattern;

          // The child path should start with the parent path followed by a "/"
          if (
            isPrefix(potentialParentPath, thisPath) &&
            !isPrefix(thisPath, potentialParentPath)
          ) {
            // Use getSharedSegmentCount to get the segment count without redundant splitting
            const segmentCount = getSharedSegmentCount(
              potentialParentPath,
              thisPath
            );
            if (segmentCount > longestParentSegmentCount) {
              longestParentSegmentCount = segmentCount;
              longestParentPath = potentialParentPath;
            }
          }
        }
      }
      if (longestParentPath) {
        metadata.parentResourceId = longestParentPath;
        // Note: we don't set parentResourceModelId here since they share the same model
      }
    }
  }

  // Update the model's resourceScope based on resource scope decorator if it exists or based on the Read method's scope.
  // This is specific to legacy resource detection
  for (const [metadataKey, metadata] of resourcePathToMetadataMap) {
    const modelId = metadataKey.split("|")[0];
    const model = resourceModelMap.get(modelId);
    if (model) {
      metadata.resourceScope = getResourceScope(model, metadata.methods);
    }
  }

  // Create parent lookup context for legacy resource detection
  // In this case, parent information comes from decorators and path matching (already populated above)
  const parentLookup: ParentResourceLookupContext = {