Skip to content
/ git Public
forked from git/git

Commit e127310

Browse files
vmggitster
authored andcommitted
ewah: compressed bitmap implementation
EWAH is a word-aligned compressed variant of a bitset (i.e. a data structure that acts as a 0-indexed boolean array for many entries). It uses a 64-bit run-length encoding (RLE) compression scheme, trading some compression for better processing speed. The goal of this word-aligned implementation is not to achieve the best compression, but rather to improve query processing time. As it stands right now, this EWAH implementation will always be more efficient storage-wise than its uncompressed alternative. EWAH arrays will be used as the on-disk format to store reachability bitmaps for all objects in a repository while keeping reasonable sizes, in the same way that JGit does. This EWAH implementation is a mostly straightforward port of the original `javaewah` library that JGit currently uses. The library is self-contained and has been embedded whole (4 files) inside the `ewah` folder to ease redistribution. The library is re-licensed under the GPLv2 with the permission of Daniel Lemire, the original author. The source code for the C version can be found on GitHub: https://github.com/vmg/libewok The original Java implementation can also be found on GitHub: https://github.com/lemire/javaewah [jc: stripped debug-only code per Peff's $gmane/239768] Signed-off-by: Vicent Marti <[email protected]> Signed-off-by: Jeff King <[email protected]> Helped-by: Ramsay Jones <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7e3dae4 commit e127310

File tree

7 files changed

+1599
-2
lines changed

7 files changed

+1599
-2
lines changed

Makefile

+9-2
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,8 @@ LIB_H += diff.h
667667
LIB_H += diffcore.h
668668
LIB_H += dir.h
669669
LIB_H += exec_cmd.h
670+
LIB_H += ewah/ewok.h
671+
LIB_H += ewah/ewok_rlw.h
670672
LIB_H += fetch-pack.h
671673
LIB_H += fmt-merge-msg.h
672674
LIB_H += fsck.h
@@ -800,6 +802,10 @@ LIB_OBJS += dir.o
800802
LIB_OBJS += editor.o
801803
LIB_OBJS += entry.o
802804
LIB_OBJS += environment.o
805+
LIB_OBJS += ewah/bitmap.o
806+
LIB_OBJS += ewah/ewah_bitmap.o
807+
LIB_OBJS += ewah/ewah_io.o
808+
LIB_OBJS += ewah/ewah_rlw.o
803809
LIB_OBJS += exec_cmd.o
804810
LIB_OBJS += fetch-pack.o
805811
LIB_OBJS += fsck.o
@@ -2474,8 +2480,9 @@ profile-clean:
24742480
$(RM) $(addsuffix *.gcno,$(addprefix $(PROFILE_DIR)/, $(object_dirs)))
24752481

24762482
clean: profile-clean coverage-clean
2477-
$(RM) *.o *.res block-sha1/*.o ppc/*.o compat/*.o compat/*/*.o xdiff/*.o vcs-svn/*.o \
2478-
builtin/*.o $(LIB_FILE) $(XDIFF_LIB) $(VCSSVN_LIB)
2483+
$(RM) *.o *.res block-sha1/*.o ppc/*.o compat/*.o compat/*/*.o
2484+
$(RM) xdiff/*.o vcs-svn/*.o ewah/*.o builtin/*.o
2485+
$(RM) $(LIB_FILE) $(XDIFF_LIB) $(VCSSVN_LIB)
24792486
$(RM) $(ALL_PROGRAMS) $(SCRIPT_LIB) $(BUILT_INS) git$X
24802487
$(RM) $(TEST_PROGRAMS) $(NO_INSTALL)
24812488
$(RM) -r bin-wrappers $(dep_dirs)

ewah/bitmap.c

+221
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
/**
2+
* Copyright 2013, GitHub, Inc
3+
* Copyright 2009-2013, Daniel Lemire, Cliff Moon,
4+
* David McIntosh, Robert Becho, Google Inc. and Veronika Zenz
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU General Public License
8+
* as published by the Free Software Foundation; either version 2
9+
* of the License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program; if not, write to the Free Software
18+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19+
*/
20+
#include "git-compat-util.h"
21+
#include "ewok.h"
22+
23+
#define MASK(x) ((eword_t)1 << (x % BITS_IN_WORD))
24+
#define BLOCK(x) (x / BITS_IN_WORD)
25+
26+
struct bitmap *bitmap_new(void)
27+
{
28+
struct bitmap *bitmap = ewah_malloc(sizeof(struct bitmap));
29+
bitmap->words = ewah_calloc(32, sizeof(eword_t));
30+
bitmap->word_alloc = 32;
31+
return bitmap;
32+
}
33+
34+
void bitmap_set(struct bitmap *self, size_t pos)
35+
{
36+
size_t block = BLOCK(pos);
37+
38+
if (block >= self->word_alloc) {
39+
size_t old_size = self->word_alloc;
40+
self->word_alloc = block * 2;
41+
self->words = ewah_realloc(self->words,
42+
self->word_alloc * sizeof(eword_t));
43+
44+
memset(self->words + old_size, 0x0,
45+
(self->word_alloc - old_size) * sizeof(eword_t));
46+
}
47+
48+
self->words[block] |= MASK(pos);
49+
}
50+
51+
void bitmap_clear(struct bitmap *self, size_t pos)
52+
{
53+
size_t block = BLOCK(pos);
54+
55+
if (block < self->word_alloc)
56+
self->words[block] &= ~MASK(pos);
57+
}
58+
59+
int bitmap_get(struct bitmap *self, size_t pos)
60+
{
61+
size_t block = BLOCK(pos);
62+
return block < self->word_alloc &&
63+
(self->words[block] & MASK(pos)) != 0;
64+
}
65+
66+
struct ewah_bitmap *bitmap_to_ewah(struct bitmap *bitmap)
67+
{
68+
struct ewah_bitmap *ewah = ewah_new();
69+
size_t i, running_empty_words = 0;
70+
eword_t last_word = 0;
71+
72+
for (i = 0; i < bitmap->word_alloc; ++i) {
73+
if (bitmap->words[i] == 0) {
74+
running_empty_words++;
75+
continue;
76+
}
77+
78+
if (last_word != 0)
79+
ewah_add(ewah, last_word);
80+
81+
if (running_empty_words > 0) {
82+
ewah_add_empty_words(ewah, 0, running_empty_words);
83+
running_empty_words = 0;
84+
}
85+
86+
last_word = bitmap->words[i];
87+
}
88+
89+
ewah_add(ewah, last_word);
90+
return ewah;
91+
}
92+
93+
struct bitmap *ewah_to_bitmap(struct ewah_bitmap *ewah)
94+
{
95+
struct bitmap *bitmap = bitmap_new();
96+
struct ewah_iterator it;
97+
eword_t blowup;
98+
size_t i = 0;
99+
100+
ewah_iterator_init(&it, ewah);
101+
102+
while (ewah_iterator_next(&blowup, &it)) {
103+
if (i >= bitmap->word_alloc) {
104+
bitmap->word_alloc *= 1.5;
105+
bitmap->words = ewah_realloc(
106+
bitmap->words, bitmap->word_alloc * sizeof(eword_t));
107+
}
108+
109+
bitmap->words[i++] = blowup;
110+
}
111+
112+
bitmap->word_alloc = i;
113+
return bitmap;
114+
}
115+
116+
void bitmap_and_not(struct bitmap *self, struct bitmap *other)
117+
{
118+
const size_t count = (self->word_alloc < other->word_alloc) ?
119+
self->word_alloc : other->word_alloc;
120+
121+
size_t i;
122+
123+
for (i = 0; i < count; ++i)
124+
self->words[i] &= ~other->words[i];
125+
}
126+
127+
void bitmap_or_ewah(struct bitmap *self, struct ewah_bitmap *other)
128+
{
129+
size_t original_size = self->word_alloc;
130+
size_t other_final = (other->bit_size / BITS_IN_WORD) + 1;
131+
size_t i = 0;
132+
struct ewah_iterator it;
133+
eword_t word;
134+
135+
if (self->word_alloc < other_final) {
136+
self->word_alloc = other_final;
137+
self->words = ewah_realloc(self->words,
138+
self->word_alloc * sizeof(eword_t));
139+
memset(self->words + original_size, 0x0,
140+
(self->word_alloc - original_size) * sizeof(eword_t));
141+
}
142+
143+
ewah_iterator_init(&it, other);
144+
145+
while (ewah_iterator_next(&word, &it))
146+
self->words[i++] |= word;
147+
}
148+
149+
void bitmap_each_bit(struct bitmap *self, ewah_callback callback, void *data)
150+
{
151+
size_t pos = 0, i;
152+
153+
for (i = 0; i < self->word_alloc; ++i) {
154+
eword_t word = self->words[i];
155+
uint32_t offset;
156+
157+
if (word == (eword_t)~0) {
158+
for (offset = 0; offset < BITS_IN_WORD; ++offset)
159+
callback(pos++, data);
160+
} else {
161+
for (offset = 0; offset < BITS_IN_WORD; ++offset) {
162+
if ((word >> offset) == 0)
163+
break;
164+
165+
offset += ewah_bit_ctz64(word >> offset);
166+
callback(pos + offset, data);
167+
}
168+
pos += BITS_IN_WORD;
169+
}
170+
}
171+
}
172+
173+
size_t bitmap_popcount(struct bitmap *self)
174+
{
175+
size_t i, count = 0;
176+
177+
for (i = 0; i < self->word_alloc; ++i)
178+
count += ewah_bit_popcount64(self->words[i]);
179+
180+
return count;
181+
}
182+
183+
int bitmap_equals(struct bitmap *self, struct bitmap *other)
184+
{
185+
struct bitmap *big, *small;
186+
size_t i;
187+
188+
if (self->word_alloc < other->word_alloc) {
189+
small = self;
190+
big = other;
191+
} else {
192+
small = other;
193+
big = self;
194+
}
195+
196+
for (i = 0; i < small->word_alloc; ++i) {
197+
if (small->words[i] != big->words[i])
198+
return 0;
199+
}
200+
201+
for (; i < big->word_alloc; ++i) {
202+
if (big->words[i] != 0)
203+
return 0;
204+
}
205+
206+
return 1;
207+
}
208+
209+
void bitmap_reset(struct bitmap *bitmap)
210+
{
211+
memset(bitmap->words, 0x0, bitmap->word_alloc * sizeof(eword_t));
212+
}
213+
214+
void bitmap_free(struct bitmap *bitmap)
215+
{
216+
if (bitmap == NULL)
217+
return;
218+
219+
free(bitmap->words);
220+
free(bitmap);
221+
}

0 commit comments

Comments
 (0)