Skip to content

syscall/js: allocate makeArgs slices on stack #49799

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

Closed
wants to merge 1 commit into from

Conversation

finnbear
Copy link

This change optimizes the syscall/js package such that:

  • Calling a JavaScript function with 16 arguments or fewer will not induce two additional heap allocations, at least with the current Go compiler
  • Using syscall/js features with slices and strings of statically-known length will not cause them to be escaped to the heap, at least with the current Go compiler
  • The reduction in allocations has the additional benefit that the garbage collector runs less often, blocking WebAssembly's one and only thread less often

There should be no observable difference in the syscall/js API.

These changes have been discussed and mostly agreed upon in the linked issue.

Fixes #39740

I am aware that this change falls outside a release cycle and during a development freeze. I will leave it up to maintainers to decide whether this qualifies for a freeze exception.

@google-cla google-cla bot added the cla: yes Used by googlebot to label PRs as having a valid CLA. The text of this label should not change. label Nov 25, 2021
@@ -580,4 +604,5 @@ func CopyBytesToJS(dst Value, src []byte) int {
return n
}

//go:noescape
Copy link
Author

@finnbear finnbear Nov 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Justification: this will affect whether byte slices with dynamic contents but statically known length to not escape anymore. It is correct because the bytes are copied to JS, not referenced by JS

@@ -292,6 +293,7 @@ func (v Value) Get(p string) Value {
return r
}

//go:noescape
Copy link
Author

@finnbear finnbear Nov 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Justification: this (and the one on valueSet and valueDelete) will affect whether strings with dynamic contents but statically known length to not escape anymore. It is correct, because the Go memory is only accessed for the duration of the call.

@@ -209,6 +209,7 @@ func ValueOf(x interface{}) Value {
}
}

//go:noescape
Copy link
Author

@finnbear finnbear Nov 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Justification: this will affect whether strings with dynamic contents but statically known length to not escape anymore. It is correct, because the string is immediately copied away from Go memory on the JS side.

@gopherbot
Copy link
Contributor

This PR (HEAD: 8c960c1) has been imported to Gerrit for code review.

Please visit https://go-review.googlesource.com/c/go/+/367045 to see it.

Tip: You can toggle comments from me using the comments slash command (e.g. /comments off)
See the Wiki page for more info

@@ -391,13 +410,15 @@ func (v Value) Call(m string, args ...interface{}) Value {
return makeValue(res)
}

//go:noescape
Copy link
Author

@finnbear finnbear Nov 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Justification: this (and the one on valueInvoke and valueNew) is essential for the slice optimization. It is correct because the slices are not kept around.

@gopherbot
Copy link
Contributor

Message from Go Bot:

Patch Set 1:

Congratulations on opening your first change. Thank you for your contribution!

Next steps:
A maintainer will review your change and provide feedback. See
https://golang.org/doc/contribute.html#review for more info and tips to get your
patch through code review.

Most changes in the Go project go through a few rounds of revision. This can be
surprising to people new to the project. The careful, iterative review process
is our way of helping mentor contributors and ensuring that their contributions
have a lasting impact.

During May-July and Nov-Jan the Go project is in a code freeze, during which
little code gets reviewed or merged. If a reviewer responds with a comment like
R=go1.11 or adds a tag like "wait-release", it means that this CL will be
reviewed as part of the next development cycle. See https://golang.org/s/release
for more details.


Please don’t reply on this GitHub thread. Visit golang.org/cl/367045.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from Hajime Hoshi:

Patch Set 1: Run-TryBot+1

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/367045.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from Go Bot:

Patch Set 1:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/367045.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from Go Bot:

Patch Set 1: TryBot-Result+1

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/367045.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from Hajime Hoshi:

Patch Set 1: Code-Review+1


Please don’t reply on this GitHub thread. Visit golang.org/cl/367045.
After addressing review feedback, remember to publish your drafts!

@finnbear
Copy link
Author

finnbear commented Apr 4, 2024

This is superseded by #66684 🚀

@finnbear finnbear closed this Apr 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arch-wasm WebAssembly issues cla: yes Used by googlebot to label PRs as having a valid CLA. The text of this label should not change.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

syscall/js: increase performance of Call, Invoke, and New by not allowing new slices to escape onto the heap
3 participants