mercurial/bitmanipulation.h
author Arseniy Alekseyev <aalekseyev@janestreet.com>
Wed, 31 May 2023 10:37:55 +0100
changeset 50672 3b56395404a1
parent 48274 d86908050375
permissions -rw-r--r--
stream-clone: avoid opening a revlog in case we do not need it Opening an revlog has a cost, especially if it is inline as we have to scan the file and construct an index. To prevent the associated slowdown, we just do a minimal scan to check that an inline file is still inline, and simply stream the file without creating a revlog when we can. This provides a big boost compared to the previous changeset, even if the full generation is still penalized by the initial gathering of information. All benchmarks are run on linux with Python 3.10.7. # benchmark.name = hg.exchange.stream.generate # benchmark.variants.version = v2 ### Compared to the previous changesets We get a large win all across the board! # mercurial-2018-08-01-zstd-sparse-revlog before: 0.250694 seconds after: 0.105986 seconds (-57.72%) # pypy-2018-08-01-zstd-sparse-revlog before: 3.885657 seconds after: 1.709748 seconds (-56.00%) # netbeans-2018-08-01-zstd-sparse-revlog before: 16.679371 seconds after: 7.687469 seconds (-53.91%) # mozilla-central-2018-08-01-zstd-sparse-revlog before: 38.575482 seconds after: 17.520316 seconds (-54.58%) # mozilla-try-2019-02-18-zstd-sparse-revlog before: 81.160994 seconds after: 37.073753 seconds (-54.32%) ### Compared to 6.4.3 We are still significantly slower than 6.4.3, the extra time is usually twice slower than the extra time we observe on the locked section, which is a quite interesting information. Except for mercurial-central that is much faster. That discrepancy is not really explained yet. # mercurial-2018-08-01-zstd-sparse-revlog 6.4.3: 0.072560 seconds after: 0.105986 seconds (+46.07%) (- 0.03 seconds) # pypy-2018-08-01-zstd-sparse-revlog 6.4.3: 1.211193 seconds after: 1.709748 seconds (+41.16%) (-0.45 seconds) # netbeans-2018-08-01-zstd-sparse-revlog 6.4.3: 4.932843 seconds after: 7.687469 seconds (+55.84%) (-2.75 seconds) # mozilla-central-2018-08-01-zstd-sparse-revlog 6.4.3: 34.012226 seconds after: 17.520316 seconds (-48.49%) (-16.49 seconds) # mozilla-try-2019-02-18-zstd-sparse-revlog 6.4.3: 23.850555 seconds after: 37.073753 seconds (+55.44%) (+13.22 seconds)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
48274
d86908050375 hg: remove reserved identifiers
David Demelier <markand@malikania.fr>
parents: 46707
diff changeset
     1
#ifndef HG_BITMANIPULATION_H
d86908050375 hg: remove reserved identifiers
David Demelier <markand@malikania.fr>
parents: 46707
diff changeset
     2
#define HG_BITMANIPULATION_H
29444
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
     3
32646
b4356d1cf3e4 bitmanipulation: add missing include of string.h
Martin von Zweigbergk <martinvonz@google.com>
parents: 29444
diff changeset
     4
#include <string.h>
b4356d1cf3e4 bitmanipulation: add missing include of string.h
Martin von Zweigbergk <martinvonz@google.com>
parents: 29444
diff changeset
     5
29444
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
     6
#include "compat.h"
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
     7
46707
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38303
diff changeset
     8
/* Reads a 64 bit integer from big-endian bytes. Assumes that the data is long
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38303
diff changeset
     9
 enough */
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38303
diff changeset
    10
static inline uint64_t getbe64(const char *c)
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38303
diff changeset
    11
{
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38303
diff changeset
    12
	const unsigned char *d = (const unsigned char *)c;
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38303
diff changeset
    13
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38303
diff changeset
    14
	return ((((uint64_t)d[0]) << 56) | (((uint64_t)d[1]) << 48) |
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38303
diff changeset
    15
	        (((uint64_t)d[2]) << 40) | (((uint64_t)d[3]) << 32) |
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38303
diff changeset
    16
	        (((uint64_t)d[4]) << 24) | (((uint64_t)d[5]) << 16) |
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38303
diff changeset
    17
	        (((uint64_t)d[6]) << 8) | (d[7]));
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38303
diff changeset
    18
}
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38303
diff changeset
    19
29444
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    20
static inline uint32_t getbe32(const char *c)
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    21
{
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    22
	const unsigned char *d = (const unsigned char *)c;
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    23
38303
1fb2510cf8c8 bitmanipulation: fix undefined behavior in bit shift in getbe32
Augie Fackler <augie@google.com>
parents: 34697
diff changeset
    24
	return ((((uint32_t)d[0]) << 24) | (((uint32_t)d[1]) << 16) |
1fb2510cf8c8 bitmanipulation: fix undefined behavior in bit shift in getbe32
Augie Fackler <augie@google.com>
parents: 34697
diff changeset
    25
	        (((uint32_t)d[2]) << 8) | (d[3]));
29444
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    26
}
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    27
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    28
static inline int16_t getbeint16(const char *c)
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    29
{
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    30
	const unsigned char *d = (const unsigned char *)c;
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    31
34697
ce77b0563228 bitmanipulation: reformat with clang-format
Augie Fackler <augie@google.com>
parents: 32646
diff changeset
    32
	return ((d[0] << 8) | (d[1]));
29444
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    33
}
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    34
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    35
static inline uint16_t getbeuint16(const char *c)
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    36
{
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    37
	const unsigned char *d = (const unsigned char *)c;
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    38
34697
ce77b0563228 bitmanipulation: reformat with clang-format
Augie Fackler <augie@google.com>
parents: 32646
diff changeset
    39
	return ((d[0] << 8) | (d[1]));
29444
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    40
}
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    41
46707
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38303
diff changeset
    42
/* Writes a 64 bit integer to bytes in a big-endian format.
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38303
diff changeset
    43
 Assumes that the buffer is long enough */
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38303
diff changeset
    44
static inline void putbe64(uint64_t x, char *c)
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38303
diff changeset
    45
{
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38303
diff changeset
    46
	c[0] = (x >> 56) & 0xff;
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38303
diff changeset
    47
	c[1] = (x >> 48) & 0xff;
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38303
diff changeset
    48
	c[2] = (x >> 40) & 0xff;
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38303
diff changeset
    49
	c[3] = (x >> 32) & 0xff;
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38303
diff changeset
    50
	c[4] = (x >> 24) & 0xff;
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38303
diff changeset
    51
	c[5] = (x >> 16) & 0xff;
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38303
diff changeset
    52
	c[6] = (x >> 8) & 0xff;
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38303
diff changeset
    53
	c[7] = (x)&0xff;
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38303
diff changeset
    54
}
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38303
diff changeset
    55
29444
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    56
static inline void putbe32(uint32_t x, char *c)
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    57
{
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    58
	c[0] = (x >> 24) & 0xff;
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    59
	c[1] = (x >> 16) & 0xff;
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    60
	c[2] = (x >> 8) & 0xff;
34697
ce77b0563228 bitmanipulation: reformat with clang-format
Augie Fackler <augie@google.com>
parents: 32646
diff changeset
    61
	c[3] = (x)&0xff;
29444
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    62
}
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    63
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    64
static inline double getbefloat64(const char *c)
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    65
{
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    66
	const unsigned char *d = (const unsigned char *)c;
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    67
	double ret;
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    68
	int i;
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    69
	uint64_t t = 0;
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    70
	for (i = 0; i < 8; i++) {
34697
ce77b0563228 bitmanipulation: reformat with clang-format
Augie Fackler <augie@google.com>
parents: 32646
diff changeset
    71
		t = (t << 8) + d[i];
29444
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    72
	}
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    73
	memcpy(&ret, &t, sizeof(t));
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    74
	return ret;
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    75
}
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    76
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    77
#endif