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

Clarify the scope of stdin/stdout for dynamically linked modules #2293

Closed
bep opened this issue Jul 27, 2024 · 2 comments
Closed

Clarify the scope of stdin/stdout for dynamically linked modules #2293

bep opened this issue Jul 27, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@bep
Copy link
Contributor

bep commented Jul 27, 2024

See https://github.com/bep/javywazeroissue/blob/58a6147e45096a2dbfc4d106002898c8f960b677/lib.go#L43

I'm pasting the relevant lines here for discussion:

buff := &bytes.Buffer{}
buff.WriteString(`{ "n": 2, "bar": "baz" }`)

_, err = r.InstantiateModule(ctx, compiledQuickJS, wazero.NewModuleConfig().WithStdout(buff).WithStderr(os.Stderr).WithStdin(buff).WithName("javy_quickjs_provider_v2"))
if err != nil {
	return "", err
}

_, err = r.InstantiateModule(ctx, compiledFoo, wazero.NewModuleConfig().WithName("foo"))
if err != nil {
	return "", err
}

fmt.Println(buff.String()) // Output: {"foo":3,"newBar":"baz!"}

The above works and is more or less the "hello world" example from https://github.com/bytecodealliance/javy.

To me, the foo module is the running instance, yet it uses stdin/stdout from the dynamically linked module, which I find surprising, and makes it impossible to init multiple JS modules (either different modules, or multiple instances of the same) in the same runtime.

I have implemented a proof of concept on this for inclusion in Hugo, with a simple RPC dispatcher over stdin/stdout, and it mostly works great. But for slower running scripts (e.g. Katex), I would want to have multiple instances of the script.1 And with the above, I need to create a new runtime per instance (with a shared compilation cache). This works, but is resource heavy:

BenchmarkKatexStartStop/PoolSize1-10         	      61	  18518154 ns/op	12242519 B/op	   18054 allocs/op
BenchmarkKatexStartStop/PoolSize8-10         	       9	 117701093 ns/op	97889447 B/op	  144385 allocs/op
BenchmarkKatexStartStop/PoolSize16-10        	       5	 222097958 ns/op	195748028 B/op	  288710 allocs/op

Ideally I would want to do something ala:

_, err = r.InstantiateModule(ctx, compiledQuickJS, wazero.NewModuleConfig().WithName("javy_quickjs_provider_v2"))
if err != nil {
	return "", err
}

_, err = r.InstantiateModule(ctx, compiledFoo, wazero.NewModuleConfig().WithStdout(buff1).WithStderr(os.Stderr).WithStdin(buff1).WithName("foo1"))
if err != nil {
	return "", err
}
_, err = r.InstantiateModule(ctx, compiledFoo, wazero.NewModuleConfig().WithStdout(buff2).WithStderr(os.Stderr).WithStdin(buff2).WithName("foo2"))
if err != nil {
	return "", err
}
// ...

Footnotes

  1. Yes, I have looked at https://github.com/suborbital/javy and https://github.com/F21/javy-wazero, but the bytecodealliance/Shopify fork seems to be the active one, and I don't think the suborbital fork would help with the main problem above.

@bep bep added the enhancement New feature or request label Jul 27, 2024
@bep
Copy link
Contributor Author

bep commented Jul 27, 2024

Looking at my own writing above, I see that I need to do a little investigation to see how this is wired up at the Javy side, but I'll keep this open and I welcome any tips.

@bep bep closed this as completed Jul 27, 2024
@bep bep reopened this Jul 27, 2024
@bep
Copy link
Contributor Author

bep commented Jul 27, 2024

I have done some investigating, and I'm guess the current behaviour is as expected with the current API: The executing function gets the fs/stdin/etc. from the module it's invoked.

What could possibly work and be worth while would be something ala:

RegisterModule(name, compiled)

Which when requested as an import, and not found in the regular instance map, would be instansiated with most of the config inherited from the importer.

@mathetake mathetake closed this as not planned Won't fix, can't repro, duplicate, stale Jul 27, 2024
bep added a commit to bep/wazero that referenced this issue Jul 28, 2024
Some upstream (Hugo) benchmarks:

```
name                          old time/op    new time/op    delta
KatexStartStop/PoolSize1-10     19.5ms ± 6%    19.4ms ± 3%     ~     (p=1.000 n=4+4)
KatexStartStop/PoolSize8-10      116ms ± 6%      55ms ± 3%  -52.61%  (p=0.029 n=4+4)
KatexStartStop/PoolSize16-10     222ms ± 3%      95ms ± 6%  -57.07%  (p=0.029 n=4+4)

name                          old alloc/op   new alloc/op   delta
KatexStartStop/PoolSize1-10     12.2MB ± 0%    12.2MB ± 0%   -0.22%  (p=0.029 n=4+4)
KatexStartStop/PoolSize8-10     97.9MB ± 0%    73.9MB ± 0%  -24.46%  (p=0.029 n=4+4)
KatexStartStop/PoolSize16-10     196MB ± 0%     148MB ± 0%  -24.15%  (p=0.029 n=4+4)

name                          old allocs/op  new allocs/op  delta
KatexStartStop/PoolSize1-10      18.1k ± 0%     17.4k ± 0%   -3.45%  (p=0.029 n=4+4)
KatexStartStop/PoolSize8-10       144k ± 0%       18k ± 0%  -87.65%  (p=0.029 n=4+4)
KatexStartStop/PoolSize16-10      289k ± 0%       18k ± 0%  -93.67%  (p=0.029 n=4+4)
```

See tetratelabs#2293
bep added a commit to bep/wazero that referenced this issue Jul 28, 2024
Some upstream (Hugo) benchmarks:

```
name                          old time/op    new time/op    delta
KatexStartStop/PoolSize1-10     19.5ms ± 6%    19.4ms ± 3%     ~     (p=1.000 n=4+4)
KatexStartStop/PoolSize8-10      116ms ± 6%      55ms ± 3%  -52.61%  (p=0.029 n=4+4)
KatexStartStop/PoolSize16-10     222ms ± 3%      95ms ± 6%  -57.07%  (p=0.029 n=4+4)

name                          old alloc/op   new alloc/op   delta
KatexStartStop/PoolSize1-10     12.2MB ± 0%    12.2MB ± 0%   -0.22%  (p=0.029 n=4+4)
KatexStartStop/PoolSize8-10     97.9MB ± 0%    73.9MB ± 0%  -24.46%  (p=0.029 n=4+4)
KatexStartStop/PoolSize16-10     196MB ± 0%     148MB ± 0%  -24.15%  (p=0.029 n=4+4)

name                          old allocs/op  new allocs/op  delta
KatexStartStop/PoolSize1-10      18.1k ± 0%     17.4k ± 0%   -3.45%  (p=0.029 n=4+4)
KatexStartStop/PoolSize8-10       144k ± 0%       18k ± 0%  -87.65%  (p=0.029 n=4+4)
KatexStartStop/PoolSize16-10      289k ± 0%       18k ± 0%  -93.67%  (p=0.029 n=4+4)
```

See tetratelabs#2293
bep added a commit to bep/wazero that referenced this issue Jul 28, 2024
Some upstream (Hugo) benchmarks:

```
name                          old time/op    new time/op    delta
KatexStartStop/PoolSize1-10     19.5ms ± 6%    19.4ms ± 3%     ~     (p=1.000 n=4+4)
KatexStartStop/PoolSize8-10      116ms ± 6%      55ms ± 3%  -52.61%  (p=0.029 n=4+4)
KatexStartStop/PoolSize16-10     222ms ± 3%      95ms ± 6%  -57.07%  (p=0.029 n=4+4)

name                          old alloc/op   new alloc/op   delta
KatexStartStop/PoolSize1-10     12.2MB ± 0%    12.2MB ± 0%   -0.22%  (p=0.029 n=4+4)
KatexStartStop/PoolSize8-10     97.9MB ± 0%    73.9MB ± 0%  -24.46%  (p=0.029 n=4+4)
KatexStartStop/PoolSize16-10     196MB ± 0%     148MB ± 0%  -24.15%  (p=0.029 n=4+4)

name                          old allocs/op  new allocs/op  delta
KatexStartStop/PoolSize1-10      18.1k ± 0%     17.4k ± 0%   -3.45%  (p=0.029 n=4+4)
KatexStartStop/PoolSize8-10       144k ± 0%       18k ± 0%  -87.65%  (p=0.029 n=4+4)
KatexStartStop/PoolSize16-10      289k ± 0%       18k ± 0%  -93.67%  (p=0.029 n=4+4)
```

See tetratelabs#2293
bep added a commit to bep/wazero that referenced this issue Jul 28, 2024
Some upstream (Hugo) benchmarks:

```
name                          old time/op    new time/op    delta
KatexStartStop/PoolSize1-10     19.5ms ± 6%    19.4ms ± 3%     ~     (p=1.000 n=4+4)
KatexStartStop/PoolSize8-10      116ms ± 6%      55ms ± 3%  -52.61%  (p=0.029 n=4+4)
KatexStartStop/PoolSize16-10     222ms ± 3%      95ms ± 6%  -57.07%  (p=0.029 n=4+4)

name                          old alloc/op   new alloc/op   delta
KatexStartStop/PoolSize1-10     12.2MB ± 0%    12.2MB ± 0%   -0.22%  (p=0.029 n=4+4)
KatexStartStop/PoolSize8-10     97.9MB ± 0%    73.9MB ± 0%  -24.46%  (p=0.029 n=4+4)
KatexStartStop/PoolSize16-10     196MB ± 0%     148MB ± 0%  -24.15%  (p=0.029 n=4+4)

name                          old allocs/op  new allocs/op  delta
KatexStartStop/PoolSize1-10      18.1k ± 0%     17.4k ± 0%   -3.45%  (p=0.029 n=4+4)
KatexStartStop/PoolSize8-10       144k ± 0%       18k ± 0%  -87.65%  (p=0.029 n=4+4)
KatexStartStop/PoolSize16-10      289k ± 0%       18k ± 0%  -93.67%  (p=0.029 n=4+4)
```

See tetratelabs#2293
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants