Skip to content

Commit

Permalink
Ensure toArray(TYPE::new) is not used as it's less efficient than toA…
Browse files Browse the repository at this point in the history
…rray(TYPE.EMPTY_ARRAY)
  • Loading branch information
eregon committed Jul 31, 2023
1 parent f91c002 commit c1dde60
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
10 changes: 7 additions & 3 deletions src/main/.checkstyle_checks.xml
Original file line number Diff line number Diff line change
Expand Up @@ -195,15 +195,19 @@
</module>
<module name="RegexpSinglelineJava">
<property name="format" value='\.toArray\(new \w+\[\w+\.size\(\)\]\)'/>
<property name="message" value='Use toArray(new Type[0]) instead.'/>
<property name="message" value='Use toArray(Type.EMPTY_ARRAY) instead.'/>
</module>
<module name="RegexpSinglelineJava">
<property name="format" value='new\s+\w+\[\]\s*\{\s*\}'/>
<property name="message" value='Use new Type[0] instead.'/>
<property name="message" value='Use EMPTY_ARRAY = new Type[0] instead.'/>
</module>
<module name="RegexpSinglelineJava">
<property name="format" value='(?!=)\S\s*new\s+\w+\[0\]'/>
<property name="message" value='Use a EMPTY_type_ARRAY constant instead.'/>
<property name="message" value='Use a Type.EMPTY_ARRAY or EMPTY_type_ARRAY constant instead.'/>
</module>
<module name="RegexpSinglelineJava">
<property name="format" value='.toArray\(.+\[\]::new\)'/>
<property name="message" value='Use .toArray(Type.EMPTY_ARRAY) instead, because this avoids extra allocations.'/>
</module>
<module name="RegexpSinglelineJava">
<property name="format" value="transferToInterpreterOnException"/>
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/truffleruby/parser/ast/ArgsParseNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import java.util.Arrays;
import java.util.List;

import org.truffleruby.core.string.StringUtils;
import org.truffleruby.language.SourceIndexLength;
import org.truffleruby.language.methods.Arity;
import org.truffleruby.parser.Helpers;
Expand Down Expand Up @@ -153,7 +154,7 @@ private Arity createArity() {
final List<String> keywords = new ArrayList<>(requiredKeywords);
keywords.addAll(optionalKeywords);

keywordArguments = keywords.toArray(String[]::new);
keywordArguments = keywords.toArray(StringUtils.EMPTY_STRING_ARRAY);
requiredKeywordArgumentsCount = requiredKeywords.size();
} else {
keywordArguments = Arity.NO_KEYWORDS;
Expand Down

0 comments on commit c1dde60

Please sign in to comment.