84 karma · joined Dec 27, 2022 (3y ago) · 19 submissions
1.
Show HN: Build WebExtensions in Go, a Native Way(github.com ↗)
3 points · by Leotard6963 · 3y ago · discuss
comment 3y ago · link · parent
Precondition: in Go, each goroutine has its own `g` structure (like TCB, but for controlling goroutines) and its address is stored in the `g` register, which is reserved by the go compiler, active goroutines can call a function runtime.getg() to obtain the value of the `g` register.
In the official runtime, the `g` structure is a concrete type and the scheduler is implemented globally in the runtime package.
But we have made the `g` structure customizable (still requires a fixed header expected by the function prologue & epilogue and linker), so custom g implementations may contain a link to a custom scheduler, and the scheduler can decide how to handle an active goroutine.
> "goroutines" that automatically suspend themselves
The automatic suspend and resume happens by
- calling `runtime.entersyscall` from a running goroutine - and calling `runtime.exitsyscall` from an finished systemstack
all these operations can interact with the custom `g`, thus can interact with the custom scheduler.
comment 3y ago · link · parent
The feature of generating go packages from C in `ffigen` still needs significant work to be done (mostly dealing with C calling conventions on different platforms), and the generated go package only works with dynamic libraries (as it uses the pragma `go:cgo_import_dynamic`).
comment 3y ago · link · parent
And in brief introduction, the `g` register is a non-scratch register, and is preserved by the go compiler, it stores the poitner to current goroutine (type `g` in the official runtime, a structure serves the similar purpose of TCB), and since all general purpose registers are thread local, the goroutine may enjoy some thread-local features without any thread-local requirements to the running environment.
[1]: https://github.com/golang/go/blob/master/src/cmd/compile/abi...
comment 3y ago · link · parent
The problem is, in order to build a std, we have to use a lot of <ABIInternal> stuff in assembly, and use internal/abi.FuncPCABIInternal in Go, so we can not make any promise regarding to the inter-std-compatibility for the time being, unless the Go project decided to make these features stable.
comment 3y ago · link · parent
And Yes, we are defining brand new interfaces as well, in order to improve usability and fix inconsistent user experience.
For your specific example `os.WriteFile` (names mentioned below may subject to change):
The api won't make it inside the std, because the local filesystem will be implemented as a `fs.FS` (concrete type `*sysfs.FS`), so you can have multiple `sysfs.FS` instances each at its own working directory, and the `os.WriteFile` will be replaced with `fs.WriteFile(fsys fs.FS, path *fs.PathBuf, wctx *io.WriteContext) io.Status`, where `fs.PathBuf` is our solution to handle all kinds of path styles (windows, unix, cygwin, utf8, utf16, end-null) and operations (join, base, dir).
comment 3y ago · link · parent
For variables you are sure living on the stack, you can use `mark.NoEscape(&v)` to obtain its pointer (a hack to cheat escape analysis found in the official runtime & reflect package), and the variable will be freed on function return.
For variables you want to control lifecycle, call `alloc.New` or `alloc.Make` with a explicit allocator, which can be an allocator whose storage is on the previous call stack (and there is a default goroutine local allocator thread.G().G().DefaultAlloc()) to allocate a non-stack-local variable, and call `alloc.Free` with the same allocator to free that variable.
The use of `make` is discouraged as go compiler does mid-stack inlining and escape analysis, so the return value of a `make` can be either on stack or on heap, thus to free it, the allocator needs to compare the address to thread.G().Stack.Lo and thread.G().Stack.Hi.
comment 3y ago · link · parent
We decided to make it this way so:
1) you can import packages from the pcz std module like you do with any other Go package without setting `GOROOT` to an unofficial std.
2) and because of 1), you can import packages from pcz std in your go application but still using the `go` command rather the `pcz` command.
In the future, we may provide drop-in replacement for applications using official std by introducing a compatibility module and a cli flag to redirect imports using packages from the official std to that module.
comment 3y ago · link · parent
Neither, though the final result can be similar, but at project level, tinygo and pcz has very different approaches to achieve the final result:
- tinygo chose the hard path to write a llvm backed toolchain with some custom behavior that compiles Go code (using official std) to llvm targets.
- pcz chose the easy path to write a stdlib from scratch that compiles to desired targets using the official Go toolchain.
One major benefit of pcz's approach is you can import packages from the pcz std module like you do with any other Go package, the development experience can remain unchanged.
comment 3y ago · link · parent
Were the confusion caused by the choice of project name `pcz` instead of `xxxstd`? or any suggestion for us to improve?
comment 3y ago · link · parent
re: goroutines
the official go runtime spawns goroutines during program startup, which makes the usage of M:N goruntine mandatory, and they have to call lockOSThread()[1] before initializing packages for certain applications (mostly GUI application).
We like the design of goroutines (especially the idea of a `g` register), but `pcz` currently doesn't have goroutine support, and we are making it possible to spawn goroutines with custom allocator (with or without gc) and scheduler attached, so you can chose 1:1, M:N model for goroutines on your own (as decided by the scheduler attached).
you can find more details in the project ROADMAP.md[2]
[1]: https://github.com/golang/go/blob/352c8835e7609ad72872b5a63b... [2]: https://github.com/primecitizens/pcz/blob/master/ROADMAP.md
comment 3y ago · link · parent
We don't like it as well, just do not enable it :^
re: RAII
It is not possible to add RAII support without updating the compiler to call specific methods automatically, so currently it is not an option (as we are using unmodified official toolchain).
But the `g` register defined in Go is of great value, and we are making use of it to provide custom goroutine support, which means you can have custom allocator and scheduler for specific goroutines, so that you can have some of them with GC enabled and others not.
re: compare to V
pcz is a stdlib (plus a cli tool to build), not a new language, you still write Go code but in a slightly different style.
17.
Show HN: A new stdlib for Golang focusing on platform native support(github.com ↗)
154 points · by Leotard6963 · 3y ago · 82 comments
comment 3y ago · link · parent
take timezone for example, time.Time.In [2] could be a good startpoint to learn how they do the conversion.
[1]: https://github.com/golang/go/tree/master/src/time [2]: https://github.com/golang/go/blob/38cfb3be9d4868334562767771...