Merge commit 'ad273dbe5dba7bd0e901270464e25fc1f030a5b5' as 'backend/goldmark'

This commit is contained in:
2026-05-24 09:40:13 +02:00
109 changed files with 54777 additions and 0 deletions
@@ -0,0 +1,5 @@
cmark-master
cmark_benchmark
main
cpu.prof
out.png
@@ -0,0 +1,33 @@
CMARK_BIN=cmark_benchmark
CMARK_RUN=LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:./cmark-master/build/src ./$(CMARK_BIN)
ifeq ($(OS),Windows_NT)
CMARK_BIN=cmark_benchmark.exe
CMARK_RUN=bash -c "PATH=./cmark-master/build/src:$${PATH} ./$(CMARK_BIN)"
endif
.PHONY: run
run: $(CMARK_BIN)
@ $(CMARK_RUN)
go run ./goldmark_benchmark.go
./cmark-master/Makefile:
wget -nc -O cmark.zip https://github.com/commonmark/cmark/archive/master.zip
unzip cmark.zip
rm -f cmark.zip
cd cmark-master && make
$(CMARK_BIN): ./cmark-master/Makefile cmark_benchmark.c
gcc -I./cmark-master/build/src -I./cmark-master/src cmark_benchmark.c -o $(CMARK_BIN) -L./cmark-master/build/src -lcmark; \
.PHONY: profile
profile:
go build -o main ./goldmark_benchmark.go
./main 1000 _data.md cpu.prof
go tool pprof -png main cpu.prof > out.png
rm -f main cpu.prof
.PHONY: clean
clean:
rm -f $(CMARK_BIN)
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,91 @@
#include <stdio.h>
#include <stdlib.h>
#ifdef WIN32
# include <windows.h>
#else
# include <sys/time.h>
# include <sys/resource.h>
#endif
#include "cmark.h"
#ifdef WIN32
double get_time()
{
LARGE_INTEGER t, f;
QueryPerformanceCounter(&t);
QueryPerformanceFrequency(&f);
return (double)t.QuadPart/(double)f.QuadPart;
}
#else
double get_time()
{
struct timeval t;
struct timezone tzp;
gettimeofday(&t, &tzp);
return t.tv_sec + t.tv_usec*1e-6;
}
#endif
int main(int argc, char **argv) {
char *markdown_file;
FILE *fp;
size_t size;
char *buf;
char *html;
double start, sum;
int i, n;
n = argc > 1 ? atoi(argv[1]) : 50;
markdown_file = argc > 2 ? argv[2] : "_data.md";
fp = fopen(markdown_file,"r");
if(fp == NULL){
fprintf(stderr, "can not open %s", markdown_file);
exit(1);
}
if(fseek(fp, 0, SEEK_END) != 0) {
fprintf(stderr, "can not seek %s", markdown_file);
exit(1);
}
if((size = ftell(fp)) < 0) {
fprintf(stderr, "can not get size of %s", markdown_file);
exit(1);
}
if(fseek(fp, 0, SEEK_SET) != 0) {
fprintf(stderr, "can not seek %s", markdown_file);
exit(1);
}
buf = malloc(sizeof(char) * size);
if(buf == NULL) {
fprintf(stderr, "can not allocate memory for %s", markdown_file);
exit(1);
}
if(fread(buf, 1, size, fp) < size) {
fprintf(stderr, "failed to read for %s", markdown_file);
exit(1);
}
fclose(fp);
for(i = 0; i < n; i++) {
start = get_time();
html = cmark_markdown_to_html(buf, size, CMARK_OPT_UNSAFE);
free(html);
sum += get_time() - start;
}
printf("----------- cmark -----------\n");
printf("file: %s\n", markdown_file);
printf("iteration: %d\n", n);
printf("average: %.10f sec\n", sum / (double)n);
free(buf);
return 0;
}
@@ -0,0 +1,59 @@
package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"runtime/pprof"
"strconv"
"time"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/renderer/html"
)
func main() {
n := 50
file := "_data.md"
if len(os.Args) > 1 {
n, _ = strconv.Atoi(os.Args[1])
}
if len(os.Args) > 2 {
file = os.Args[2]
}
if len(os.Args) > 3 {
f, err := os.Create(os.Args[3])
if err != nil {
log.Fatal(err)
}
defer f.Close()
if err := pprof.StartCPUProfile(f); err != nil {
log.Fatal(err)
}
defer pprof.StopCPUProfile()
}
source, err := ioutil.ReadFile(file)
if err != nil {
panic(err)
}
markdown := goldmark.New(goldmark.WithRendererOptions(html.WithXHTML(), html.WithUnsafe()))
var out bytes.Buffer
markdown.Convert([]byte(""), &out)
sum := time.Duration(0)
for i := 0; i < n; i++ {
start := time.Now()
out.Reset()
if err := markdown.Convert(source, &out); err != nil {
panic(err)
}
sum += time.Since(start)
}
fmt.Printf("------- goldmark -------\n")
fmt.Printf("file: %s\n", file)
fmt.Printf("iteration: %d\n", n)
fmt.Printf("average: %.10f sec\n", float64((int64(sum)/int64(n)))/1000000000.0)
}