Throughout the Prawn codebase, repeated calculations which can benefit from caching are made In some cases, caching and reusing results can not only save CPU cycles but also greatly
reduce memory requirements
But at the same time, we don't want to throw away thread safety We have two interchangeable thread-safe cache implementations:
As an optimization, this could access the hash directly on VMs with a global interpreter lock (like MRI)
# File lib/prawn/utilities.rb, line 20 def initialize @cache = {} @mutex = Mutex.new end
# File lib/prawn/utilities.rb, line 24 def [](key) @mutex.synchronize { @cache[key] } end
# File lib/prawn/utilities.rb, line 27 def []=(key,value) @mutex.synchronize { @cache[key] = value } end