diff Implab/Xml/XmlNameContext.cs @ 229:5f7a3e1d32b9 v2

JsonXmlReader performance tuning JsonScanner now operates strings and doesn't parses number and literals. Added SerializationHelpers to common serialize/deserialize operations
author cin
date Tue, 12 Sep 2017 19:07:42 +0300
parents 8d5de4eb9c2c
children
line wrap: on
line diff
--- a/Implab/Xml/XmlNameContext.cs	Tue Sep 12 01:19:12 2017 +0300
+++ b/Implab/Xml/XmlNameContext.cs	Tue Sep 12 19:07:42 2017 +0300
@@ -6,7 +6,7 @@
 using System.Xml;
 
 namespace Implab.Xml {
-    public class XmlNameContext {
+    class XmlNameContext {
         public const string XmlnsNamespace = "http://www.w3.org/2000/xmlns/";
         public const string XmlnsPrefix = "xmlns";
         public const string XmlNamespace = "http://www.w3.org/XML/1998/namespace";
@@ -19,11 +19,17 @@
         Dictionary<string, string> m_ns2prefix;
         Dictionary<string, string> m_prefix2ns;
         int m_nextPrefix = 1;
+        string m_lastNs;
+        string m_lastPrefix;
         
         public XmlNameContext ParentContext { get; private set; }
 
-        public XmlNameContext(XmlNameContext parent) {
+        public int Depth { get; private set; }
+
+        public XmlNameContext(XmlNameContext parent, int depth) {
             ParentContext = parent;
+            Depth = depth;
+
             if (parent == null) {
                 DefinePrefixNoCheck(XmlnsNamespace, XmlnsPrefix);
                 DefinePrefixNoCheck(XmlNamespace, XmlPrefix);
@@ -35,12 +41,17 @@
         public bool LookupNamespacePrefix(string ns, out string prefix) {
             if (ns == null)
                 ns = string.Empty;
+            if (ns == m_lastNs) {
+                prefix = m_lastPrefix;
+                return true;
+            }
+                
 
             prefix = null;
             for (var ctx = this; ctx != null; ctx = ctx.ParentContext) {
-                if (ctx.m_ns2prefix?.TryGetValue(ns, out prefix) == true) {
-                    if (ctx != this) // cache for the future use
-                        DefinePrefixNoCheck(ns, prefix);
+                if (ctx.m_ns2prefix != null && ctx.m_ns2prefix.TryGetValue(ns, out prefix)) {
+                    m_lastNs = ns;
+                    m_lastPrefix = prefix;
                     return true;
                 }
             }
@@ -88,11 +99,8 @@
                 prefix = string.Empty;
             string ns = null;
             for(var ctx = this; ctx != null; ctx = ctx.ParentContext) {
-                if (ctx.m_prefix2ns?.TryGetValue(prefix, out ns) == true) {
-                    if (ctx != this) // cache for the future use
-                        DefinePrefixNoCheck(ns, prefix);
+                if (ctx.m_prefix2ns != null && ctx.m_prefix2ns.TryGetValue(prefix, out ns) == true)
                     return ns;
-                }
             }
             return null;
         }