-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Reduce memory usage when rendering markdown #20326
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Took me (surprisingly) way too long to understand where we reduce the memory usage until I noticed it...
Not only that, I also removed |
Co-authored-by: delvh <[email protected]>
|
It's different memory usage between
Yes |
} | ||
n = copy(bs, tagCleaner.ReplaceAll([]byte(nulCleaner.Replace(string(original[:n]))), []byte("<$1"))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not think it is correct.
The content is incomplete in the original
buffer, the tagCleaner
may not work correctly. And tagCleaner
is quite a complex regexp, I am not sure whether it works for incomplete content.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And one more thing, after the replacing, the returned string may be longer than before, it may overflow the bs
buffer.
The reduced memory is only correct for larger inputs. 1024 bytes (this PR use more memory):
1024 * 8 bytes (this PR use less memory):
And for reference 1024 * 128 bytes:
Bench code: diff --git a/bench/bench_test.go b/bench/bench_test.go
new file mode 100644
index 000000000..33d9279cc
--- /dev/null
+++ b/bench/bench_test.go
@@ -0,0 +1,17 @@
+package bench
+
+import (
+ "io"
+ "strings"
+ "testing"
+
+ "code.gitea.io/gitea/modules/markup"
+)
+
+func BenchmarkPostProcess(b *testing.B) {
+ input := strings.Repeat("a", 1024)
+ b.ReportAllocs()
+ for i := 0; i < b.N; i++ {
+ markup.PostProcess(&markup.RenderContext{}, strings.NewReader(input), io.Discard)
+ }
+} |
☝️ would be nice to also add this test into this pull |
closed as it's not always correct. |
This PR removed at least two whole markdown copy when rendering a document.