Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize leftpad on raw_html #526

Merged
merged 3 commits into from
Jan 12, 2024

Conversation

ypconstante
Copy link
Contributor

@ypconstante ypconstante commented Jan 11, 2024

Today leftpad calls String.duplicate, which ends up being significantly costly when generating a pretty raw html, since this is used on each line being generated.

This PR moves the string creation to pad_increase, instead of doing it on every call to leftpad.

##### With input big #####
Name                 ips        average  deviation         median         99th %
bench (pr)         35.87       27.88 ms    ±11.34%       27.06 ms       41.01 ms
bench              33.05       30.26 ms     ±6.39%       30.00 ms       34.48 ms

Comparison: 
bench (pr)         35.87
bench              33.05 - 1.09x slower +2.38 ms

Memory usage statistics:

Name          Memory usage
bench (pr)        12.68 MB
bench             13.83 MB - 1.09x memory usage +1.15 MB

**All measurements for memory usage were the same**

##### With input medium #####
Name                 ips        average  deviation         median         99th %
bench (pr)        133.80        7.47 ms    ±17.97%        7.08 ms       10.64 ms
bench             117.21        8.53 ms    ±16.18%        8.29 ms       11.99 ms

Comparison: 
bench (pr)        133.80
bench             117.21 - 1.14x slower +1.06 ms

Memory usage statistics:

Name          Memory usage
bench (pr)         4.14 MB
bench              4.49 MB - 1.09x memory usage +0.36 MB

**All measurements for memory usage were the same**

##### With input small #####
Name                 ips        average  deviation         median         99th %
bench (pr)        749.69        1.33 ms    ±25.91%        1.30 ms        2.17 ms
bench             648.79        1.54 ms    ±23.00%        1.46 ms        2.48 ms

Comparison: 
bench (pr)        749.69
bench             648.79 - 1.16x slower +0.21 ms

Memory usage statistics:

Name          Memory usage
bench (pr)       891.88 KB
bench            960.57 KB - 1.08x memory usage +68.69 KB

**All measurements for memory usage were the same**
read_file = fn name ->
  __ENV__.file
  |> Path.dirname()
  |> Path.join(name)
  |> File.read!()
  |> Floki.parse_document!()
end

inputs = %{
  "big" => read_file.("big.html"),
  "medium" => read_file.("medium.html"),
  "small" => read_file.("small.html")
}

Benchee.run(
  %{
    "bench" => fn doc -> Floki.raw_html(doc, pretty: true) end
  },
  time: 10,
  inputs: inputs,
  memory_time: 2
)

@philss philss merged commit b01543a into philss:main Jan 12, 2024
9 checks passed
@philss
Copy link
Owner

philss commented Jan 12, 2024

@ypconstante Thank you again! :D

@ypconstante ypconstante deleted the optimize-pretty-raw-html branch February 11, 2024 02:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants