Reduce memory usage when rendering markdown#20326
Conversation
delvh
left a comment
There was a problem hiding this comment.
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 <dev.lh@web.de>
|
|
It's different memory usage between
Yes |
| return err | ||
| return n, err | ||
| } | ||
| n = copy(bs, tagCleaner.ReplaceAll([]byte(nulCleaner.Replace(string(original[:n]))), []byte("<$1"))) |
There was a problem hiding this comment.
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.
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.