title: Serious performance issues with Java SAX
body: <p>I'm building an application that converts an SVG directly to a PDF. Because the SVGs can get quite large and they can be handled linearly, I decided to implement the XML parser with SAX. However, I am not getting the proper performance from the parser: It takes 20 seconds to handle a 45KB SVG file.
  <p>
  Profiling reveals that the CPU hog is in XMLParser's parse method. More specifically, it's taking all this time not to process the data, but just to read it in. It goes all the way down to java.net.SocketInputStream.socketRead0, where the CPU is spending 19 seconds.
  <p>
  Has anyone else had this problem? Does anyone know how to fix it?
  <p>
  The driver I'm using:</p>
  
  <pre><code>// Initialize SAX components
  SAXParserFactory spf = SAXParserFactory.newInstance();
  SAXParser saxParser = spf.newSAXParser();
  XMLReader xmlReader = saxParser.getXMLReader();
  
  // Set System L&amp;F
  UIManager.setLookAndFeel(
          UIManager.getSystemLookAndFeelClassName());
  
  // Create a new file chooser
  JFileChooser fileChooser = new JFileChooser();
  fileChooser.setFileFilter(
          new FileNameExtensionFilter("SVG file", "svg"));
  
  // Let the user choose an SVG file and convert it
  if (fileChooser.showDialog(null, "Convert")
          == JFileChooser.APPROVE_OPTION) {
      File svgInput = fileChooser.getSelectedFile();
      File pdfOutput =
              new File(svgInput.getPath().replace(".svg", ".pdf"));
      xmlReader.setContentHandler(new SVGToPDFConverter(pdfOutput));
      URL inputURL = new URL(svgInput.toURI().toString());
      System.out.println("Working...");
  
      // Parse the file
      try (InputStream inputStream = inputURL.openStream()) {
          xmlReader.parse(new InputSource(inputStream));
      }
  
      System.out.println("Done!");
  }
  </code></pre>
  
  <p>The start of the SVG file I've been testing with:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd"&gt;
  
  &lt;svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="fill-opacity:1; color-rendering:auto; color-interpolation:auto; stroke:black; text-rendering:auto; stroke-linecap:square; stroke-miterlimit:10; stroke-opacity:1; shape-rendering:auto; fill:black; stroke-dasharray:none; font-weight:normal; stroke-width:1; font-family:'Dialog'; font-style:normal; stroke-linejoin:miter; font-size:12; stroke-dashoffset:0; image-rendering:auto;" preserveAspectRatio="xMidYMid meet" zoomAndPan="magnify" version="1.0" contentScriptType="text/ecmascript" contentStyleType="text/css"&gt;
    &lt;!--Generated by the Batik Graphics2D SVG Generator--&gt;
    &lt;defs id="genericDefs" /&gt;
    &lt;g&gt;
      &lt;g style="font-size:14; fill:white; text-rendering:optimizeLegibility; color-rendering:optimizeQuality; image-rendering:optimizeQuality; font-family:'Calibri'; color-interpolation:linearRGB; stroke:white; font-weight:bold;"&gt;
        &lt;rect x="68" width="54" height="17" y="2" style="stroke:none;" /&gt;
        &lt;text x="73.4" xml:space="preserve" y="15" style="fill:black; stroke:none;"&gt;Dates (Ma)&lt;/text&gt;
        &lt;rect x="447" width="40" height="17" y="2" style="stroke:none;" /&gt;
        &lt;text x="452.7" xml:space="preserve" y="15" style="fill:black; stroke:none;"&gt;Composition&lt;/text&gt;
        &lt;rect x="604" width="54" height="17" y="2" style="stroke:none;" /&gt;
        &lt;text x="609.5001" xml:space="preserve" y="15" style="fill:black; stroke:none;"&gt;Isotopic Ratios&lt;/text&gt;
        &lt;text x="5" xml:space="preserve" y="32" style="fill:black; stroke:none;" /&gt;
        &lt;text x="5" xml:space="preserve" y="43" style="fill:black; stroke:none;" /&gt;
        &lt;text x="5" xml:space="preserve" y="54" style="fill:black; stroke:none;"&gt;Fraction  &lt;/text&gt;
        &lt;line x1="70" x2="70" y1="18" style="fill:none; stroke:gray; stroke-width:0.5;" y2="76" /&gt;
        &lt;text x="73.4" y="32" style="fill:black; stroke-width:0.5; stroke:none;" xml:space="preserve" /&gt;
        &lt;text x="73.4" y="43" style="fill:black; stroke-width:0.5; stroke:none;" xml:space="preserve"&gt;206Pb/&lt;/text&gt;
        &lt;text x="73.4" y="54" style="fill:black; stroke-width:0.5; stroke:none;" xml:space="preserve"&gt;238U&lt;/text&gt;
      &lt;/g&gt;
  </code></pre>