Skip to content

Commit bfd5942

Browse files
authored
Merge pull request #4621 from apple/siphash
stdlib: switch string hashing on non-ObjC platforms to SipHash-1-3
2 parents 4820453 + e8e8b35 commit bfd5942

17 files changed

+786
-96
lines changed

stdlib/private/StdlibCollectionUnittest/CheckCollectionType.swift.gyb

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -468,18 +468,6 @@ internal enum _SubSequenceSubscriptOnRangeMode {
468468
}
469469
}
470470

471-
internal func _product<C1 : Collection, C2 : Collection>(
472-
_ c1: C1, _ c2: C2
473-
) -> [(C1.Iterator.Element, C2.Iterator.Element)] {
474-
var result: [(C1.Iterator.Element, C2.Iterator.Element)] = []
475-
for e1 in c1 {
476-
for e2 in c2 {
477-
result.append((e1, e2))
478-
}
479-
}
480-
return result
481-
}
482-
483471
%{
484472
from gyb_stdlib_support import collectionForTraversal
485473
def testConstraints(protocol):
@@ -626,7 +614,7 @@ extension TestSuite {
626614
_blackHole(c[index])
627615
}
628616

629-
let tests = _product(
617+
let tests = cartesianProduct(
630618
subscriptRangeTests,
631619
_SubSequenceSubscriptOnIndexMode.all)
632620

@@ -725,7 +713,7 @@ extension TestSuite {
725713
_blackHole(c[index..<index])
726714
}
727715

728-
let tests = _product(
716+
let tests = cartesianProduct(
729717
subscriptRangeTests,
730718
_SubSequenceSubscriptOnRangeMode.all)
731719

stdlib/private/StdlibCollectionUnittest/CheckMutableCollectionType.swift.gyb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ if resiliencyChecks.subscriptOnOutOfBoundsIndicesBehavior != .none {
175175
c[index] = wrapValue(OpaqueValue(9999))
176176
}
177177

178-
let tests = _product(
178+
let tests = cartesianProduct(
179179
subscriptRangeTests,
180180
_SubSequenceSubscriptOnIndexMode.all)
181181

@@ -328,7 +328,7 @@ if isFixedLengthCollection {
328328
}
329329

330330
if resiliencyChecks.subscriptRangeOnOutOfBoundsRangesBehavior != .none {
331-
let tests = _product(
331+
let tests = cartesianProduct(
332332
subscriptRangeTests,
333333
_SubSequenceSubscriptOnRangeMode.all)
334334

stdlib/private/StdlibUnittest/StdlibCoreExtras.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,16 @@ public func forAllPermutations<S : Sequence>(
232232
return ()
233233
}
234234
}
235+
236+
public func cartesianProduct<C1 : Collection, C2 : Collection>(
237+
_ c1: C1, _ c2: C2
238+
) -> [(C1.Iterator.Element, C2.Iterator.Element)] {
239+
var result: [(C1.Iterator.Element, C2.Iterator.Element)] = []
240+
for e1 in c1 {
241+
for e2 in c2 {
242+
result.append((e1, e2))
243+
}
244+
}
245+
return result
246+
}
247+

stdlib/public/SwiftShims/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ set(sources
99
RefCount.h
1010
RuntimeShims.h
1111
RuntimeStubs.h
12+
SwiftStdbool.h
1213
SwiftStddef.h
1314
SwiftStdint.h
1415
UnicodeShims.h

stdlib/public/SwiftShims/GlobalObjects.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ struct _SwiftEmptyArrayStorage {
3939
extern SWIFT_RUNTIME_STDLIB_INTERFACE
4040
struct _SwiftEmptyArrayStorage _swiftEmptyArrayStorage;
4141

42+
struct _SwiftHashingSecretKey {
43+
__swift_uint64_t key0;
44+
__swift_uint64_t key1;
45+
};
46+
47+
extern SWIFT_RUNTIME_STDLIB_INTERFACE
48+
struct _SwiftHashingSecretKey _swift_stdlib_Hashing_secretKey;
49+
4250
extern SWIFT_RUNTIME_STDLIB_INTERFACE
4351
__swift_uint64_t _swift_stdlib_HashingDetail_fixedSeedOverride;
4452

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef SWIFT_STDLIB_SHIMS_SWIFTSTDBOOL_H_
14+
#define SWIFT_STDLIB_SHIMS_SWIFTSTDBOOL_H_
15+
16+
#ifdef __cplusplus
17+
typedef bool __swift_bool;
18+
#else
19+
typedef _Bool __swift_bool;
20+
#endif
21+
22+
#endif
23+

stdlib/public/SwiftShims/UnicodeShims.h

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#define SWIFT_STDLIB_SHIMS_UNICODESHIMS_H_
1919

2020
#include "SwiftStdint.h"
21+
#include "SwiftStdbool.h"
2122
#include "Visibility.h"
2223

2324
#if __has_feature(nullability)
@@ -83,13 +84,20 @@ _swift_stdlib_unicode_compare_utf8_utf8(const unsigned char *Left,
8384
__swift_int32_t RightLength);
8485

8586
SWIFT_RUNTIME_STDLIB_INTERFACE
86-
__attribute__((__pure__)) __swift_intptr_t
87-
_swift_stdlib_unicode_hash(const __swift_uint16_t *Str, __swift_int32_t Length);
87+
void *_swift_stdlib_unicodeCollationIterator_create(
88+
const __swift_uint16_t *Str,
89+
__swift_uint32_t Length);
8890

8991
SWIFT_RUNTIME_STDLIB_INTERFACE
90-
__attribute__((__pure__)) __swift_intptr_t
91-
_swift_stdlib_unicode_hash_ascii(const unsigned char *Str,
92-
__swift_int32_t Length);
92+
__swift_int32_t _swift_stdlib_unicodeCollationIterator_next(
93+
void *CollationIterator, __swift_bool *HitEnd);
94+
95+
SWIFT_RUNTIME_STDLIB_INTERFACE
96+
void _swift_stdlib_unicodeCollationIterator_delete(
97+
void *CollationIterator);
98+
99+
SWIFT_RUNTIME_STDLIB_INTERFACE
100+
const __swift_int32_t *_swift_stdlib_unicode_getASCIICollationTable();
93101

94102
SWIFT_RUNTIME_STDLIB_INTERFACE
95103
__swift_int32_t _swift_stdlib_unicode_strToUpper(

stdlib/public/SwiftShims/module.modulemap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module SwiftShims {
99
header "RefCount.h"
1010
header "RuntimeShims.h"
1111
header "RuntimeStubs.h"
12+
header "SwiftStdbool.h"
1213
header "SwiftStddef.h"
1314
header "SwiftStdint.h"
1415
header "UnicodeShims.h"

stdlib/public/core/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ set(SWIFTLIB_ESSENTIAL
101101
REPL.swift
102102
Reverse.swift
103103
Runtime.swift.gyb
104+
SipHash.swift.gyb
104105
Sequence.swift
105106
SequenceAlgorithms.swift.gyb
106107
SequenceWrapper.swift

stdlib/public/core/GroupInfo.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@
135135
"AnyHashable.swift",
136136
"Interval.swift",
137137
"Hashing.swift",
138+
"SipHash.swift",
138139
"ErrorType.swift",
139140
"InputStream.swift",
140141
"LifetimeManager.swift",

stdlib/public/core/Hashing.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,27 @@
2323

2424
import SwiftShims
2525

26+
public // @testable
27+
struct _Hashing {
28+
// FIXME(ABI): make this an actual public API.
29+
public // SPI
30+
static var secretKey: (UInt64, UInt64) {
31+
get {
32+
// The variable itself is defined in C++ code so that it is initialized
33+
// during static construction. Almost every Swift program uses hash
34+
// tables, so initializing the secret key during the startup seems to be
35+
// the right trade-off.
36+
return (
37+
_swift_stdlib_Hashing_secretKey.key0,
38+
_swift_stdlib_Hashing_secretKey.key1)
39+
}
40+
set {
41+
(_swift_stdlib_Hashing_secretKey.key0,
42+
_swift_stdlib_Hashing_secretKey.key1) = newValue
43+
}
44+
}
45+
}
46+
2647
public // @testable
2748
struct _HashingDetail {
2849

0 commit comments

Comments
 (0)