Skip to content

Remove unnecessary explicit receiver in Html2Text.convert#30

Merged
mscrivo merged 1 commit into
soundasleep:masterfrom
splattael:allow-subclassig
Jun 7, 2024
Merged

Remove unnecessary explicit receiver in Html2Text.convert#30
mscrivo merged 1 commit into
soundasleep:masterfrom
splattael:allow-subclassig

Conversation

@splattael

Copy link
Copy Markdown
Contributor

This makes subclassing Html2Text easier as convert does not have to be reimplemented.

This makes subclassing Html2Text easier as `convert` does not have to be
reimplemented.
@mscrivo
mscrivo merged commit 4b91b5c into soundasleep:master Jun 7, 2024
mscrivo added a commit that referenced this pull request Jul 8, 2026
Profiling Html2Text.convert on the 1.4MB huge-msoffice fixture showed
GC accounting for 41% of wall time, driven by 1.37M object allocations
per convert — mostly node.name.downcase being recomputed ~19 times per
node across iterate_over, prefix_whitespace, suffix_whitespace and
next_node_name, plus per-node Array/map/compact/join churn.

Changes, all behavior-preserving (output is byte-identical on every
spec fixture):

- iterate_over computes node.name.downcase once per node and reuses it
- children are appended into a single mutable string instead of
  building an Array, mapping, compacting and joining per node
- suffix_whitespace calls next_node_name once instead of twice for
  br/div nodes
- merged duplicate h1-h6/p case branches, removing the need for the
  Lint/DuplicateBranch disables
- dropped no-op /im regex flags and simplified /\n\n\n*/ to /\n{3,}/

Method signatures and return types are unchanged so subclasses that
override these methods (supported since #30) are unaffected; new specs
pin those extension points, and were verified to pass against the old
implementation as well.

Benchmarks (Ruby 4.0.5, arm64 macOS):

  huge-msoffice (1.4MB) x5:  0.852s -> 0.522s (1.63x)
  full_email (15KB) x500:    0.340s -> 0.269s (1.26x)
  allocations (1x 1.4MB):    1,367,401 -> 642,896 (2.1x fewer)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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