Comments (3)
Swift 源代码
- https://github.com/apple/swift
- How to Read the Swift Standard Library Source - Ole Begemann
- Exploring the Swift standard library source code
Runtime
热修复
内存布局
- Size, Stride, Alignment - Swift Unboxed
⭐️ - Swift进阶之内存模型和方法调度
🎬 GOTO 2016 • Exploring Swift Memory Layout • Mike Ash(文字版:Mike Ash: Exploring Swift Memory Layout - Realm Academy)- Understanding Swift Value Semantics
- Introduction To Swift Memory Layout
- Swift 对象内存模型探究(一) - Bugly
- 【基本功】深入剖析Swift性能优化 - 美团技术团队
🎬 Advanced Swift L1 - Memory Layout & Pointers- ABI Stability Manifesto - swift
- 指针
- 为什么Swift比OC快?
from aswifttour.
元类型( AnyObject
, AnyClass
, .Type
, .Self
, .self
, type(of:)
)
class Cat {
func eat() {
let classType_: AnyClass = type(of: self)
print("Class type is \(classType_)")
}
static func eat() {
let classType_: AnyClass = self
print("Class type is \(classType_)")
}
}
class Garfield: Cat { }
let aCat = Cat()
aCat.eat() // Class type is Cat
Cat.eat() // Class type is Cat
let aGarfield = Garfield()
aGarfield.eat() // Class type is Garfield
Garfield.eat() // Class type is Garfield
let typeOfCat/*: Cat.Type */= Cat.self
let typeOfCatObj/*: Cat.Type */= type(of: aCat)
let classType: AnyClass = typeOfCat
let classType2: AnyClass = typeOfCatObj
let object: AnyObject = aCat
print(typeOfCat == typeOfCatObj) // true
print(typeOfCat) // Cat
print(typeOfCatObj) // Cat
print(classType) // Cat
print(classType2) // Cat
let str = NSString("Jack Ma")
let typeOfNSString = NSString.self
let typeOfStr = type(of: str)
print(typeOfNSString)
print(typeOfStr)
if typeOfStr is NSString.Type {
print("str is a NSSting Object")
}
extension BinaryInteger {
func squared() -> Self { // 这里的 Self 指的就是实体类型本身
return self * self
}
}
let a: Int = 3
print(a.squared()) // 9
参考
- What's .self, .Type and .Protocol? Understanding Swift Metatypes
- 理解 Swift 中的元类型:.Type 与 .self
- type(of:) - Swift Standard Library | Apple Developer Documentation
- ANYCLASS,元类型和 .SELF - Swifter
- Self vs self - what's the difference?
- 类型元数据
from aswifttour.
Size, Stride, Alignment
struct Year {
let year: Int // 8
}
struct YearWithMonth {
let year: Int
let month: Int
}
let sizeOfYear = MemoryLayout<Year>.size
let instanceOfYear = Year(year: 1984)
let sizeOfYearInstance = MemoryLayout.size(ofValue: instanceOfYear)
print(sizeOfYear) // 8
print(sizeOfYearInstance) // 8
let sizeOfYearWithMonth = MemoryLayout<YearWithMonth>.size
let instanceOfYearWithMonth = YearWithMonth(year: 1984, month: 10)
let sizeOfYearWithMonthInstance = MemoryLayout.size(ofValue: instanceOfYearWithMonth)
print(sizeOfYearWithMonth) // 16
print(sizeOfYearWithMonthInstance) // 16
//__________________________________________________________________________________
struct Puppy {
let age: Int // 8
let name: String // 16
let isTrained: Bool // 1
}
let sizeOfProperties = MemoryLayout<Int>.size + MemoryLayout<String>.size + MemoryLayout<Bool>.size
print(sizeOfProperties) // returns 25, from 8 + 16 + 1
let sizeOfPuppy = MemoryLayout<Puppy>.size
print(sizeOfPuppy) // returns 25
let strideOfPuppy = MemoryLayout<Puppy>.stride
print(strideOfPuppy) // returns 32
// We’ve preserved the alignment requirements of all the values inside the struct: the second integer is at byte 16, which is a multiple of 8.
// That’s why the struct’s stride can be greater than its size: to add enough padding to fulfill alignment requirements.
let alignmentOfPuppy = MemoryLayout<Puppy>.alignment
print(alignmentOfPuppy) // returns 8
// The stride then becomes the size rounded up to the next multiple of the alignment. In our case:
// the size is 25
// 25 is not a multiple of 8
// the next multiple of 8 after 25 is 32
// therefore, the stride is 32
// Size is the number of bytes to read from the pointer to reach all the data.
// Stride is the number of bytes to advance to reach the next item in the buffer.
// Alignment is the the “evenly divisible by” number that each instance needs to be at. If you’re allocating memory to copy data into, you need to specify the correct alignment (e.g. allocate(byteCount: 100, alignment: 4)).
// https://swiftunboxed.com/images/size-stride-alignment-summary.png
参考
from aswifttour.
Related Issues (15)
- Swift-ObjC 混编 HOT 6
- Swift Package Manager HOT 1
- 最佳实践(Best Practices) HOT 1
- Debugging HOT 1
- Talks, posts and questions HOT 3
- JSON 解析
- OC和siwft混编,脚本生成XXX-swift.h,脚本报错
- SwiftUI HOT 1
- 值类型(struct) VS. 引用类型(class) HOT 2
- WWDC(Swift 相关) HOT 1
- Swift 5.x HOT 3
- Swift 编译 HOT 1
- Swift 性能 HOT 2
- 语言特性 HOT 10
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from aswifttour.