| 1 |
use criterion::BenchmarkId; |
| 2 |
use criterion::Criterion; |
| 3 |
use criterion::criterion_group; |
| 4 |
use criterion::criterion_main; |
| 5 |
use jj_core::diff; |
| 6 |
|
| 7 |
fn unchanged_lines(count: usize) -> (String, String) { |
| 8 |
let mut lines = vec![]; |
| 9 |
for i in 0..count { |
| 10 |
lines.push(format!("left line {i}\n")); |
| 11 |
} |
| 12 |
(lines.join(""), lines.join("")) |
| 13 |
} |
| 14 |
|
| 15 |
fn modified_lines(count: usize) -> (String, String) { |
| 16 |
let mut left_lines = vec![]; |
| 17 |
let mut right_lines = vec![]; |
| 18 |
for i in 0..count { |
| 19 |
left_lines.push(format!("left line {i}\n")); |
| 20 |
right_lines.push(format!("right line {i}\n")); |
| 21 |
} |
| 22 |
(left_lines.join(""), right_lines.join("")) |
| 23 |
} |
| 24 |
|
| 25 |
fn reversed_lines(count: usize) -> (String, String) { |
| 26 |
let mut left_lines = vec![]; |
| 27 |
for i in 0..count { |
| 28 |
left_lines.push(format!("left line {i}\n")); |
| 29 |
} |
| 30 |
let mut right_lines = left_lines.clone(); |
| 31 |
right_lines.reverse(); |
| 32 |
(left_lines.join(""), right_lines.join("")) |
| 33 |
} |
| 34 |
|
| 35 |
fn bench_diff_lines(c: &mut Criterion) { |
| 36 |
let mut group = c.benchmark_group("bench_diff_lines"); |
| 37 |
for count in [1000, 10000] { |
| 38 |
let label = format!("{}k", count / 1000); |
| 39 |
group.bench_with_input( |
| 40 |
BenchmarkId::new("unchanged", &label), |
| 41 |
&unchanged_lines(count), |
| 42 |
|b, (left, right)| b.iter(|| diff::diff([left, right])), |
| 43 |
); |
| 44 |
group.bench_with_input( |
| 45 |
BenchmarkId::new("modified", &label), |
| 46 |
&modified_lines(count), |
| 47 |
|b, (left, right)| b.iter(|| diff::diff([left, right])), |
| 48 |
); |
| 49 |
group.bench_with_input( |
| 50 |
BenchmarkId::new("reversed", &label), |
| 51 |
&reversed_lines(count), |
| 52 |
|b, (left, right)| b.iter(|| diff::diff([left, right])), |
| 53 |
); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
fn bench_diff_git_git_read_tree_c(c: &mut Criterion) { |
| 58 |
c.bench_function("bench_diff_git_git_read_tree_c", |b| { |
| 59 |
b.iter(|| { |
| 60 |
diff::diff([ |
| 61 |
r##"/* |
| 62 |
* GIT - The information manager from hell |
| 63 |
* |
| 64 |
* Copyright (C) Linus Torvalds, 2005 |
| 65 |
*/ |
| 66 |
#include "#cache.h" |
| 67 |
|
| 68 |
static int unpack(unsigned char *sha1) |
| 69 |
{ |
| 70 |
void *buffer; |
| 71 |
unsigned long size; |
| 72 |
char type[20]; |
| 73 |
|
| 74 |
buffer = read_sha1_file(sha1, type, &size); |
| 75 |
if (!buffer) |
| 76 |
usage("unable to read sha1 file"); |
| 77 |
if (strcmp(type, "tree")) |
| 78 |
usage("expected a 'tree' node"); |
| 79 |
while (size) { |
| 80 |
int len = strlen(buffer)+1; |
| 81 |
unsigned char *sha1 = buffer + len; |
| 82 |
char *path = strchr(buffer, ' ')+1; |
| 83 |
unsigned int mode; |
| 84 |
if (size < len + 20 || sscanf(buffer, "%o", &mode) != 1) |
| 85 |
usage("corrupt 'tree' file"); |
| 86 |
buffer = sha1 + 20; |
| 87 |
size -= len + 20; |
| 88 |
printf("%o %s (%s)\n", mode, path, sha1_to_hex(sha1)); |
| 89 |
} |
| 90 |
return 0; |
| 91 |
} |
| 92 |
|
| 93 |
int main(int argc, char **argv) |
| 94 |
{ |
| 95 |
int fd; |
| 96 |
unsigned char sha1[20]; |
| 97 |
|
| 98 |
if (argc != 2) |
| 99 |
usage("read-tree <key>"); |
| 100 |
if (get_sha1_hex(argv[1], sha1) < 0) |
| 101 |
usage("read-tree <key>"); |
| 102 |
sha1_file_directory = getenv(DB_ENVIRONMENT); |
| 103 |
if (!sha1_file_directory) |
| 104 |
sha1_file_directory = DEFAULT_DB_ENVIRONMENT; |
| 105 |
if (unpack(sha1) < 0) |
| 106 |
usage("unpack failed"); |
| 107 |
return 0; |
| 108 |
} |
| 109 |
"##, |
| 110 |
r##"/* |
| 111 |
* GIT - The information manager from hell |
| 112 |
* |
| 113 |
* Copyright (C) Linus Torvalds, 2005 |
| 114 |
*/ |
| 115 |
#include "#cache.h" |
| 116 |
|
| 117 |
static void create_directories(const char *path) |
| 118 |
{ |
| 119 |
int len = strlen(path); |
| 120 |
char *buf = malloc(len + 1); |
| 121 |
const char *slash = path; |
| 122 |
|
| 123 |
while ((slash = strchr(slash+1, '/')) != NULL) { |
| 124 |
len = slash - path; |
| 125 |
memcpy(buf, path, len); |
| 126 |
buf[len] = 0; |
| 127 |
mkdir(buf, 0700); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
static int create_file(const char *path) |
| 132 |
{ |
| 133 |
int fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0600); |
| 134 |
if (fd < 0) { |
| 135 |
if (errno == ENOENT) { |
| 136 |
create_directories(path); |
| 137 |
fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0600); |
| 138 |
} |
| 139 |
} |
| 140 |
return fd; |
| 141 |
} |
| 142 |
|
| 143 |
static int unpack(unsigned char *sha1) |
| 144 |
{ |
| 145 |
void *buffer; |
| 146 |
unsigned long size; |
| 147 |
char type[20]; |
| 148 |
|
| 149 |
buffer = read_sha1_file(sha1, type, &size); |
| 150 |
if (!buffer) |
| 151 |
usage("unable to read sha1 file"); |
| 152 |
if (strcmp(type, "tree")) |
| 153 |
usage("expected a 'tree' node"); |
| 154 |
while (size) { |
| 155 |
int len = strlen(buffer)+1; |
| 156 |
unsigned char *sha1 = buffer + len; |
| 157 |
char *path = strchr(buffer, ' ')+1; |
| 158 |
char *data; |
| 159 |
unsigned long filesize; |
| 160 |
unsigned int mode; |
| 161 |
int fd; |
| 162 |
|
| 163 |
if (size < len + 20 || sscanf(buffer, "%o", &mode) != 1) |
| 164 |
usage("corrupt 'tree' file"); |
| 165 |
buffer = sha1 + 20; |
| 166 |
size -= len + 20; |
| 167 |
data = read_sha1_file(sha1, type, &filesize); |
| 168 |
if (!data || strcmp(type, "blob")) |
| 169 |
usage("tree file refers to bad file data"); |
| 170 |
fd = create_file(path); |
| 171 |
if (fd < 0) |
| 172 |
usage("unable to create file"); |
| 173 |
if (write(fd, data, filesize) != filesize) |
| 174 |
usage("unable to write file"); |
| 175 |
fchmod(fd, mode); |
| 176 |
close(fd); |
| 177 |
free(data); |
| 178 |
} |
| 179 |
return 0; |
| 180 |
} |
| 181 |
|
| 182 |
int main(int argc, char **argv) |
| 183 |
{ |
| 184 |
int fd; |
| 185 |
unsigned char sha1[20]; |
| 186 |
|
| 187 |
if (argc != 2) |
| 188 |
usage("read-tree <key>"); |
| 189 |
if (get_sha1_hex(argv[1], sha1) < 0) |
| 190 |
usage("read-tree <key>"); |
| 191 |
sha1_file_directory = getenv(DB_ENVIRONMENT); |
| 192 |
if (!sha1_file_directory) |
| 193 |
sha1_file_directory = DEFAULT_DB_ENVIRONMENT; |
| 194 |
if (unpack(sha1) < 0) |
| 195 |
usage("unpack failed"); |
| 196 |
return 0; |
| 197 |
} |
| 198 |
"##, |
| 199 |
]) |
| 200 |
}); |
| 201 |
}); |
| 202 |
} |
| 203 |
|
| 204 |
criterion_group!(benches, bench_diff_lines, bench_diff_git_git_read_tree_c,); |
| 205 |
criterion_main!(benches); |