@@ -75,6 +75,7 @@ def self.xml_to_hash(node, hash = {}, opts = {})
75
75
node_hash = { CONTENT_ROOT => '' , ATTR_ROOT => { } }
76
76
name = node . name
77
77
schema = opts [ :schema ]
78
+ opts [ :typecast_cache ] ||= { }
78
79
79
80
# Insert node hash into parent hash correctly.
80
81
case hash [ name ]
@@ -105,7 +106,7 @@ def self.xml_to_hash(node, hash = {}, opts = {})
105
106
node_hash . delete ( ATTR_ROOT )
106
107
hash [ name ] = \
107
108
if schema
108
- typecast ( schema , node . path , node_hash [ CONTENT_ROOT ] )
109
+ typecast ( schema , node . path , node_hash [ CONTENT_ROOT ] , opts [ :typecast_cache ] )
109
110
else
110
111
node_hash [ CONTENT_ROOT ]
111
112
end
@@ -119,9 +120,10 @@ def self.xml_to_hash(node, hash = {}, opts = {})
119
120
120
121
private
121
122
122
- def self . typecast ( schema , xpath , value )
123
+ def self . typecast ( schema , xpath , value , typecast_cache )
123
124
type_path = xpath . gsub ( /\[ \d +\] / , '' )
124
- type_proc = Qbxml ::TYPE_MAP [ schema . xpath ( type_path ) . first . try ( :text ) ]
125
+ # This is fairly expensive. Cache it for better performance when parsing lots of records of the same type.
126
+ type_proc = typecast_cache [ type_path ] ||= Qbxml ::TYPE_MAP [ schema . xpath ( type_path ) . first . try ( :text ) ]
125
127
raise "#{ xpath } is not a valid type" unless type_proc
126
128
type_proc [ value ]
127
129
end
0 commit comments