File: diff_bench.rs

package info (click to toggle)
jujutsu 0.30.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 9,756 kB
  • sloc: sh: 283; makefile: 31
file content (205 lines) | stat: -rw-r--r-- 5,087 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
use criterion::criterion_group;
use criterion::criterion_main;
use criterion::BenchmarkId;
use criterion::Criterion;
use jj_lib::diff;

fn unchanged_lines(count: usize) -> (String, String) {
    let mut lines = vec![];
    for i in 0..count {
        lines.push(format!("left line {i}\n"));
    }
    (lines.join(""), lines.join(""))
}

fn modified_lines(count: usize) -> (String, String) {
    let mut left_lines = vec![];
    let mut right_lines = vec![];
    for i in 0..count {
        left_lines.push(format!("left line {i}\n"));
        right_lines.push(format!("right line {i}\n"));
    }
    (left_lines.join(""), right_lines.join(""))
}

fn reversed_lines(count: usize) -> (String, String) {
    let mut left_lines = vec![];
    for i in 0..count {
        left_lines.push(format!("left line {i}\n"));
    }
    let mut right_lines = left_lines.clone();
    right_lines.reverse();
    (left_lines.join(""), right_lines.join(""))
}

fn bench_diff_lines(c: &mut Criterion) {
    let mut group = c.benchmark_group("bench_diff_lines");
    for count in [1000, 10000] {
        let label = format!("{}k", count / 1000);
        group.bench_with_input(
            BenchmarkId::new("unchanged", &label),
            &unchanged_lines(count),
            |b, (left, right)| b.iter(|| diff::diff([left, right])),
        );
        group.bench_with_input(
            BenchmarkId::new("modified", &label),
            &modified_lines(count),
            |b, (left, right)| b.iter(|| diff::diff([left, right])),
        );
        group.bench_with_input(
            BenchmarkId::new("reversed", &label),
            &reversed_lines(count),
            |b, (left, right)| b.iter(|| diff::diff([left, right])),
        );
    }
}

fn bench_diff_git_git_read_tree_c(c: &mut Criterion) {
    c.bench_function("bench_diff_git_git_read_tree_c", |b| {
        b.iter(|| {
            diff::diff([
                r##"/*
 * GIT - The information manager from hell
 *
 * Copyright (C) Linus Torvalds, 2005
 */
#include "#cache.h"

static int unpack(unsigned char *sha1)
{
	void *buffer;
	unsigned long size;
	char type[20];

	buffer = read_sha1_file(sha1, type, &size);
	if (!buffer)
		usage("unable to read sha1 file");
	if (strcmp(type, "tree"))
		usage("expected a 'tree' node");
	while (size) {
		int len = strlen(buffer)+1;
		unsigned char *sha1 = buffer + len;
		char *path = strchr(buffer, ' ')+1;
		unsigned int mode;
		if (size < len + 20 || sscanf(buffer, "%o", &mode) != 1)
			usage("corrupt 'tree' file");
		buffer = sha1 + 20;
		size -= len + 20;
		printf("%o %s (%s)\n", mode, path, sha1_to_hex(sha1));
	}
	return 0;
}

int main(int argc, char **argv)
{
	int fd;
	unsigned char sha1[20];

	if (argc != 2)
		usage("read-tree <key>");
	if (get_sha1_hex(argv[1], sha1) < 0)
		usage("read-tree <key>");
	sha1_file_directory = getenv(DB_ENVIRONMENT);
	if (!sha1_file_directory)
		sha1_file_directory = DEFAULT_DB_ENVIRONMENT;
	if (unpack(sha1) < 0)
		usage("unpack failed");
	return 0;
}
"##,
                r##"/*
 * GIT - The information manager from hell
 *
 * Copyright (C) Linus Torvalds, 2005
 */
#include "#cache.h"

static void create_directories(const char *path)
{
	int len = strlen(path);
	char *buf = malloc(len + 1);
	const char *slash = path;

	while ((slash = strchr(slash+1, '/')) != NULL) {
		len = slash - path;
		memcpy(buf, path, len);
		buf[len] = 0;
		mkdir(buf, 0700);
	}
}

static int create_file(const char *path)
{
	int fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0600);
	if (fd < 0) {
		if (errno == ENOENT) {
			create_directories(path);
			fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0600);
		}
	}
	return fd;
}

static int unpack(unsigned char *sha1)
{
	void *buffer;
	unsigned long size;
	char type[20];

	buffer = read_sha1_file(sha1, type, &size);
	if (!buffer)
		usage("unable to read sha1 file");
	if (strcmp(type, "tree"))
		usage("expected a 'tree' node");
	while (size) {
		int len = strlen(buffer)+1;
		unsigned char *sha1 = buffer + len;
		char *path = strchr(buffer, ' ')+1;
		char *data;
		unsigned long filesize;
		unsigned int mode;
		int fd;

		if (size < len + 20 || sscanf(buffer, "%o", &mode) != 1)
			usage("corrupt 'tree' file");
		buffer = sha1 + 20;
		size -= len + 20;
		data = read_sha1_file(sha1, type, &filesize);
		if (!data || strcmp(type, "blob"))
			usage("tree file refers to bad file data");
		fd = create_file(path);
		if (fd < 0)
			usage("unable to create file");
		if (write(fd, data, filesize) != filesize)
			usage("unable to write file");
		fchmod(fd, mode);
		close(fd);
		free(data);
	}
	return 0;
}

int main(int argc, char **argv)
{
	int fd;
	unsigned char sha1[20];

	if (argc != 2)
		usage("read-tree <key>");
	if (get_sha1_hex(argv[1], sha1) < 0)
		usage("read-tree <key>");
	sha1_file_directory = getenv(DB_ENVIRONMENT);
	if (!sha1_file_directory)
		sha1_file_directory = DEFAULT_DB_ENVIRONMENT;
	if (unpack(sha1) < 0)
		usage("unpack failed");
	return 0;
}
"##,
            ])
        });
    });
}

criterion_group!(benches, bench_diff_lines, bench_diff_git_git_read_tree_c,);
criterion_main!(benches);