aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.benchmarks/src/org/eclipse/jgit/benchmarks/RawTextBenchmark.java
blob: 19297ebebb1b606ec6055ba61a753a96fd6018c5 (plain)
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*
 * Copyright (C) 2022, Matthias Sohn <matthias.sohn@sap.com> and others
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Distribution License v. 1.0 which is available at
 * https://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */
package org.eclipse.jgit.benchmarks;

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.TearDown;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.infra.Blackhole;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.util.concurrent.TimeUnit;

import static org.eclipse.jgit.diff.RawText.getBufferSize;
import static org.eclipse.jgit.diff.RawText.isBinary;
import static org.eclipse.jgit.diff.RawText.isCrLfText;

@State(Scope.Thread)
public class RawTextBenchmark {

	@State(Scope.Benchmark)
	public static class BenchmarkState {

		@Param({"1", "2", "3", "4", "5", "6"})
		int testIndex;

		@Param({"false", "true"})
		boolean complete;

		byte[] bytes;

		@Setup
		public void setupBenchmark() {
			switch (testIndex) {
				case 1: {
					byte[] tmpBytes = "a".repeat(102400).getBytes();
					bytes = tmpBytes;
					break;
				}
				case 2: {
					byte[] tmpBytes = "a".repeat(102400).getBytes();
					byte[] tmpBytes2 = new byte[tmpBytes.length + 1];
					System.arraycopy(tmpBytes, 0, tmpBytes2, 0, tmpBytes.length);
					tmpBytes2[500] = '\0';
					tmpBytes2[tmpBytes.length] = '\0';
					bytes = tmpBytes2;
					break;
				}
				case 3: {
					byte[] tmpBytes = "a".repeat(102400).getBytes();
					byte[] tmpBytes2 = new byte[tmpBytes.length + 1];
					System.arraycopy(tmpBytes, 0, tmpBytes2, 0, tmpBytes.length);
					tmpBytes2[500] = '\r';
					tmpBytes2[tmpBytes.length] = '\r';
					bytes = tmpBytes2;
					break;
				}
				case 4: {
					byte[] tmpBytes = "a".repeat(102400).getBytes();
					byte[] tmpBytes2 = new byte[tmpBytes.length + 1];
					System.arraycopy(tmpBytes, 0, tmpBytes2, 0, tmpBytes.length);
					tmpBytes2[499] = '\r';
					tmpBytes2[500] = '\n';
					tmpBytes2[tmpBytes.length - 1] = '\r';
					tmpBytes2[tmpBytes.length] = '\n';
					bytes = tmpBytes2;
					break;
				}
				case 5: {
					byte[] tmpBytes = "a".repeat(102400).getBytes();
					tmpBytes[0] = '\0';
					bytes = tmpBytes;
					break;
				}
				case 6: {
					byte[] tmpBytes = "a".repeat(102400).getBytes();
					tmpBytes[0] = '\r';
					bytes = tmpBytes;
					break;
				}
				default:
			}
		}

		@TearDown
		public void teardown() {
		}
	}

	@Benchmark
	@BenchmarkMode({Mode.AverageTime})
	@OutputTimeUnit(TimeUnit.NANOSECONDS)
	@Warmup(iterations = 2, time = 5, timeUnit = TimeUnit.SECONDS)
	@Measurement(iterations = 2, time = 5, timeUnit = TimeUnit.SECONDS)
	@Fork(1)
	public void testIsCrLfTextOld(Blackhole blackhole, BenchmarkState state) {
		blackhole.consume(
				isCrLfTextOld(
						state.bytes,
						state.bytes.length,
						state.complete
				)
		);
	}

	@Benchmark
	@BenchmarkMode({Mode.AverageTime})
	@OutputTimeUnit(TimeUnit.NANOSECONDS)
	@Warmup(iterations = 2, time = 5, timeUnit = TimeUnit.SECONDS)
	@Measurement(iterations = 2, time = 5, timeUnit = TimeUnit.SECONDS)
	@Fork(1)
	public void testIsCrLfTextNewCandidate1(Blackhole blackhole, BenchmarkState state) {
		blackhole.consume(
				isCrLfTextNewCandidate1(
						state.bytes,
						state.bytes.length,
						state.complete
				)
		);
	}

	@Benchmark
	@BenchmarkMode({Mode.AverageTime})
	@OutputTimeUnit(TimeUnit.NANOSECONDS)
	@Warmup(iterations = 2, time = 5, timeUnit = TimeUnit.SECONDS)
	@Measurement(iterations = 2, time = 5, timeUnit = TimeUnit.SECONDS)
	@Fork(1)
	public void testIsCrLfTextNewCandidate2(Blackhole blackhole, BenchmarkState state) {
		blackhole.consume(
				isCrLfTextNewCandidate2(
						state.bytes,
						state.bytes.length,
						state.complete
				)
		);
	}

	@Benchmark
	@BenchmarkMode({Mode.AverageTime})
	@OutputTimeUnit(TimeUnit.NANOSECONDS)
	@Warmup(iterations = 2, time = 5, timeUnit = TimeUnit.SECONDS)
	@Measurement(iterations = 2, time = 5, timeUnit = TimeUnit.SECONDS)
	@Fork(1)
	public void testIsCrLfTextNewCandidate3(Blackhole blackhole, BenchmarkState state) {
		blackhole.consume(
				isCrLfTextNewCandidate3(
						state.bytes,
						state.bytes.length,
						state.complete
				)
		);
	}

	@Benchmark
	@BenchmarkMode({Mode.AverageTime})
	@OutputTimeUnit(TimeUnit.NANOSECONDS)
	@Warmup(iterations = 2, time = 5, timeUnit = TimeUnit.SECONDS)
	@Measurement(iterations = 2, time = 5, timeUnit = TimeUnit.SECONDS)
	@Fork(1)
	public void testIsCrLfTextNew(Blackhole blackhole, BenchmarkState state) {
		blackhole.consume(
				isCrLfText(
						state.bytes,
						state.bytes.length,
						state.complete
				)
		);
	}

	@Benchmark
	@BenchmarkMode({Mode.AverageTime})
	@OutputTimeUnit(TimeUnit.NANOSECONDS)
	@Warmup(iterations = 2, time = 5, timeUnit = TimeUnit.SECONDS)
	@Measurement(iterations = 2, time = 5, timeUnit = TimeUnit.SECONDS)
	@Fork(1)
	public void testIsBinaryOld(Blackhole blackhole, BenchmarkState state) {
		blackhole.consume(
				isBinaryOld(
						state.bytes,
						state.bytes.length,
						state.complete
				)
		);
	}


	@Benchmark
	@BenchmarkMode({Mode.AverageTime})
	@OutputTimeUnit(TimeUnit.NANOSECONDS)
	@Warmup(iterations = 2, time = 5, timeUnit = TimeUnit.SECONDS)
	@Measurement(iterations = 2, time = 5, timeUnit = TimeUnit.SECONDS)
	@Fork(1)
	public void testIsBinaryNew(Blackhole blackhole, BenchmarkState state) {
		blackhole.consume(
				isBinary(
						state.bytes,
						state.bytes.length,
						state.complete
				)
		);
	}


	/**
	 * Determine heuristically whether a byte array represents binary (as
	 * opposed to text) content.
	 *
	 * @param raw
	 *			the raw file content.
	 * @param length
	 *			number of bytes in {@code raw} to evaluate. This should be
	 *			{@code raw.length} unless {@code raw} was over-allocated by
	 *			the caller.
	 * @param complete
	 *			whether {@code raw} contains the whole data
	 * @return true if raw is likely to be a binary file, false otherwise
	 * @since 6.0
	 */
	public static boolean isBinaryOld(byte[] raw, int length, boolean complete) {
		// Similar heuristic as C Git. Differences:
		// - limited buffer size; may be only the beginning of a large blob
		// - no counting of printable vs. non-printable bytes < 0x20 and 0x7F
		int maxLength = getBufferSize();
		boolean isComplete = complete;
		if (length > maxLength) {
			// We restrict the length in all cases to getBufferSize() to get
			// predictable behavior. Sometimes we load streams, and sometimes we
			// have the full data in memory. With streams, we never look at more
			// than the first getBufferSize() bytes. If we looked at more when
			// we have the full data, different code paths in JGit might come to
			// different conclusions.
			length = maxLength;
			isComplete = false;
		}
		byte last = 'x'; // Just something inconspicuous.
		for (int ptr = 0; ptr < length; ptr++) {
			byte curr = raw[ptr];
			if (isBinary(curr, last)) {
				return true;
			}
			last = curr;
		}
		if (isComplete) {
			// Buffer contains everything...
			return last == '\r'; // ... so this must be a lone CR
		}
		return false;
	}

	/**
	 * Determine heuristically whether a byte array represents text content
	 * using CR-LF as line separator.
	 *
	 * @param raw	  the raw file content.
	 * @param length   number of bytes in {@code raw} to evaluate.
	 * @param complete whether {@code raw} contains the whole data
	 * @return {@code true} if raw is likely to be CR-LF delimited text,
	 * {@code false} otherwise
	 * @since 6.0
	 */
	public static boolean isCrLfTextOld(byte[] raw, int length, boolean complete) {
		boolean has_crlf = false;
		byte last = 'x'; // Just something inconspicuous
		for (int ptr = 0; ptr < length; ptr++) {
			byte curr = raw[ptr];
			if (isBinary(curr, last)) {
				return false;
			}
			if (curr == '\n' && last == '\r') {
				has_crlf = true;
			}
			last = curr;
		}
		if (last == '\r') {
			if (complete) {
				// Lone CR: it's binary after all.
				return false;
			}
			// Tough call. If the next byte, which we don't have, would be a
			// '\n', it'd be a CR-LF text, otherwise it'd be binary. Just decide
			// based on what we already scanned; it wasn't binary until now.
		}
		return has_crlf;
	}

	/**
	 * Determine heuristically whether a byte array represents text content
	 * using CR-LF as line separator.
	 *
	 * @param raw
	 *			the raw file content.
	 * @param length
	 *			number of bytes in {@code raw} to evaluate.
	 * @return {@code true} if raw is likely to be CR-LF delimited text,
	 *		 {@code false} otherwise
	 * @param complete
	 *			whether {@code raw} contains the whole data
	 * @since 6.0
	 */
	public static boolean isCrLfTextNewCandidate1(byte[] raw, int length, boolean complete) {
		boolean has_crlf = false;

		// first detect empty
		if (length <= 0) {
			return false;
		}

		// next detect '\0'
		for (int reversePtr = length - 1; reversePtr >= 0; --reversePtr) {
			if (raw[reversePtr] == '\0') {
				return false;
			}
		}

		// if '\r' be last, then if complete then return non-crlf
		if (raw[length - 1] == '\r' && complete) {
			return false;
		}

		for (int ptr = 0; ptr < length - 1; ptr++) {
			byte curr = raw[ptr];
			if (curr == '\r') {
				byte next = raw[ptr + 1];
				if (next != '\n') {
					return false;
				}
				// else
				// we have crlf here
				has_crlf = true;
				// as next is '\n', it can never be '\r', just skip it from next check
				++ptr;
			}
		}

		return has_crlf;
	}

	/**
	 * Determine heuristically whether a byte array represents text content
	 * using CR-LF as line separator.
	 *
	 * @param raw
	 *			the raw file content.
	 * @param length
	 *			number of bytes in {@code raw} to evaluate.
	 * @return {@code true} if raw is likely to be CR-LF delimited text,
	 *		 {@code false} otherwise
	 * @param complete
	 *			whether {@code raw} contains the whole data
	 * @since 6.0
	 */
	public static boolean isCrLfTextNewCandidate2(byte[] raw, int length, boolean complete) {
		boolean has_crlf = false;

		// first detect empty
		if (length <= 0) {
			return false;
		}

		// if '\r' be last, then if complete then return non-crlf
		byte last = raw[length - 1];
		if (last == '\0' || last == '\r' && complete) {
			return false;
		}

		for (int ptr = 0; ptr < length - 1; ptr++) {
			byte b = raw[ptr];
			switch (b) {
				case  '\0':
					return false;
				case '\r': {
					++ptr;
					b = raw[ptr];
					if (b != '\n') {
						return false;
					}
					// else
					// we have crlf here
					has_crlf = true;
					// as next is '\n', it can never be '\r', just skip it from next check
					break;
				}
				default:
					// do nothing;
					break;
			}
		}

		return has_crlf;
	}

	/**
	 * Determine heuristically whether a byte array represents text content
	 * using CR-LF as line separator.
	 *
	 * @param raw
	 *			the raw file content.
	 * @param length
	 *			number of bytes in {@code raw} to evaluate.
	 * @return {@code true} if raw is likely to be CR-LF delimited text,
	 *		 {@code false} otherwise
	 * @param complete
	 *			whether {@code raw} contains the whole data
	 * @since 6.0
	 */
	public static boolean isCrLfTextNewCandidate3(byte[] raw, int length, boolean complete) {
		boolean has_crlf = false;

		int ptr = -1;
		byte current;
		while (ptr < length - 2) {
			current = raw[++ptr];
			if ('\0' == current || '\r' == current && (raw[++ptr] != '\n' || !(has_crlf = true))) {
				return false;
			}
		}

		if (ptr == length - 2) {
			// if '\r' be last, then if isComplete then return binary
			current = raw[++ptr];
			if('\0' == current || '\r' == current && complete){
				return false;
			}
		}

		return has_crlf;
	}


	public static void main(String[] args) throws RunnerException {
		Options opt = new OptionsBuilder()
				.include(RawTextBenchmark.class.getSimpleName())
				.forks(1).jvmArgs("-ea").build();
		new Runner(opt).run();
	}
}