Skip to content

Commit 4b9a371

Browse files
committed
[GR-47996] [GR-45678] Implement performance warnings (Warning[:performance])
1 parent 2f628e5 commit 4b9a371

File tree

17 files changed

+76
-7
lines changed

17 files changed

+76
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Compatibility:
3939
* Add `Enumerator#product` (#3039, @itarato).
4040
* Add `Module#const_added` (#3039, @itarato).
4141
* Show the pointer size information (if available) in `FFI::Pointer#inspect` (@nirvdrum).
42+
* Implement performance warnings (`Warning[:performance]`) like in CRuby 3.3 (@eregon).
4243

4344
Performance:
4445

spec/ruby/core/warning/element_reference_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
describe "Warning.[]" do
44
ruby_version_is '2.7.2' do
55
it "returns default values for categories :deprecated and :experimental" do
6-
ruby_exe('p Warning[:deprecated]').chomp.should == "false"
7-
ruby_exe('p Warning[:experimental]').chomp.should == "true"
6+
ruby_exe('p [Warning[:deprecated], Warning[:experimental]]').chomp.should == "[false, true]"
7+
ruby_exe('p [Warning[:deprecated], Warning[:experimental]]', options: "-w").chomp.should == "[true, true]"
88
end
99
end
1010

spec/ruby/core/warning/element_set_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@
1717
end
1818
end
1919

20+
ruby_version_is '3.3' do
21+
it "enables or disables performance warnings" do
22+
original = Warning[:performance]
23+
begin
24+
Warning[:performance] = !original
25+
Warning[:performance].should == !original
26+
ensure
27+
Warning[:performance] = original
28+
end
29+
end
30+
end
31+
2032
it "raises for unknown category" do
2133
-> { Warning[:noop] = false }.should raise_error(ArgumentError, /unknown category: noop/)
2234
end
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
slow:Warning.[] returns default values for categories :deprecated and :experimental
2+
slow:Warning.[] returns default values for :performance category

spec/truffleruby.next-specs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@
99
# Use spec/ruby/core/nil/nil_spec.rb as a dummy file to avoid being empty (what causes mspec to error)
1010
spec/ruby/core/nil/nil_spec.rb
1111

12+
spec/ruby/core/warning/element_reference_spec.rb
13+
spec/ruby/core/warning/element_set_spec.rb

src/launcher/java/org/truffleruby/launcher/CommandLineParser.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,12 @@ private void processArgument() throws CommandLineException {
361361
case ":no-experimental":
362362
config.setOption(OptionsCatalog.WARN_EXPERIMENTAL, false);
363363
break;
364+
case ":performance":
365+
config.setOption(OptionsCatalog.WARN_PERFORMANCE, true);
366+
break;
367+
case ":no-performance":
368+
config.setOption(OptionsCatalog.WARN_PERFORMANCE, false);
369+
break;
364370
default:
365371
LOGGER.warning("unknown warning category: `" + temp.substring(1) + "'");
366372
break;
@@ -494,6 +500,7 @@ private void processArgument() throws CommandLineException {
494500
private void setAllWarningCategories(boolean value) {
495501
config.setOption(OptionsCatalog.WARN_DEPRECATED, value);
496502
config.setOption(OptionsCatalog.WARN_EXPERIMENTAL, value);
503+
// WARN_PERFORMANCE is excluded here, it is not set by -w/-W2 on CRuby
497504
}
498505

499506
private void enableDisableFeature(String name, boolean enable) {

src/launcher/java/org/truffleruby/launcher/RubyLauncher.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,7 @@ private static void printHelp(PrintStream out) {
437437
out.println("Warning categories:");
438438
out.println(" deprecated deprecated features");
439439
out.println(" experimental experimental features");
440+
out.println(" performance performance issues");
440441
}
441442

442443
// Same as above, but with "ruby -h"

src/main/java/org/truffleruby/RubyContext.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ public final class RubyContext {
166166

167167
private final AssumedValue<Boolean> warningCategoryDeprecated;
168168
private final AssumedValue<Boolean> warningCategoryExperimental;
169+
private final AssumedValue<Boolean> warningCategoryPerformance;
169170

170171
private ImmutableRubyString mainScriptName;
171172

@@ -189,6 +190,7 @@ public RubyContext(RubyLanguage language, TruffleLanguage.Env env) {
189190

190191
warningCategoryDeprecated = new AssumedValue<>(options.WARN_DEPRECATED);
191192
warningCategoryExperimental = new AssumedValue<>(options.WARN_EXPERIMENTAL);
193+
warningCategoryPerformance = new AssumedValue<>(options.WARN_PERFORMANCE);
192194

193195
safepointManager = new SafepointManager(this);
194196
coreExceptions = new CoreExceptions(this, language);
@@ -305,6 +307,9 @@ protected boolean patch(Env newEnv) {
305307
if (newOptions.WARN_EXPERIMENTAL != oldOptions.WARN_EXPERIMENTAL) {
306308
warningCategoryExperimental.set(newOptions.WARN_EXPERIMENTAL);
307309
}
310+
if (newOptions.WARN_PERFORMANCE != oldOptions.WARN_PERFORMANCE) {
311+
warningCategoryPerformance.set(newOptions.WARN_PERFORMANCE);
312+
}
308313

309314
// Re-read the value of $TZ as it can be different in the new process
310315
GetTimeZoneNode.invalidateTZ();
@@ -758,6 +763,10 @@ public AssumedValue<Boolean> getWarningCategoryExperimental() {
758763
return warningCategoryExperimental;
759764
}
760765

766+
public AssumedValue<Boolean> getWarningCategoryPerformance() {
767+
return warningCategoryPerformance;
768+
}
769+
761770
public PrintStream getEnvOutStream() {
762771
return outStream;
763772
}

src/main/java/org/truffleruby/core/kernel/KernelNodes.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1851,6 +1851,11 @@ protected boolean getCategoryExperimental(RubySymbol category) {
18511851
return getContext().getWarningCategoryExperimental().get();
18521852
}
18531853

1854+
@Specialization(guards = "category == coreSymbols().PERFORMANCE")
1855+
protected boolean getCategoryPerformance(RubySymbol category) {
1856+
return getContext().getWarningCategoryPerformance().get();
1857+
}
1858+
18541859
}
18551860

18561861
@Primitive(name = "warning_set_category")
@@ -1864,6 +1869,8 @@ protected boolean setCategory(RubySymbol category, boolean newValue) {
18641869
existingValue = getContext().getWarningCategoryDeprecated();
18651870
} else if (category == coreSymbols().EXPERIMENTAL) {
18661871
existingValue = getContext().getWarningCategoryExperimental();
1872+
} else if (category == coreSymbols().PERFORMANCE) {
1873+
existingValue = getContext().getWarningCategoryPerformance();
18671874
} else {
18681875
throw CompilerDirectives.shouldNotReachHere("unexpected warning category");
18691876
}

src/main/java/org/truffleruby/core/symbol/CoreSymbols.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public final class CoreSymbols {
4040
public final RubySymbol ON_BLOCKING = createRubySymbol("on_blocking");
4141
public final RubySymbol DEPRECATED = createRubySymbol("deprecated");
4242
public final RubySymbol EXPERIMENTAL = createRubySymbol("experimental");
43+
public final RubySymbol PERFORMANCE = createRubySymbol("performance");
4344
public final RubySymbol BIG = createRubySymbol("big");
4445
public final RubySymbol LITTLE = createRubySymbol("little");
4546
public final RubySymbol NATIVE = createRubySymbol("native");

0 commit comments

Comments
 (0)