Permalink
Browse files

Reimplemented string and symbol cache.

  • Loading branch information...
1 parent 93b6f3c commit 9e43eb467e5ef37cdb27f786b528e18caf26a73b @Fryguy committed Jan 14, 2013
Showing with 32 additions and 16 deletions.
  1. +16 −8 lib/psych/scalar_scanner.rb
  2. +16 −8 lib/psych/scalar_scanner.rl
@@ -311,8 +311,15 @@ class << self
# END OF SCANNER DATA STRUCTURES
###
+ def initialize
+ @string_cache = {}
+ @symbol_cache = {}
+ end
+
def tokenize string
return nil if string.empty?
+ return string if @string_cache.key?(string)
+ return @symbol_cache[string] if @symbol_cache.key?(string)
data = string_to_data(string)
eof = data.length
@@ -557,7 +564,7 @@ def parse_bool_false string
def parse_int_base_10 string
Integer(string.gsub(/[,_]/, ''))
rescue
- string
+ parse_string string
end
alias parse_int_base_2 parse_int_base_10
alias parse_int_base_8 parse_int_base_10
@@ -574,7 +581,7 @@ def parse_int_base_60 string
def parse_float_base_10 string
Float(string.gsub(/[,_]|\.$/, ''))
rescue
- string
+ parse_string string
end
def parse_float_base_60 string
@@ -601,7 +608,7 @@ def parse_time_ymd string
require 'date'
Date.strptime(string, '%Y-%m-%d')
rescue
- string
+ parse_string string
end
def parse_time_full string
@@ -628,24 +635,25 @@ def parse_time_full string
Time.at((time - offset).to_i, us)
rescue
- string
+ parse_string string
end
def parse_symbol_quoted string
last = string[-1]
return string unless last == '"' || last == "'"
- string[2..-2].to_sym
+ @symbol_cache[string] = string[2..-2].to_sym
rescue
- string
+ parse_string string
end
def parse_symbol_unquoted string
- string[1..-1].to_sym
+ @symbol_cache[string] = string[1..-1].to_sym
rescue
- string
+ parse_string string
end
def parse_string string
+ @string_cache[string] = true
string
end
end
@@ -96,8 +96,15 @@ module Psych
# END OF SCANNER DATA STRUCTURES
###
+ def initialize
+ @string_cache = {}
+ @symbol_cache = {}
+ end
+
def tokenize string
return nil if string.empty?
+ return string if @string_cache.key?(string)
+ return @symbol_cache[string] if @symbol_cache.key?(string)
data = string_to_data(string)
eof = data.length
@@ -162,7 +169,7 @@ module Psych
def parse_int_base_10 string
Integer(string.gsub(/[,_]/, ''))
rescue
- string
+ parse_string string
end
alias parse_int_base_2 parse_int_base_10
alias parse_int_base_8 parse_int_base_10
@@ -179,7 +186,7 @@ module Psych
def parse_float_base_10 string
Float(string.gsub(/[,_]|\.$/, ''))
rescue
- string
+ parse_string string
end
def parse_float_base_60 string
@@ -206,7 +213,7 @@ module Psych
require 'date'
Date.strptime(string, '%Y-%m-%d')
rescue
- string
+ parse_string string
end
def parse_time_full string
@@ -233,24 +240,25 @@ module Psych
Time.at((time - offset).to_i, us)
rescue
- string
+ parse_string string
end
def parse_symbol_quoted string
last = string[-1]
return string unless last == '"' || last == "'"
- string[2..-2].to_sym
+ @symbol_cache[string] = string[2..-2].to_sym
rescue
- string
+ parse_string string
end
def parse_symbol_unquoted string
- string[1..-1].to_sym
+ @symbol_cache[string] = string[1..-1].to_sym
rescue
- string
+ parse_string string
end
def parse_string string
+ @string_cache[string] = true
string
end
end

0 comments on commit 9e43eb4

Please sign in to comment.