Skip to content
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

Best approach to determining the memory layout of SwiftProtobuf message types with Storage classes? #1110

Closed
MrMage opened this issue Feb 18, 2021 · 2 comments

Comments

@MrMage
Copy link
Contributor

MrMage commented Feb 18, 2021

I am trying to determine the allocated size of my generated SwiftProtobuf message types in order to see whether I could e.g. reduce their memory usage by changing the field order of smaller types inside them.

However, for types without inline storage, the _StorageClass types are fileprivate, which means that I can't easily measure their size with class_getInstanceSize.

Right now I am using the following workaround based on reflection:

	private func getMemoryUsageOfStorageClass(of messageType: Message.Type) -> Int? {
		let mirror = Mirror(reflecting: messageType.init())
		for case let (label?, value) in mirror.children {
			if label == "_storage" {
				return class_getInstanceSize(type(of: value) as? AnyClass)
			}
		}
		return nil
	}

Is there a better workaround for this, or should I just keep using that?

@thomasvl
Copy link
Collaborator

I'm not aware of anything better than that. But the decisions around internal layout for size is something that's currently counted as an implementation detail and subject to change. We've had changes in this space in the past, and there are open issues about it (#735, #733). Other protobuf libraries do tweak the ordering to get better layout, it isn't something we've gone after yet for Swift, but is likely something that might eventually get some attention.

@MrMage
Copy link
Contributor Author

MrMage commented Feb 18, 2021

Thank you for the elaboration. For now, I'll at least add tests for my messages' current sizes in order to spot any regressions.

@MrMage MrMage closed this as completed Feb 18, 2021
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

No branches or pull requests

2 participants