Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 33 additions & 16 deletions validation-test/stdlib/StringMemoryTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ import Foundation
let str = "abcdefg\u{A758}hijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz\u{A759}"
let str2 = "abcdefg\u{A759}hijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz\u{A758}"


@inline(never)
func getMemoryUsage() -> Int {
var usage = rusage()
getrusage(RUSAGE_SELF, &usage)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This wont work on Windows, and should use GetProcessMemoryInfo there.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a new test - the PR just moves this code within the test.

I think this test is disabled on windows by requiring: // REQUIRES: objc_interop

return usage.ru_maxrss
}

@inline(never)
func lookup(_ str: String, _ dict: [String: Int]) -> Bool {
if let _ = dict[str] {
Expand All @@ -30,35 +38,44 @@ func lowercase(_ str: String) -> String {
return str.lowercased()
}

@inline(never)
func runTest() {
for _ in 0 ..< 10_0000 {
if lookup("\u{1F1E7}\u{1F1E7}", dict) {
print("Found?!")
}
if uppercase(str) == "A" {
print("Found?!")
}
if lowercase(str2) == "A" {
print("Found?!")
}
}
}


/// Make sure the hash function does not leak.

let dict = [ "foo" : 1]
for _ in 0 ..< 10_000_000 {
if lookup("\u{1F1E7}\u{1F1E7}", dict) {
print("Found?!")
}
if uppercase(str) == "A" {
print("Found?!")
}
if lowercase(str2) == "A" {
print("Found?!")
}
}

let baseUsage = getMemoryUsage()
runTest()
let firstRun = getMemoryUsage () - baseUsage
runTest()
runTest()
let secondRun = getMemoryUsage () - baseUsage

// CHECK-NOT: Found?!
// CHECK: Not found

print("Not found")

var usage = rusage()
getrusage(RUSAGE_SELF, &usage)

// CHECK: success
// CHECK-NOT: failure

// We should not need 50MB for this.
if usage.ru_maxrss > 50 * 1024 * 1024 {
print("failure - should not need 50MB!")
if firstRun * 2 < secondRun {
print("failure - should not linearly increase")
} else {
print("success")
}