Module: Pod::PodGenerate::Patches::CacheAnalyzerPatch::ParallelCacheKeyComputation

Defined in:
lib/cocoapods-podgenerate/patches/cache_analyzer_patch.rb

Instance Method Summary collapse

Instance Method Details

#create_cache_key_mappings(target_by_label) ⇒ Hash{String => TargetCacheKey}

并行计算所有 target 的缓存键

原实现(串行):

Hash[target_by_label.map { |label, target| [label, compute_key(target)] }]

优化后(并行):

使用 Concurrent::FixedThreadPool,每个 target 分配一个线程,
并发计算 TargetCacheKey,结果通过 Mutex 合并到 results Hash。

Parameters:

  • target_by_label (Hash{String => PodTarget|AggregateTarget})

    target 标签到 target 对象的映射

Returns:

  • (Hash{String => TargetCacheKey})

    target 标签到缓存键的映射



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/cocoapods-podgenerate/patches/cache_analyzer_patch.rb', line 60

def create_cache_key_mappings(target_by_label)
  UI.message '- Creating cache key mappings (parallel)' do
    pool_size = compute_pool_size
    pool = Concurrent::FixedThreadPool.new(pool_size)
    mutex = Mutex.new
    results = {}

    # 为每个 target 提交一个线程任务
    target_by_label.each do |label, target|
      pool.post do
        key = compute_cache_key(target, target_by_label)
        mutex.synchronize { results[label] = key }
      rescue StandardError => e
        # v0.1.3 bug 修复: 异常时同步重试,确保 results[label] 不为 nil
        # 如果重试仍然失败,异常会传播导致 pod install 失败(正确的行为)
        Pod::UI.warn "[cocoapods-podgenerate] Cache key computation error, retrying sync: #{e.message}"
        fallback_key = compute_cache_key(target, target_by_label)
        mutex.synchronize { results[label] = fallback_key }
      end
    end

    # v0.1.4: 带超时的等待
    pool.shutdown
    unless pool.wait_for_termination(Pod::PodGenerate::Parallel::ThreadPool::DEFAULT_TIMEOUT)
      Pod::UI.warn '[cocoapods-podgenerate] Cache key computation timed out after 120s'
      pool.kill
    end

    results
  end
end