Skip to content

stdlib: switch string hashing on non-ObjC platforms to SipHash-1-3 #4621

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 7, 2016
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -468,18 +468,6 @@ internal enum _SubSequenceSubscriptOnRangeMode {
}
}

internal func _product<C1 : Collection, C2 : Collection>(
_ c1: C1, _ c2: C2
) -> [(C1.Iterator.Element, C2.Iterator.Element)] {
var result: [(C1.Iterator.Element, C2.Iterator.Element)] = []
for e1 in c1 {
for e2 in c2 {
result.append((e1, e2))
}
}
return result
}

%{
from gyb_stdlib_support import collectionForTraversal
def testConstraints(protocol):
Expand Down Expand Up @@ -626,7 +614,7 @@ extension TestSuite {
_blackHole(c[index])
}

let tests = _product(
let tests = cartesianProduct(
subscriptRangeTests,
_SubSequenceSubscriptOnIndexMode.all)

Expand Down Expand Up @@ -725,7 +713,7 @@ extension TestSuite {
_blackHole(c[index..<index])
}

let tests = _product(
let tests = cartesianProduct(
subscriptRangeTests,
_SubSequenceSubscriptOnRangeMode.all)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ if resiliencyChecks.subscriptOnOutOfBoundsIndicesBehavior != .none {
c[index] = wrapValue(OpaqueValue(9999))
}

let tests = _product(
let tests = cartesianProduct(
subscriptRangeTests,
_SubSequenceSubscriptOnIndexMode.all)

Expand Down Expand Up @@ -328,7 +328,7 @@ if isFixedLengthCollection {
}

if resiliencyChecks.subscriptRangeOnOutOfBoundsRangesBehavior != .none {
let tests = _product(
let tests = cartesianProduct(
subscriptRangeTests,
_SubSequenceSubscriptOnRangeMode.all)

Expand Down
13 changes: 13 additions & 0 deletions stdlib/private/StdlibUnittest/StdlibCoreExtras.swift
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,16 @@ public func forAllPermutations<S : Sequence>(
return ()
}
}

public func cartesianProduct<C1 : Collection, C2 : Collection>(
_ c1: C1, _ c2: C2
) -> [(C1.Iterator.Element, C2.Iterator.Element)] {
var result: [(C1.Iterator.Element, C2.Iterator.Element)] = []
for e1 in c1 {
for e2 in c2 {
result.append((e1, e2))
}
}
return result
}

1 change: 1 addition & 0 deletions stdlib/public/SwiftShims/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ set(sources
RefCount.h
RuntimeShims.h
RuntimeStubs.h
SwiftStdbool.h
SwiftStddef.h
SwiftStdint.h
UnicodeShims.h
Expand Down
8 changes: 8 additions & 0 deletions stdlib/public/SwiftShims/GlobalObjects.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ struct _SwiftEmptyArrayStorage {
extern SWIFT_RUNTIME_STDLIB_INTERFACE
struct _SwiftEmptyArrayStorage _swiftEmptyArrayStorage;

struct _SwiftHashingSecretKey {
__swift_uint64_t key0;
__swift_uint64_t key1;
};

extern SWIFT_RUNTIME_STDLIB_INTERFACE
struct _SwiftHashingSecretKey _swift_stdlib_Hashing_secretKey;

extern SWIFT_RUNTIME_STDLIB_INTERFACE
__swift_uint64_t _swift_stdlib_HashingDetail_fixedSeedOverride;

Expand Down
23 changes: 23 additions & 0 deletions stdlib/public/SwiftShims/SwiftStdbool.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#ifndef SWIFT_STDLIB_SHIMS_SWIFTSTDBOOL_H_
#define SWIFT_STDLIB_SHIMS_SWIFTSTDBOOL_H_

#ifdef __cplusplus
typedef bool __swift_bool;
#else
typedef _Bool __swift_bool;
#endif

#endif

18 changes: 13 additions & 5 deletions stdlib/public/SwiftShims/UnicodeShims.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#define SWIFT_STDLIB_SHIMS_UNICODESHIMS_H_

#include "SwiftStdint.h"
#include "SwiftStdbool.h"
#include "Visibility.h"

#if __has_feature(nullability)
Expand Down Expand Up @@ -83,13 +84,20 @@ _swift_stdlib_unicode_compare_utf8_utf8(const unsigned char *Left,
__swift_int32_t RightLength);

SWIFT_RUNTIME_STDLIB_INTERFACE
__attribute__((__pure__)) __swift_intptr_t
_swift_stdlib_unicode_hash(const __swift_uint16_t *Str, __swift_int32_t Length);
void *_swift_stdlib_unicodeCollationIterator_create(
const __swift_uint16_t *Str,
__swift_uint32_t Length);

SWIFT_RUNTIME_STDLIB_INTERFACE
__attribute__((__pure__)) __swift_intptr_t
_swift_stdlib_unicode_hash_ascii(const unsigned char *Str,
__swift_int32_t Length);
__swift_int32_t _swift_stdlib_unicodeCollationIterator_next(
void *CollationIterator, __swift_bool *HitEnd);

SWIFT_RUNTIME_STDLIB_INTERFACE
void _swift_stdlib_unicodeCollationIterator_delete(
void *CollationIterator);

SWIFT_RUNTIME_STDLIB_INTERFACE
const __swift_int32_t *_swift_stdlib_unicode_getASCIICollationTable();

SWIFT_RUNTIME_STDLIB_INTERFACE
__swift_int32_t _swift_stdlib_unicode_strToUpper(
Expand Down
1 change: 1 addition & 0 deletions stdlib/public/SwiftShims/module.modulemap
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module SwiftShims {
header "RefCount.h"
header "RuntimeShims.h"
header "RuntimeStubs.h"
header "SwiftStdbool.h"
header "SwiftStddef.h"
header "SwiftStdint.h"
header "UnicodeShims.h"
Expand Down
1 change: 1 addition & 0 deletions stdlib/public/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ set(SWIFTLIB_ESSENTIAL
REPL.swift
Reverse.swift
Runtime.swift.gyb
SipHash.swift.gyb
Sequence.swift
SequenceAlgorithms.swift.gyb
SequenceWrapper.swift
Expand Down
1 change: 1 addition & 0 deletions stdlib/public/core/GroupInfo.json
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@
"AnyHashable.swift",
"Interval.swift",
"Hashing.swift",
"SipHash.swift",
"ErrorType.swift",
"InputStream.swift",
"LifetimeManager.swift",
Expand Down
21 changes: 21 additions & 0 deletions stdlib/public/core/Hashing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,27 @@

import SwiftShims

public // @testable
struct _Hashing {
// FIXME(ABI): make this an actual public API.
public // SPI
static var secretKey: (UInt64, UInt64) {
get {
// The variable itself is defined in C++ code so that it is initialized
// during static construction. Almost every Swift program uses hash
// tables, so initializing the secret key during the startup seems to be
// the right trade-off.
return (
_swift_stdlib_Hashing_secretKey.key0,
_swift_stdlib_Hashing_secretKey.key1)
}
set {
(_swift_stdlib_Hashing_secretKey.key0,
_swift_stdlib_Hashing_secretKey.key1) = newValue
}
}
}

public // @testable
struct _HashingDetail {

Expand Down
Loading