module Kernel

def load(path, wrap = false)

def load(path, wrap = false)
  if (resolved = Bootsnap::LoadPathCache.load_path_cache.find(path))
    return load_without_bootsnap(resolved, wrap)
  end
  # load also allows relative paths from pwd even when not in $:
  if File.exist?(relative = File.expand_path(path).freeze)
    return load_without_bootsnap(relative, wrap)
  end
  raise(Bootsnap::LoadPathCache::CoreExt.make_load_error(path))
rescue LoadError => e
  e.instance_variable_set(Bootsnap::LoadPathCache::ERROR_TAG_IVAR, true)
  raise(e)
rescue Bootsnap::LoadPathCache::ReturnFalse
  false
rescue Bootsnap::LoadPathCache::FallbackScan
  fallback = true
ensure
  if fallback
    load_without_bootsnap(path, wrap)
  end
end

def require(path)

def require(path)
  return false if Bootsnap::LoadPathCache.loaded_features_index.key?(path)
  if (resolved = Bootsnap::LoadPathCache.load_path_cache.find(path))
    return require_with_bootsnap_lfi(path, resolved)
  end
  raise(Bootsnap::LoadPathCache::CoreExt.make_load_error(path))
rescue LoadError => e
  e.instance_variable_set(Bootsnap::LoadPathCache::ERROR_TAG_IVAR, true)
  raise(e)
rescue Bootsnap::LoadPathCache::ReturnFalse
  false
rescue Bootsnap::LoadPathCache::FallbackScan
  fallback = true
ensure
  if fallback
    require_with_bootsnap_lfi(path)
  end
end

def require_relative(path)

def require_relative(path)
  realpath = Bootsnap::LoadPathCache.realpath_cache.call(
    caller_locations(1..1).first.absolute_path, path
  )
  require(realpath)
end

def require_with_bootsnap_lfi(path, resolved = nil)

Note that require registers to $LOADED_FEATURES while load does not.
def require_with_bootsnap_lfi(path, resolved = nil)
  Bootsnap::LoadPathCache.loaded_features_index.register(path, resolved) do
    require_without_bootsnap(resolved || path)
  end
end