Skip to content

Commit 3438c83

Browse files
committed
Optimized createHash() by avoiding expensive String operations.
1 parent 70f6af5 commit 3438c83

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

src/com/skjegstad/utils/BloomFilter.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -122,33 +122,37 @@ public BloomFilter(int bitSetSize, int expectedNumberOfFilterElements, int actua
122122
*
123123
* @param val specifies the input data.
124124
* @param charset specifies the encoding of the input data.
125+
* @param salt to use for the digest function
125126
* @return digest as long.
126127
*/
127-
public static long createHash(String val, Charset charset) {
128-
return createHash(val.getBytes(charset));
128+
public static int createHash(String val, Charset charset, byte salt) {
129+
return createHash(val.getBytes(charset), salt);
129130
}
130131

131132
/**
132133
* Generates a digest based on the contents of a String.
133134
*
134135
* @param val specifies the input data. The encoding is expected to be UTF-8.
136+
* @param salt to use for the digest function
135137
* @return digest as long.
136138
*/
137-
public static long createHash(String val) {
138-
return createHash(val, charset);
139+
public static int createHash(String val, byte salt) {
140+
return createHash(val, charset, salt);
139141
}
140142

141143
/**
142144
* Generates a digest based on the contents of an array of bytes.
143145
*
144146
* @param data specifies input data.
147+
* @param salt to use for the digest function
145148
* @return digest as long.
146149
*/
147-
public static long createHash(byte[] data) {
148-
long h = 0;
150+
public static int createHash(byte[] data, byte salt) {
151+
int h = 0;
149152
byte[] res;
150153

151154
synchronized (digestFunction) {
155+
digestFunction.update(salt);
152156
res = digestFunction.digest(data);
153157
}
154158

@@ -272,9 +276,9 @@ public void clear() {
272276
*/
273277
public void add(E element) {
274278
long hash;
275-
String valString = element.toString();
279+
byte[] data = element.toString().getBytes(charset);
276280
for (int x = 0; x < k; x++) {
277-
hash = createHash(valString + Integer.toString(x));
281+
hash = createHash(data, (byte)x);
278282
hash = hash % (long)bitSetSize;
279283
bitset.set(Math.abs((int)hash), true);
280284
}
@@ -300,9 +304,9 @@ public void addAll(Collection<? extends E> c) {
300304
*/
301305
public boolean contains(E element) {
302306
long hash;
303-
String valString = element.toString();
307+
byte[] data = element.toString().getBytes(charset);
304308
for (int x = 0; x < k; x++) {
305-
hash = createHash(valString + Integer.toString(x));
309+
hash = createHash(data, (byte)x);
306310
hash = hash % (long)bitSetSize;
307311
if (!bitset.get(Math.abs((int)hash)))
308312
return false;

test/com/skjegstad/utils/BloomFilterTest.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,14 @@ public void testConstructorCNK() throws Exception {
5656
public void testCreateHash_String() throws Exception {
5757
System.out.println("createHash");
5858
String val = UUID.randomUUID().toString();
59-
long result1 = BloomFilter.createHash(val);
60-
long result2 = BloomFilter.createHash(val);
59+
byte salt = 11;
60+
int result1 = BloomFilter.createHash(val, salt);
61+
int result2 = BloomFilter.createHash(val, salt);
6162
assertEquals(result2, result1);
62-
long result3 = BloomFilter.createHash(UUID.randomUUID().toString());
63+
long result3 = BloomFilter.createHash(UUID.randomUUID().toString(),salt);
6364
assertNotSame(result3, result2);
6465

65-
long result4 = BloomFilter.createHash(val.getBytes("UTF-8"));
66+
long result4 = BloomFilter.createHash(val.getBytes("UTF-8"), salt);
6667
assertEquals(result4, result1);
6768
}
6869

@@ -75,8 +76,9 @@ public void testCreateHash_byteArr() throws UnsupportedEncodingException {
7576
System.out.println("createHash");
7677
String val = UUID.randomUUID().toString();
7778
byte[] data = val.getBytes("UTF-8");
78-
long result1 = BloomFilter.createHash(data);
79-
long result2 = BloomFilter.createHash(val);
79+
byte salt = 11;
80+
long result1 = BloomFilter.createHash(data, salt);
81+
long result2 = BloomFilter.createHash(val, salt);
8082
assertEquals(result1, result2);
8183
}
8284

0 commit comments

Comments
 (0)