Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace global Hash usage with TS::Cache #209

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ Hoe.spec 'arel' do
self.licenses = ['MIT']
self.readme_file = 'README.markdown'
self.extra_rdoc_files = FileList['README.markdown']

dependency('thread_safe', '~> 0.1')
end
5 changes: 3 additions & 2 deletions lib/arel/visitors/to_sql.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'bigdecimal'
require 'date'
require 'thread_safe'

module Arel
module Visitors
Expand Down Expand Up @@ -56,8 +57,8 @@ class ToSql < Arel::Visitors::Visitor
def initialize connection
@connection = connection
@schema_cache = connection.schema_cache
@quoted_tables = {}
@quoted_columns = {}
@quoted_tables = ThreadSafe::Cache.new
@quoted_columns = ThreadSafe::Cache.new
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Visitor instances aren't shared among threads, so I don't think we don't need these. (Side note: I'm not sure that those caches are useful anyway)

end

private
Expand Down
8 changes: 5 additions & 3 deletions lib/arel/visitors/visitor.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'thread_safe'

module Arel
module Visitors
class Visitor
Expand All @@ -7,10 +9,10 @@ def accept object

private

DISPATCH = Hash.new do |hash, visitor_class|
DISPATCH = ThreadSafe::Cache.new do |hash, visitor_class|
hash[visitor_class] =
Hash.new do |hash, node_class|
hash[node_class] = "visit_#{(node_class.name || '').gsub('::', '_')}"
ThreadSafe::Cache.new(:initial_capacity => 2) do |inner_hash, node_class|
inner_hash[node_class] = "visit_#{(node_class.name || '').gsub('::', '_')}"
end
end

Expand Down