Skip to content

Commit f00920b

Browse files
committed
Make some changes suggested by Flog
1 parent 23f3a4a commit f00920b

File tree

4 files changed

+241
-197
lines changed

4 files changed

+241
-197
lines changed

doc/contributor/static-analysis.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ occasionally run this tool locally but only take its output as a suggestion.
9191

9292
```
9393
$ gem install flog
94-
$ flog lib/truffle lib/cext src/main
94+
$ flog -m -t 10 lib/truffle lib/cext src/main
9595
```
9696

9797
### Flay

src/main/ruby/core/array.rb

Lines changed: 47 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,70 +1042,76 @@ def sample(count=undefined, options=undefined)
10421042
end
10431043
return [at(i), at(j)]
10441044
else
1045-
if size / count > 3
1046-
abandon = false
1045+
sample_many(count, rng)
1046+
end
1047+
end
10471048

1048-
result = Array.new count
1049-
i = 1
1049+
def sample_many(count, rng)
1050+
if size / count > 3
1051+
abandon = false
10501052

1051-
result[0] = rng.rand(size)
1052-
while i < count
1053-
k = rng.rand(size)
1053+
result = Array.new count
1054+
i = 1
10541055

1055-
spin = false
1056-
spin_count = 0
1056+
result[0] = rng.rand(size)
1057+
while i < count
1058+
k = rng.rand(size)
10571059

1058-
while true # rubocop:disable Lint/LiteralInCondition
1059-
j = 0
1060-
while j < i
1061-
if k == result[j]
1062-
spin = true
1063-
break
1064-
end
1060+
spin = false
1061+
spin_count = 0
10651062

1066-
j += 1
1063+
while true # rubocop:disable Lint/LiteralInCondition
1064+
j = 0
1065+
while j < i
1066+
if k == result[j]
1067+
spin = true
1068+
break
10671069
end
10681070

1069-
if spin
1070-
if (spin_count += 1) > 100
1071-
abandon = true
1072-
break
1073-
end
1071+
j += 1
1072+
end
10741073

1075-
k = rng.rand(size)
1076-
else
1074+
if spin
1075+
if (spin_count += 1) > 100
1076+
abandon = true
10771077
break
10781078
end
1079+
1080+
k = rng.rand(size)
1081+
else
1082+
break
10791083
end
1084+
end
10801085

1081-
break if abandon
1086+
break if abandon
10821087

1083-
result[i] = k
1088+
result[i] = k
10841089

1090+
i += 1
1091+
end
1092+
1093+
unless abandon
1094+
i = 0
1095+
while i < count
1096+
result[i] = at result[i]
10851097
i += 1
10861098
end
10871099

1088-
unless abandon
1089-
i = 0
1090-
while i < count
1091-
result[i] = at result[i]
1092-
i += 1
1093-
end
1094-
1095-
return result
1096-
end
1100+
return result
10971101
end
1102+
end
10981103

1099-
result = Array.new(self)
1100-
1101-
count.times do |c|
1102-
result.swap c, rng.rand(size)
1103-
end
1104+
result = Array.new(self)
11041105

1105-
return count == size ? result : result[0, count]
1106+
count.times do |c|
1107+
result.swap c, rng.rand(size)
11061108
end
1109+
1110+
return count == size ? result : result[0, count]
11071111
end
11081112

1113+
private :sample_many
1114+
11091115
def select!(&block)
11101116
return to_enum(:select!) { size } unless block_given?
11111117

src/main/ruby/core/io.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,6 +1065,18 @@ def self.select(readables=nil, writables=nil, errorables=nil, timeout=nil)
10651065
end
10661066
}
10671067

1068+
do_select(
1069+
readables, writables, errorables,
1070+
mark_ready, to_fds,
1071+
original_timeout, timeout_us,
1072+
readables_ready, writables_ready, errorables_ready)
1073+
end
1074+
1075+
def self.do_select(
1076+
readables, writables, errorables,
1077+
mark_ready, to_fds,
1078+
original_timeout, timeout_us,
1079+
readables_ready, writables_ready, errorables_ready)
10681080
Truffle::POSIX.with_array_of_ints(to_fds.call(readables)) do |readables_ptr|
10691081
Truffle::POSIX.with_array_of_ints(to_fds.call(writables)) do |writables_ptr|
10701082
Truffle::POSIX.with_array_of_ints(to_fds.call(errorables)) do |errorables_ptr|
@@ -1110,6 +1122,8 @@ def self.select(readables=nil, writables=nil, errorables=nil, timeout=nil)
11101122
end
11111123
end
11121124

1125+
private_class_method :do_select
1126+
11131127
##
11141128
# Opens the given path, returning the underlying file descriptor as an Integer.
11151129
# IO.sysopen("testfile") #=> 3

0 commit comments

Comments
 (0)