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

Cache symbols while tokenizing. #92

Merged
merged 1 commit into from Oct 13, 2012
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/psych/scalar_scanner.rb
Expand Up @@ -16,12 +16,14 @@ class ScalarScanner
# Create a new scanner # Create a new scanner
def initialize def initialize
@string_cache = {} @string_cache = {}
@symbol_cache = {}
end end


# Tokenize +string+ returning the ruby object # Tokenize +string+ returning the ruby object
def tokenize string def tokenize string
return nil if string.empty? return nil if string.empty?
return string if @string_cache.key?(string) return string if @string_cache.key?(string)
return @symbol_cache[string] if @symbol_cache.key?(string)


case string case string
# Check for a String type, being careful not to get caught by hash keys, hex values, and # Check for a String type, being careful not to get caught by hash keys, hex values, and
Expand Down Expand Up @@ -67,9 +69,9 @@ def tokenize string
0.0 / 0.0 0.0 / 0.0
when /^:./ when /^:./
if string =~ /^:(["'])(.*)\1/ if string =~ /^:(["'])(.*)\1/
$2.sub(/^:/, '').to_sym @symbol_cache[string] = $2.sub(/^:/, '').to_sym
else else
string.sub(/^:/, '').to_sym @symbol_cache[string] = string.sub(/^:/, '').to_sym
end end
when /^[-+]?[0-9][0-9_]*(:[0-5]?[0-9])+$/ when /^[-+]?[0-9][0-9_]*(:[0-5]?[0-9])+$/
i = 0 i = 0
Expand Down