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

Provide a way to generate files that only import FoundationEssentials and conditionalize all Foundation imports #1756

Open
FranzBusch opened this issue Feb 12, 2025 · 7 comments

Comments

@FranzBusch
Copy link
Member

FoundationEssentials allows to import just a small subset of Foundation on non-Darwin platforms. This has a great impact on binary size. The current recommended way to do this is this pattern:

#if canImport(FoundationEssentials)
import FoundationEssentials
#else
import Foundation
#endif

We need to update our generator to support this and audit all of our current Foundation imports. As a first step it would be good to understand if we rely on anything from Foundation that is not in FoundationEssentials.

@thomasvl
Copy link
Collaborator

Does a Swift version also come into play? Or does FoundationEssentials exist back to 5.8?

@FranzBusch
Copy link
Member Author

The canImport should be enough here. Since the module doesn't exist in previous versions.

@thomasvl
Copy link
Collaborator

Is there a good listing of what is in/out of FoundationEssentials? https://github.com/swiftlang/swift-foundation?tab=readme-ov-file#swift-foundation just lists a few and ends with and more which isn't that helpful.

My initial thought is we probably only make use of Data generally (and ContinuousBytes). Then Duration and Timestamp might make use of things for some of their bridging. So maybe it wouldn't be much to move the runtime and generated code over?

@FranzBusch
Copy link
Member Author

I just landed this PR swiftlang/swift-foundation#1178 . So hopefully the documentation shows up on the Swift Package Index soon. This should help you see what types are part of FoundationEssentials.

@thomasvl
Copy link
Collaborator

@thomasvl
Copy link
Collaborator

Will this count as a breaking change since someone importing SwiftProtobuf or a module that had generated proto code, indirectly was getting Foundation?

@thomasvl
Copy link
Collaborator

thomasvl commented Feb 24, 2025

Did a quick search/replace on the runtime to try the saw, and a few things trip it up, so I think that means, we'd have to find a way to replace those things, otherwise, there wouldn't be any real wins. Failure: https://github.com/apple/swift-protobuf/actions/runs/13506421799/job/37736916317?pr=1758

Issues:

  • DoubleParser.swift : strtod
  • BinaryDelimited.swift : InputStream, OutputStream

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