Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Improve the performance using the hints by rubocop-performance and fasterer

未关闭
#33 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

@anew-bhav 已经在做这个了。

开始于 2021年5月31日。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
38/100
Issue 类型
重构
描述清晰度
基本清楚
活跃度
停滞
技术栈
ruby
领域
performance

调研方向

从 rubocop-performance 和 fasterer 报告中列出的文件和行位置开始,尤其关注 parser、XPath、quickpath、node、element 和 doctype 文件。对项目运行这两个工具,并检查每项建议的更改是否保持行为不变。当报告的违规项得到处理且现有检查仍然通过时,即视为完成。

由索引模型根据 Issue 内容生成。

描述

rubocop-performance (see https://github.com/rubocop-hq/rubocop-performance ) says:

Offenses:

/usr/lib/ruby/2.7.0/rexml/attlistdecl.rb:45:7: C: Performance/InefficientHashSearch: Use #key? instead of #keys.include?.
      @pairs.keys.include? key
      ^^^^^^^^^^^^^^^^^^^^^^^^
/usr/lib/ruby/2.7.0/rexml/doctype.rb:201:7: C: Performance/RegexpMatch: Use match? instead of =~ when MatchData is not used.
      quoted_string =~ /^[\'\"].*[\'\"]$/ ?
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
/usr/lib/ruby/2.7.0/rexml/element.rb:255:43: C: Performance/RegexpMatch: Use match? instead of =~ when MatchData is not used.
        prefix = "xmlns:#{prefix}" unless prefix =~ /^xmlns:/
                                          ^^^^^^^^^^^^^^^^^^^
/usr/lib/ruby/2.7.0/rexml/entity.rb:44:12: C: Performance/RegexpMatch: Use match? instead of =~ when MatchData is not used.
        if stream[2] =~ /SYSTEM|PUBLIC/
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
/usr/lib/ruby/2.7.0/rexml/node.rb:56:9: C: Performance/RedundantBlockCall: Use yield instead of block.call.
        block.call(node)
        ^^^^^^^^^^^^^^^^
/usr/lib/ruby/2.7.0/rexml/node.rb:65:24: C: Performance/RedundantBlockCall: Use yield instead of block.call.
        return node if block.call(node)
                       ^^^^^^^^^^^^^^^^
/usr/lib/ruby/2.7.0/rexml/parseexception.rb:32:67: C: Performance/StringReplacement: Use tr instead of gsub.
        err << @source.buffer[0..80].force_encoding("ASCII-8BIT").gsub(/\n/, ' ')
                                                                  ^^^^^^^^^^^^^^^
/usr/lib/ruby/2.7.0/rexml/parsers/baseparser.rb:476:25: C: Performance/RegexpMatch: Use match? instead of =~ when MatchData is not used.
        return false if /\AUTF-16\z/i =~ xml_declaration_encoding
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
/usr/lib/ruby/2.7.0/rexml/parsers/pullparser.rb:73:29: C: Performance/RegexpMatch: Use match? instead of =~ when MatchData is not used.
            event[2] unless event[2] =~ /PUBLIC|SYSTEM/
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^
/usr/lib/ruby/2.7.0/rexml/parsers/treeparser.rb:81:54: C: Performance/RegexpMatch: Use match? instead of =~ when MatchData is not used.
              entities[ event[1] ] = event[2] unless event[2] =~ /PUBLIC|SYSTEM/
                                                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^
/usr/lib/ruby/2.7.0/rexml/parsers/xpathparser.rb:304:14: C: Performance/RegexpMatch: Use match? instead of !~ when MatchData is not used.
          if path !~ /^\s*\)/
             ^^^^^^^^^^^^^^^^
/usr/lib/ruby/2.7.0/rexml/parsers/xpathparser.rb:539:41: C: Performance/RegexpMatch: Use match? instead of =~ when MatchData is not used.
        rest = LocationPath(rest, n) if rest =~ /\A[\/\.\@\[\w*]/
                                        ^^^^^^^^^^^^^^^^^^^^^^^^^
/usr/lib/ruby/2.7.0/rexml/quickpath.rb:108:49: C: Performance/RegexpMatch: Use match? instead of =~ when MatchData is not used.
      matches = filter( elements.dup, rest ) if axe_name =~ /-or-self$/u
                                                ^^^^^^^^^^^^^^^^^^^^^^^^

49 files inspected, 13 offenses detected


fasterer (see https://github.com/DamirSvrtan/fasterer) says:

validation/relaxng.rb:416 Calling argumentless methods within blocks is slower than using symbol to proc.
validation/relaxng.rb:523 Calling argumentless methods within blocks is slower than using symbol to proc.

parsers/xpathparser.rb:602 For loop is slower than using each.

parsers/baseparser.rb:381 For loop is slower than using each.

formatters/default.rb:70 Calling argumentless methods within blocks is slower than using symbol to proc.

xpath_parser.rb:105 For loop is slower than using each.
xpath_parser.rb:120 For loop is slower than using each.
xpath_parser.rb:458 Enumerable#sort is slower than Enumerable#sort_by.
xpath_parser.rb:590 Using each_with_index is slower than while loop.

quickpath.rb:88 Calling argumentless methods within blocks is slower than using symbol to proc.
quickpath.rb:133 Calling argumentless methods within blocks is slower than using symbol to proc.
quickpath.rb:135 Calling argumentless methods within blocks is slower than using symbol to proc.
quickpath.rb:138 Calling argumentless methods within blocks is slower than using symbol to proc.

node.rb:54 Calling blocks with call is slower than yielding.
node.rb:63 Calling blocks with call is slower than yielding.

functions.rb:42 Calling argumentless methods within blocks is slower than using symbol to proc.

element.rb:1068 Hash#fetch with second argument is slower than Hash#fetch with block.
element.rb:1075 Hash#fetch with second argument is slower than Hash#fetch with block.
element.rb:1128 Hash#fetch with second argument is slower than Hash#fetch with block.
element.rb:1212 Hash#fetch with second argument is slower than Hash#fetch with block.
element.rb:1246 Calling argumentless methods within blocks is slower than using symbol to proc.

doctype.rb:283 Use attr_reader for reading ivars.

49 files inspected, 22 offenses detected

主要语言
Ruby
星标
181
派生
105
平均合并
11 小时 3 分钟
30 天内合并 PR
1

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

ruby/rexml 的其他 Issue

查看 ruby/rexml 的全部 Issue

相似的 Issue

更多 Ruby Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。