   ;; TODO - this function is way too busy
   (let [{:keys [namespaces]} opts
 
+        ;; Memoize the metadata maps
+        md-map-with-prefix
+        (let [cache (atom nil)]
+          (fn [md-map prefix]
+            (if-let [m (get-in @cache [md-map prefix])]
+              m
+              (let [new-map (assoc md-map :prefix prefix)]
+                (swap! cache assoc-in [md-map prefix] new-map)
+                new-map))))
+        
         make-element
         (if namespaces
-          (fn [[_ uri local-name q-name attrs] content ns-map]
+          (fn [[_ uri local-name q-name attrs] content md-map]
             (let [[prefix tag] (string/split q-name #":" 2)
                   prefix (if tag prefix nil)]
               (with-meta

      (.write "<")
      (.write tagname))
    (doseq [[k v] attrs]
      (if-not (nil? v)
        (doto out
          (.write " ")
          (.write (name k))
          (.write "='")
          (.write (xml-escape-attr (str v)))
          (.write "'"))))
    (if (empty? content)
      (.write out "/>")
      (do

  [in & [opts]]
  (let [{:keys [namespaces]} opts
        ^InputSource in (as-input-source in)
        parser          (XMLReaderFactory/createXMLReader) ;TODO - make this pluggable
        [put event-seq] (pipe/pipe {:capacity 4096})
        handler         (sax-handler put opts)]
    
