Skip to content

Commit d6eb168

Browse files
Merge pull request #23158 from aschwaighofer/reduce_time_string_memory_test
Reduce the time spend in StringMemoryTest
2 parents ae189ef + 8cc813d commit d6eb168

File tree

1 file changed

+33
-16
lines changed

1 file changed

+33
-16
lines changed

validation-test/stdlib/StringMemoryTest.swift

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ import Foundation
1212
let str = "abcdefg\u{A758}hijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz\u{A759}"
1313
let str2 = "abcdefg\u{A759}hijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz\u{A758}"
1414

15+
16+
@inline(never)
17+
func getMemoryUsage() -> Int {
18+
var usage = rusage()
19+
getrusage(RUSAGE_SELF, &usage)
20+
return usage.ru_maxrss
21+
}
22+
1523
@inline(never)
1624
func lookup(_ str: String, _ dict: [String: Int]) -> Bool {
1725
if let _ = dict[str] {
@@ -30,35 +38,44 @@ func lowercase(_ str: String) -> String {
3038
return str.lowercased()
3139
}
3240

41+
@inline(never)
42+
func runTest() {
43+
for _ in 0 ..< 10_0000 {
44+
if lookup("\u{1F1E7}\u{1F1E7}", dict) {
45+
print("Found?!")
46+
}
47+
if uppercase(str) == "A" {
48+
print("Found?!")
49+
}
50+
if lowercase(str2) == "A" {
51+
print("Found?!")
52+
}
53+
}
54+
}
55+
56+
3357
/// Make sure the hash function does not leak.
3458

3559
let dict = [ "foo" : 1]
36-
for _ in 0 ..< 10_000_000 {
37-
if lookup("\u{1F1E7}\u{1F1E7}", dict) {
38-
print("Found?!")
39-
}
40-
if uppercase(str) == "A" {
41-
print("Found?!")
42-
}
43-
if lowercase(str2) == "A" {
44-
print("Found?!")
45-
}
46-
}
60+
61+
let baseUsage = getMemoryUsage()
62+
runTest()
63+
let firstRun = getMemoryUsage () - baseUsage
64+
runTest()
65+
runTest()
66+
let secondRun = getMemoryUsage () - baseUsage
4767

4868
// CHECK-NOT: Found?!
4969
// CHECK: Not found
5070

5171
print("Not found")
5272

53-
var usage = rusage()
54-
getrusage(RUSAGE_SELF, &usage)
55-
5673
// CHECK: success
5774
// CHECK-NOT: failure
5875

5976
// We should not need 50MB for this.
60-
if usage.ru_maxrss > 50 * 1024 * 1024 {
61-
print("failure - should not need 50MB!")
77+
if firstRun * 2 < secondRun {
78+
print("failure - should not linearly increase")
6279
} else {
6380
print("success")
6481
}

0 commit comments

Comments
 (0)