Repository: apple/pkl
File: pkl-core/src/main/java/org/pkl/core/resource/ResourceReaders.java (Java)

Code before:

  private static final class ProjectPackageResource implements ResourceReader {
    static final ProjectPackageResource INSTANCE = new ProjectPackageResource();

    @Override
    public String getUriScheme() {
      return "projectpackage";
    }

    @Override
    public Optional<Object> read(URI uri)
        throws IOException, URISyntaxException, SecurityManagerException {
      var assetUri = new PackageAssetUri(uri);
      var dependency = getProjectDepsResolver().getResolvedDependency(assetUri.getPackageUri());
      var local = getLocalUri(dependency, assetUri);
      if (local != null) {
        return VmContext.get(null).getResourceManager().read(local, null);
      }
      var remoteDep = (Dependency.RemoteDependency) dependency;
      var bytes = getPackageResolver().getBytes(assetUri, true, remoteDep.getChecksums());
      return Optional.of(new Resource(uri, bytes));
    }

    @Override
    public boolean hasHierarchicalUris() {
      return true;
    }

    @Override
    public boolean isGlobbable() {
      return true;
    }

    @Override
    public boolean hasFragmentPaths() {
      return true;
    }

    @Override
    public List<PathElement> listElements(SecurityManager securityManager, URI baseUri)
        throws IOException, SecurityManagerException {
      securityManager.checkResolveResource(baseUri);
      var packageAssetUri = PackageAssetUri.create(baseUri);
      var dependency =
          getProjectDepsResolver().getResolvedDependency(packageAssetUri.getPackageUri());
      var local = getLocalUri(dependency, packageAssetUri);
      if (local != null) {
        var reader = VmContext.get(null).getResourceManager().getResourceReader(local);
        if (reader == null) {
          throw new VmExceptionBuilder()
              .evalError("noResourceReaderRegistered", local.getScheme())

Review comment (performance):
The core problem is around using `ResourceManager#read`, which returns a `org.pkl.core.runtime.VmTyped` rather than a `org.pkl.core.resource.Resource`.

Additionally, we shouldn't use the ResourceManager here because that method statefully alters its cache. Since this method is already called within the same cache's `computeIfAbsent` call, mutating that cache results in a ConcurrentModificationException.

Code after:

  private static final class ProjectPackageResource implements ResourceReader {
    static final ProjectPackageResource INSTANCE = new ProjectPackageResource();

    @Override
    public String getUriScheme() {
      return "projectpackage";
    }

    @Override
    public Optional<Object> read(URI uri)
        throws IOException, URISyntaxException, SecurityManagerException {
      var assetUri = new PackageAssetUri(uri);
      var dependency = getProjectDepsResolver().getResolvedDependency(assetUri.getPackageUri());
      var local = getLocalUri(dependency, assetUri);
      if (local != null) {
        var resourceManager = VmContext.get(null).getResourceManager();
        var securityManager = VmContext.get(null).getSecurityManager();
        securityManager.checkReadResource(local);
        var reader = resourceManager.getResourceReader(local);
        if (reader == null) {
          throw new VmExceptionBuilder()
              .evalError("noResourceReaderRegistered", uri.getScheme())
              .build();
        }
        return resourceManager.doRead(reader, local, null);
      }
      var remoteDep = (Dependency.RemoteDependency) dependency;
      var bytes = getPackageResolver().getBytes(assetUri, true, remoteDep.getChecksums());
      return Optional.of(new Resource(uri, bytes));
    }

    @Override
    public boolean hasHierarchicalUris() {
      return true;
    }

    @Override
    public boolean isGlobbable() {
      return true;
    }

    @Override
    public boolean hasFragmentPaths() {
      return true;
    }

    @Override
    public List<PathElement> listElements(SecurityManager securityManager, URI baseUri)
        throws IOException, SecurityManagerException {
      securityManager.checkResolveResource(baseUri);