Skip to content

[stdlib] Fix bug in small string uninitialized init #36667

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 1 commit into from
Apr 2, 2021

Conversation

milseman
Copy link
Member

Fix a bug and enforce the invariant that all bits between the last code unit
and the descriminator in a small string should be unset.

Fix a bug and enforce the invariant that all bits between the last code unit
and the descriminator in a small string should be unset.
let len = try f(rawBufPtr)
UnsafeMutableRawBufferPointer(
rebasing: rawBufPtr[len...]
).initializeMemory(as: UInt8.self, repeating: 0)
Copy link
Member Author

Choose a reason for hiding this comment

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

@atrick is this a proper use of initializeMemory? It's basically a convenient way to mask off our unused bytes and make sure they're set to 0.

Copy link
Collaborator

Choose a reason for hiding this comment

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

@milseman Now that I'm looking at this again, I think this needs to rebind the memory; otherwise, after initializing memory as UInt8, portions of the memory are bound simultaneously as RawBitPattern.self and UInt8.self 😕

let count = self.count
let len = try withUnsafeMutableBytes(of: &self._storage) {
  (rawBufPtr: UnsafeMutableRawBufferPointer) -> Int in
  let len = try f(rawBufPtr)
  rawBufPtr.bindMemory(to: UInt8.self, capacity: count)
  defer {
    _ = rawBufPtr.bindMemory(to: RawBitPattern.self, capacity: 1)
  }
  UnsafeMutableRawBufferPointer(
    rebasing: rawBufPtr[len...]
  ).initializeMemory(as: UInt8.self, repeating: 0)
  return len
}

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't see any API for initializing raw bytes without using a typed pointer. You can do the silly rebinding thing above or just write your own raw initialize that doesn't care about the in-memory type

var nextPtr = self
    for _ in 0..<count {
      Builtin.initialize(repeatedValue, nextPtr._rawValue)
      nextPtr += MemoryLayout<T>.stride
    }

@milseman
Copy link
Member Author

@swift-ci please smoke test

@milseman
Copy link
Member Author

@swift-ci please benchmark

@milseman milseman requested a review from atrick March 31, 2021 13:52
@swift-ci
Copy link
Contributor

Performance: -O

Regression OLD NEW DELTA RATIO
AngryPhonebook 350 497 +42.0% 0.70x
AngryPhonebook.ASCII2.Small 159 219 +37.7% 0.73x
StringFromLongWholeSubstring 4 5 +25.0% 0.80x
NSStringConversion 450 546 +21.3% 0.82x (?)
NSStringConversion.Mutable 765 830 +8.5% 0.92x (?)
 
Improvement OLD NEW DELTA RATIO
StringFromLongWholeSubstringGeneric 6 5 -16.7% 1.20x
FlattenListLoop 2508 2198 -12.4% 1.14x (?)
LessSubstringSubstring 42 39 -7.1% 1.08x (?)
EqualStringSubstring 42 39 -7.1% 1.08x (?)
EqualSubstringString 42 39 -7.1% 1.08x (?)
LessSubstringSubstringGenericComparable 42 39 -7.1% 1.08x
SortStringsUnicode 3110 2905 -6.6% 1.07x (?)

Code size: -O

Performance: -Osize

Regression OLD NEW DELTA RATIO
AngryPhonebook.ASCII2.Small 159 223 +40.3% 0.71x
AngryPhonebook 350 490 +40.0% 0.71x
NSStringConversion 450 564 +25.3% 0.80x
StringFromLongWholeSubstring 4 5 +25.0% 0.80x
NSStringConversion.Rebridge 228 263 +15.4% 0.87x (?)
Array2D 6944 7520 +8.3% 0.92x (?)
String.replaceSubrange.String.Small 61 66 +8.2% 0.92x (?)
RandomShuffleLCG2 416 448 +7.7% 0.93x
 
Improvement OLD NEW DELTA RATIO
StringFromLongWholeSubstringGeneric 6 5 -16.7% 1.20x
EqualSubstringSubstringGenericEquatable 43 39 -9.3% 1.10x
FindString.Loop1.Substring 511 471 -7.8% 1.08x
LessSubstringSubstring 42 39 -7.1% 1.08x (?)
EqualSubstringSubstring 42 39 -7.1% 1.08x (?)
EqualStringSubstring 42 39 -7.1% 1.08x (?)
EqualSubstringString 42 39 -7.1% 1.08x (?)
LessSubstringSubstringGenericComparable 42 39 -7.1% 1.08x (?)
DropLastAnyCollectionLazy 29698 27720 -6.7% 1.07x (?)

Code size: -Osize

Performance: -Onone

Regression OLD NEW DELTA RATIO
AngryPhonebook 546 686 +25.6% 0.80x
AngryPhonebook.ASCII2.Small 255 310 +21.6% 0.82x
NSStringConversion 1246 1372 +10.1% 0.91x (?)
Data.hash.Medium 54 59 +9.3% 0.92x (?)
SevenBoom 1431 1539 +7.5% 0.93x (?)
 
Improvement OLD NEW DELTA RATIO
SortStringsUnicode 4815 4490 -6.7% 1.07x (?)

Code size: -swiftlibs

How to read the data The tables contain differences in performance which are larger than 8% and differences in code size which are larger than 1%.

If you see any unexpected regressions, you should consider fixing the
regressions before you merge the PR.

Noise: Sometimes the performance results (not code size!) contain false
alarms. Unexpected regressions which are marked with '(?)' are probably noise.
If you see regressions which you cannot explain you can try to run the
benchmarks again. If regressions still show up, please consult with the
performance team (@eeckstein).

Hardware Overview
  Model Name: Mac Pro
  Model Identifier: MacPro6,1
  Processor Name: 12-Core Intel Xeon E5
  Processor Speed: 2.7 GHz
  Number of Processors: 1
  Total Number of Cores: 12
  L2 Cache (per Core): 256 KB
  L3 Cache: 30 MB
  Memory: 64 GB

@milseman
Copy link
Member Author

@swift-ci please smoke test macOS platform

2 similar comments
@milseman
Copy link
Member Author

milseman commented Apr 1, 2021

@swift-ci please smoke test macOS platform

@milseman
Copy link
Member Author

milseman commented Apr 1, 2021

@swift-ci please smoke test macOS platform

@milseman
Copy link
Member Author

milseman commented Apr 1, 2021

@swift-ci please smoke test macOS platform.

@milseman
Copy link
Member Author

milseman commented Apr 1, 2021

Waiting on swiftlang/llvm-project#2770

@milseman
Copy link
Member Author

milseman commented Apr 1, 2021

@swift-ci please smoke test

@milseman
Copy link
Member Author

milseman commented Apr 2, 2021

Seriously...

@swift-ci please smoke test

@milseman
Copy link
Member Author

milseman commented Apr 2, 2021

@swift-ci please smoke test linux platform

@milseman
Copy link
Member Author

milseman commented Apr 2, 2021

@swift-ci please smoke test windows platform

1 similar comment
@milseman
Copy link
Member Author

milseman commented Apr 2, 2021

@swift-ci please smoke test windows platform

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants