aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRunxi Yu <me@runxiyu.org>2025-04-05 14:09:15 +0800
committerRunxi Yu <me@runxiyu.org>2025-04-05 14:09:15 +0800
commit98826d198b228e725ceb5a9fcf1d936ad3817d8e (patch)
treeba1840795c2b46529128945f80a548f11daf9c00
parentReduce allocations when converting string to []byte (diff)
downloadforge-98826d198b228e725ceb5a9fcf1d936ad3817d8e.tar.gz
forge-98826d198b228e725ceb5a9fcf1d936ad3817d8e.tar.zst
forge-98826d198b228e725ceb5a9fcf1d936ad3817d8e.zip
Reduce unnecessary allocations when converting []byte to stringv0.1.22
-rw-r--r--git_plumbing.go4
-rw-r--r--http_handle_repo_index.go8
-rw-r--r--http_handle_repo_raw.go4
-rw-r--r--http_handle_repo_tree.go4
-rw-r--r--readme_to_html.go6
5 files changed, 13 insertions, 13 deletions
diff --git a/git_plumbing.go b/git_plumbing.go
index e787c59..15329ad 100644
--- a/git_plumbing.go
+++ b/git_plumbing.go
@@ -76,14 +76,14 @@ func buildTreeRecursive(ctx context.Context, repoPath, baseTree string, updates
if modeEnd < 0 {
return errors.New("invalid tree format")
}
- mode := string(data[i : i+modeEnd])
+ mode := bytesToString(data[i : i+modeEnd])
i += modeEnd + 1
nameEnd := bytes.IndexByte(data[i:], 0)
if nameEnd < 0 {
return errors.New("missing null after filename")
}
- name := string(data[i : i+nameEnd])
+ name := bytesToString(data[i : i+nameEnd])
i += nameEnd + 1
if i+20 > len(data) {
diff --git a/http_handle_repo_index.go b/http_handle_repo_index.go
index b0a5899..8f0a62b 100644
--- a/http_handle_repo_index.go
+++ b/http_handle_repo_index.go
@@ -91,10 +91,10 @@ func httpHandleRepoIndex(w http.ResponseWriter, req *http.Request, params map[st
commits = append(commits, commitDisplay{
Hash: hex.EncodeToString(id),
- Author: string(authorName),
- Email: string(authorEmail),
- Date: string(authorDate),
- Message: string(title),
+ Author: bytesToString(authorName),
+ Email: bytesToString(authorEmail),
+ Date: bytesToString(authorDate),
+ Message: bytesToString(title),
})
}
diff --git a/http_handle_repo_raw.go b/http_handle_repo_raw.go
index 5c8f626..d12fa39 100644
--- a/http_handle_repo_raw.go
+++ b/http_handle_repo_raw.go
@@ -98,7 +98,7 @@ func httpHandleRepoRaw(writer http.ResponseWriter, request *http.Request, params
return
}
files = append(files, displayTreeEntry{
- Name: string(name),
+ Name: bytesToString(name),
Mode: fmt.Sprintf("%06o", mode),
Size: size,
IsFile: typeCode == 2,
@@ -123,7 +123,7 @@ func httpHandleRepoRaw(writer http.ResponseWriter, request *http.Request, params
return
}
writer.Header().Set("Content-Type", "application/octet-stream")
- fmt.Fprint(writer, string(content))
+ fmt.Fprint(writer, bytesToString(content))
default:
errorPage500(writer, params, fmt.Sprintf("unknown object kind: %d", kind))
diff --git a/http_handle_repo_tree.go b/http_handle_repo_tree.go
index c834ad9..347026d 100644
--- a/http_handle_repo_tree.go
+++ b/http_handle_repo_tree.go
@@ -97,7 +97,7 @@ func httpHandleRepoTree(writer http.ResponseWriter, request *http.Request, param
}
files = append(files, displayTreeEntry{
- Name: string(name),
+ Name: bytesToString(name),
Mode: fmt.Sprintf("%06o", mode),
Size: size,
IsFile: typeCode == 2,
@@ -116,7 +116,7 @@ func httpHandleRepoTree(writer http.ResponseWriter, request *http.Request, param
errorPage500(writer, params, "error reading file content: "+err.Error())
return
}
- rendered := renderHighlightedFile(pathSpec, string(content))
+ rendered := renderHighlightedFile(pathSpec, bytesToString(content))
params["file_contents"] = rendered
renderTemplate(writer, "repo_tree_file", params)
diff --git a/readme_to_html.go b/readme_to_html.go
index c5cc706..a7a9cc3 100644
--- a/readme_to_html.go
+++ b/readme_to_html.go
@@ -44,7 +44,7 @@ func renderReadmeAtTree(tree *object.Tree) (string, template.HTML) {
func renderReadme(data []byte, filename string) (string, template.HTML) {
switch strings.ToLower(filename) {
case "readme":
- return "README", template.HTML("<pre>" + html.EscapeString(string(data)) + "</pre>") //#nosec G203
+ return "README", template.HTML("<pre>" + html.EscapeString(bytesToString(data)) + "</pre>") //#nosec G203
case "readme.md":
var buf bytes.Buffer
if err := markdownConverter.Convert(data, &buf); err != nil {
@@ -52,12 +52,12 @@ func renderReadme(data []byte, filename string) (string, template.HTML) {
}
return "README.md", template.HTML(bluemonday.UGCPolicy().SanitizeBytes(buf.Bytes())) //#nosec G203
case "readme.org":
- htmlStr, err := org.New().Parse(strings.NewReader(string(data)), filename).Write(org.NewHTMLWriter())
+ htmlStr, err := org.New().Parse(strings.NewReader(bytesToString(data)), filename).Write(org.NewHTMLWriter())
if err != nil {
return "Error fetching README", escapeHTML("Unable to render README: " + err.Error())
}
return "README.org", template.HTML(bluemonday.UGCPolicy().Sanitize(htmlStr)) //#nosec G203
default:
- return filename, template.HTML("<pre>" + html.EscapeString(string(data)) + "</pre>") //#nosec G203
+ return filename, template.HTML("<pre>" + html.EscapeString(bytesToString(data)) + "</pre>") //#nosec G203
}
}