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

Redundant Buffer Allocation & Copy #2736

Open
jfcg opened this issue Mar 25, 2022 · 0 comments
Open

Redundant Buffer Allocation & Copy #2736

jfcg opened this issue Mar 25, 2022 · 0 comments
Labels
enhancement New feature or request

Comments

@jfcg
Copy link

jfcg commented Mar 25, 2022

Hi,
Please see the discussion here. This piece of code:

package main

import (
	"fmt"
	"unsafe"
)

// ToLower converts ascii string s to lower-case
func ToLower(s string) string {
	if s == "" {
		return ""
	}
	buf := make([]byte, len(s))
	fmt.Println(unsafe.Pointer(&buf[0])) // just to identify buffer

	for i := 0; i < len(s); i++ {
		c := s[i]
		if 'A' <= c && c <= 'Z' {
			c |= 32
		}
		buf[i] = c
	}
	return string(buf)
}

func main() {
	s := ToLower("ASfgQW")

	fmt.Println(*(*unsafe.Pointer)(unsafe.Pointer(&s)))
}

shows that both Go and TinyGo compilers fail to detect that buf does not escape, can just be converted to a string and returned. Instead both compilers redundantly allocate a new buffer and copy bytes in return string(buf).

How can this be solved in TinyGo?

@deadprogram deadprogram added the enhancement New feature or request label Apr 23, 2022
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