-
-
Notifications
You must be signed in to change notification settings - Fork 49
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
Embedded Swift support [WIP] #251
Conversation
Wow awsome update. Hope to see it soon. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First of all, thank you for bringing this up! General feedbacks:
- Why do we need String16 instead of StaticString? If I remember correctly, we don't need much string manipulation but just passing around it. So StaticString should be enough for us.
- I'd like to keep this package standalone without any external dependency to have maintenance flexibility. Also it's a risk for us to add dependencies without explicit license.
- Could you reduce number of code duplication? Also I think it would be great to incrementally unlock a subset of API for Embedded target instead of trying to unlock whole APIs. It would allow me to review easily and move this forward quickly.
@kateinoigakukun thank you very much for the quick review! Number of code duplication is mostly because of moving from Without #if hasFeature(Embedded)
public typealias Str = String16
#else
public typealias Str = String
#endif When I started working with Embedded Swift + WebAssembly a month ago I asked a question regarding excluded Keeping this package standalone is important, I agree. Though something like
Do you mean that it'd be better to have completely separate Embedded target with its own code? |
Ah, I see. That's an unfortunate situation... Let me think more on how the current API could fit on Embedded target.
Even though we use UTF-16 as a representation in Swift memory space, taking a string from Swift to JS still require copying buffer and re-interpret byte sequence. And also transcoding UTF-8 into UTF-16 is not difficult to optimize with fast path where it fits within ASCII range. I took a quick benchmark on Node.js and it looks like decoding UTF-8 within ASCII range is noticeably faster than UTF-16 case. Even though it's slower for long non-ASCII chars, but most Web APIs are named within ASCII range, so it still makes sense to keep UTF-8 even on Embedded target.
function decodePerformance(data, encoder, decoder) {
// Encode the data
const encoded = encoder.encode(data);
const start = performance.now();
// Decode the data many times
for (let i = 0; i < 1000; i++) {
decoder.decode(encoded);
}
const end = performance.now();
return end - start;
}
function encodePerformance(data, encoder) {
const start = performance.now();
// Encode the data many times
for (let i = 0; i < 1000; i++) {
encoder.encode(data);
}
const end = performance.now();
return end - start;
}
for (const [direction, doit] of [
['Decode', (data, encoding) => decodePerformance(data, new TextEncoder(encoding), new TextDecoder(encoding))],
['Encode', (data, encoding) => encodePerformance(data, new TextEncoder(encoding))],
]) {
console.log(`\n### ${direction} Performance ###`);
for (const [charType, data] of [
['ASCII chars (fast-path)', 'a'.repeat(1000000)],
['Non-ASCII chars ', '🦄'.repeat(1000000)],
]) {
for (const encoding of ['utf-8', 'utf-16']) {
const time = doit(data, encoding);
const formattedTime = time.toFixed(2);
const formattedEncoding = encoding.padEnd(7);
console.log(`${charType} ${formattedEncoding} ${formattedTime}ms`);
}
}
}
We need a malloc implementation for testing but JavaScriptKit package should not ship it. We should leave the allocator choice to the user decision.
Sorry, I meant it would be a good first step if we guard out most of source files from the new |
Brings back `JSClosure` and `JSFunction` to life
74a45ec |
Closing in favor of #263 |
It adds an ability to use the original
JavaScriptKit
in Embedded SwiftAll the targets has been copied with
Embedded
suffix and uses the original source code via symlinks in order to have an ability to add special flags for Embedded Swift.String16 library has been written with an absolute minimal working code at this moment in order to provide UTF-16 String replacement for Embedded Swift.
EmbeddedFoundation library provides
malloc
,free
,arc4rand
methods for Embedded Swift.What's not working currently:JSClosure
,JSOneShotClosure
. It seems I missing something. Investigating what's wrong.LightWebApp is a demo Embedded Swift project which uses this version of
JavaScriptKit
.