Class: Fontisan::Variation::ParallelGenerator
- Inherits:
-
Object
- Object
- Fontisan::Variation::ParallelGenerator
- Defined in:
- lib/fontisan/variation/parallel_generator.rb
Overview
Generates multiple font instances in parallel
Uses thread pool for efficient batch processing with caching. Supports progress tracking and graceful error handling per instance.
Instance Attribute Summary collapse
-
#cache ⇒ ThreadSafeCache
readonly
Thread-safe cache.
-
#font ⇒ TrueTypeFont, OpenTypeFont
readonly
Variable font.
-
#thread_count ⇒ Integer
readonly
Number of threads.
Instance Method Summary collapse
-
#generate_batch(coordinates_list) {|index, total| ... } ⇒ Array<Hash>
Generate multiple instances in parallel.
-
#generate_with_cache(coordinates) ⇒ Hash
Generate instance with caching.
-
#initialize(font, options = {}) ⇒ ParallelGenerator
constructor
Initialize parallel generator.
Constructor Details
#initialize(font, options = {}) ⇒ ParallelGenerator
Initialize parallel generator
46 47 48 49 50 |
# File 'lib/fontisan/variation/parallel_generator.rb', line 46 def initialize(font, = {}) @font = font @cache = [:cache] || ThreadSafeCache.new @thread_count = [:threads] || [4, Etc.nprocessors].max end |
Instance Attribute Details
#cache ⇒ ThreadSafeCache (readonly)
Returns Thread-safe cache.
35 36 37 |
# File 'lib/fontisan/variation/parallel_generator.rb', line 35 def cache @cache end |
#font ⇒ TrueTypeFont, OpenTypeFont (readonly)
Returns Variable font.
32 33 34 |
# File 'lib/fontisan/variation/parallel_generator.rb', line 32 def font @font end |
#thread_count ⇒ Integer (readonly)
Returns Number of threads.
38 39 40 |
# File 'lib/fontisan/variation/parallel_generator.rb', line 38 def thread_count @thread_count end |
Instance Method Details
#generate_batch(coordinates_list) {|index, total| ... } ⇒ Array<Hash>
Generate multiple instances in parallel
Processes each coordinate set in parallel using thread pool. Returns results in same order as input coordinates.
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 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/fontisan/variation/parallel_generator.rb', line 62 def generate_batch(coordinates_list, &progress_callback) return [] if coordinates_list.empty? total = coordinates_list.length results = Array.new(total) completed = 0 mutex = Mutex.new # Create thread pool pool = Fontisan::Utils::ThreadPool.new(@thread_count) # Schedule all jobs futures = coordinates_list.map.with_index do |coordinates, index| pool.schedule do { index: index, result: generate_with_cache(coordinates), } end end # Collect results futures.each do |future| job_result = future.value results[job_result[:index]] = job_result[:result] # Update progress if progress_callback mutex.synchronize do completed += 1 yield(completed, total) end end end # Shutdown pool pool.shutdown results end |
#generate_with_cache(coordinates) ⇒ Hash
Generate instance with caching
Uses cache to avoid regenerating identical instances.
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/fontisan/variation/parallel_generator.rb', line 109 def generate_with_cache(coordinates) font_checksum = calculate_font_checksum begin tables = @cache.fetch_instance(font_checksum, coordinates) do generator = InstanceGenerator.new(@font, coordinates) generator.generate end { success: true, coordinates: coordinates, tables: tables, error: nil, } rescue StandardError => e { success: false, coordinates: coordinates, tables: nil, error: { message: e., class: e.class.name, backtrace: e.backtrace&.first(5), }, } end end |