Skip to content

Improve Performance with Nested CDATA #243

Description

@Masamuneee

I think we should improve the implementation to handle nested CDATA more efficiently.

require "rexml/document"

include REXML  

(1..1000000000).step(10000000) do |depth|
  puts "Trying depth #{depth}"
  string = "<?xml version=\"1.0\"?>\n" +
           "<root>Test</root>\n" +
           "<!" + "[CDATA[" * depth + "]]>\n"
  
  start = Time.now
  begin
    doc = Document.new(string)
  rescue Exception => e
    puts "Error at depth #{depth}: #{e}"
    break
  end
  elapsed_time = Time.now - start
  puts "Elapsed time: #{elapsed_time} seconds"
end

Result:

Elapsed time: 24.994068928 seconds
Trying depth 200000001
Elapsed time: 27.070803532 seconds
Trying depth 210000001
Elapsed time: 27.870487966 seconds
Trying depth 220000001
Elapsed time: 28.787843908 seconds
Trying depth 230000001
[1]    4336 killed     ruby test.rb

Activity

  1. added a commit that references this issue on Mar 1, 2025
    9f25332
  2. tompng commented on Mar 1, 2025

    @tompng
    Member

    I don't think it is a problem. Large data takes time. Not related to nested cdata.

    size = 1000000
    nested_cdata = "<?xml version=\"1.0\"?><root>Test</root><!" + "[CDATA[" * size + "]]>"
    normal_cdata = "<?xml version=\"1.0\"?><root>Test</root><![CDATA[" + 'aaaaaaa' * size + "]]>"
    normal_text = "<?xml version=\"1.0\"?><root>Test" + 'aaaaaaa' * size + "</root>"
    normal_tags = "<?xml version=\"1.0\"?><root>Test" + '<b></b>' * size + "</root>"
    
    # Size are almost the same.
    [nested_cdata, normal_cdata, normal_text, normal_tags].map(&:size)
    # => [7000043, 7000050, 7000038, 7000038]
    
    REXML::Document.new(nested_cdata);
    # processing time: 0.130978s
    REXML::Document.new(normal_cdata);
    # processing time: 0.109913s
    REXML::Document.new(normal_text);
    # processing time: 0.144391s
    REXML::Document.new(normal_tags);
    # processing time: 5.863289s

    The slowness is just caused by the input size. Time consumption of parsing nested cdata, non-nested normal cdata, normal text node is almost the same.

    Trying depth 220000001
    Elapsed time: 28.787843908 seconds
    # depth=1000000 takes 0.13s
    # estimated time 220000001/1000000 * 0.13 => 28.6
    
  3. added a commit that references this issue on Mar 2, 2025
    64a709e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions