Skip to content

Conversation

@dgryski
Copy link
Member

@dgryski dgryski commented Feb 22, 2022

Fixes #2646

@dankegel
Copy link
Contributor

this pull request should also bring back the test deleted by #2603

src/os/env.go Outdated
func Environ() []string {
return syscall.Environ()
orig := syscall.Environ()
single := strings.Join(orig, "")
Copy link
Contributor

Choose a reason for hiding this comment

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

Upsteam does the copy in syscall.Environ(), we might want to do the same in case someone calls that directly?

@dgryski
Copy link
Member Author

dgryski commented Feb 23, 2022

That probably makes sense. I'll move this logic (or equivalent) there.

@dkegel-fastly
Copy link
Contributor

Would it make sense to do a single allocation to copy libc_environ and then point to bits of it? That might be faster...?

@dgryski
Copy link
Member Author

dgryski commented Feb 23, 2022

Yeah, I was torn about doing that because we need to loop over the environment a few times instead of just once. Let me try that approach and see what it looks like

@dgryski
Copy link
Member Author

dgryski commented Feb 23, 2022

Now with a single allocation.

Copy link
Contributor

@dkegel-fastly dkegel-fastly left a comment

Choose a reason for hiding this comment

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

LGTM

// copy over the bytes to the Go heap
copy(bs, envVar)
// convert trimmed slice to string
s := *(*string)(unsafe.Pointer(&bs))
Copy link
Member

Choose a reason for hiding this comment

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

This is a dirty trick... but I have to admit, this whole function isn't exactly clean and I wrote it originally.
(Dirty because it's not guaranteed that slice/string layout will stay the way it is now - although I don't see why it would need to change).

@aykevl
Copy link
Member

aykevl commented Mar 1, 2022

Now with a single allocation.

Note that this can have the side effect of keeping more alive than needed. Imagine you only need a single value out of the environment. So you call Environ(), pick the one value, and keep that string around. Now all the environment strings can't be garbage collected.
Maybe that's fine, just noting in case that wasn't the intention.

@aykevl aykevl merged commit bf23839 into tinygo-org:dev Mar 1, 2022
@dgryski
Copy link
Member Author

dgryski commented Mar 1, 2022

Good point about a single environment variable pinning the entire allocation. I think I'll add a comment to this function about that

@dkegel-fastly
Copy link
Contributor

Yeah, that went through my head too, I should have mentioned it.

@dankegel dankegel mentioned this pull request Mar 12, 2022
@dgryski dgryski deleted the dgryski/env-copy branch January 24, 2023 22:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants