-
Notifications
You must be signed in to change notification settings - Fork 28
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
build wasm file is very big??? #7
Comments
After running Still quite big, as with |
Now, we're focussing on making produced binary work properly. But I think binary size is one of the most important things too. Fat stdlibAs far as I investigated, Swift stdlib is so fat that weights about 8.8MB because it depends on ICU and libc. Other platform projects are facing on same problem too. Fortunately, Swift6 will work on supporting more platforms, and core team know this problem. If they decide to implement a subset of stdlib to be more small, it can be helpful. LTO approachMost of stdlib parts are unused, so if linker omits unused code, the produced binary will be more small. Here is a Swift LTO talk at LLVM meeting. Swift has many dynamic language feature, like dynamic casting, dynamic dispatch or etc.., so my concerns with this approach is that linker can't know what For example,
public protocol P {
func foo()
}
extension String: P {
func foo() {}
}
public func useP<T>(_ value: T) {
(value as? P)?.foo()
}
import ModuleA
struct S: P {}
useP(S()) In this case, So, to achieve more reduction, we need to consider new optimization approach, like WMO or something LTO plugin that tells linker which protocols are used and omits unused conformances and functions. |
We are integrating SwiftWasm into Wasienv and realized that creating a file for a simple hello world is 42Mb (see attached file at the bottom of this comment). You can follow the steps to compile easily Swift to Wasm easily here: https://docs.wasmer.io/ecosystem/wasienv/compile-swift-to-wasm-wasi How Wasienv worksThis is what wasienv is doing under the hood:
Perhaps we are doing something wrong? Simple Example - 42MbThe file that if CommandLine.arguments.count < 2 {
print("Hello, WASI!\n");
} else {
print("Hello, \(CommandLine.arguments[1])\n");
} |
@syrusakbary Thanks for trying Swift for WASM! Wasienv looks doing right. Please try the lastest toolchain from here https://github.com/swiftwasm/swift/releases |
Cool, let me update the release and I'll put my findings after. Thanks for the quick response! |
Nice! I just got it running!! The file is now much smaller as you pointed (9Mb), and takes much less time to execute.
🎉 TimingsWhile trying things out, I've been doing an analysis of the I just compared wasmer and wasmtime for running a generated file, and Wasmer timings are much better (about 10x faster runtime speed once the file is compiled) Wasmtime: 1s to execute
Wasmer: .2s to execute (5x faster)
|
I tried to use wasmer instead of wasmtime, it saved 13min for test execution! It's really good news! |
wasmtime has opt flags |
Yup, tried them both, but the timings are exactly the same than without those flags (mainly because You can also try them locally, following instructions here: https://docs.wasmer.io/ecosystem/wasienv/compile-swift-to-wasm-wasi But the timings are: .2s for Wasmer and 1s for Wasmtime (with opt flags turned on) |
I can confirm that running Both became 1.5M when zipped, that's not great, but not terrible either, almost order of magnitude better than the raw binary 😄 |
Whoever is interested, the detailed plan for improvements is available in this Swift Forums thread:
Parts of phase 1 are currently in review as swiftlang#31146. |
The progress report is now posted in this new Swift Forums thread. |
I tried reducing a
There's a meta point on building Swift with size optimizations. Swift LTO Are there any other ways to reduce the binary size? @MaxDesiatov wrote a nice summary of what improvements could be made in future. With respect to that, I saw that stdlib dropped the ICU dependency. Is this change already in effect? What's the word on WASI custom allocators? Attaching results for reference - SizeOptimization.zip |
I've posted an update about ICU here recently: #2411 (comment). Basically, we should phrase this as "removal of Unicode tables" instead of "removal of ICU". Even though ICU was removed as a dependency, Unicode tables were copied from it to stdlib itself, that shouldn't make a big difference in binary size if any. As for build flags, there's ongoing work on swiftlang#42366, which may be relevant, but I haven't done any measurements with that myself. WRT custom allocators and WASI-less builds, we could link with uSwift instead of stdlib, but we need to make #4374 fully work, and uSwift itself is super barebones and doesn't even support generics. This option is going to take most time and effort to implement when compared to other options, in my opinion. |
@MaxDesiatov How could one assist in reducing the binary size? Is it possible to use uSwift with swiftwasm? If so, how does it work? If not, what work would be needed to make it possible? Would a sponsorship help to implement this? |
One blocker that I know of for implementing a minimal standard library is swiftlang#63603. There may be more of course to be found when this one is resolved. |
Is it possible to use SwiftWasm with Also, it looks like Apple is working on "Embedded Swift": https://forums.swift.org/t/embedded-swift/67057 |
Trying $ swiftc -target wasm32-unknown-wasi -lto=llvm-full -lswiftCore -static Sources/main.swift
wasm-ld: error: /home/bastian/Downloads/swift-wasm-5.8.0-RELEASE/usr/share/wasi-sysroot/lib/wasm32-wasi/libc.a(__main_argc_argv.o): undefined symbol: main
clang-13: error: linker command failed with exit code 1 (use -v to see invocation)
<unknown>:0: error: link command failed with exit code 1 (use -v to see invocation) Any pointers @kateinoigakukun @MaxDesiatov? |
It indeed looks like LTO hermetic seal may be the most promising option here. I see that @kateinoigakukun actually did a talk about this option last year. Is the work towards supporting this option for SwiftWasm blocked by something in particular or is it a case of it "just" needing more hands/eyes? |
As far as I know, there is no major blocker though we might need some minor adjustments specific to wasm. This is "just" a priority and human-resource problem. At this moment, upstreaming has a higher priority |
Ok, thanks so much for your continued work on this @kateinoigakukun. You are doing heroic work. どうもありがとうございます。 |
@MaxDesiatov wrote up a great post about using the upcoming Embedded feature of Swift to build small binaries: https://forums.swift.org/t/some-feedback-from-my-short-experience-with-swiftwasm/69605/5 |
I was curious how the combination of Embedded Swift and wasm would change this large file size issue. I was surprised to find that when I built the one shown in Wasm I/O at hand, it was an astonishingly small 101k bytes! This definitely indicates that swiftwasm can be adopted for some development, including deployment to wasm.
Apple Example: https://github.com/apple/swift-for-wasm-examples |
build wasm file is very big
golang is 2mb+
rust is 20k+
this is 9.5mb
The text was updated successfully, but these errors were encountered: