Skip to content

Commit

Permalink
Avoid array duplication in Java implementation. (#9)
Browse files Browse the repository at this point in the history
* Do not copy arrays in Java
  • Loading branch information
nicolaasuni authored Mar 10, 2024
1 parent 94ac5e7 commit d43e2f6
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 77 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.6.0
1.7.0
2 changes: 1 addition & 1 deletion c/doc/Doxyfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ PROJECT_NAME = FarmHash64
# could be handy for archiving the generated documentation or if some version
# control system is used.

PROJECT_NUMBER = 1.6.0
PROJECT_NUMBER = 1.7.0

# Using the PROJECT_BRIEF tag one can provide an optional one line description
# for a project that appears at the top of each page and should give viewer a
Expand Down
42 changes: 21 additions & 21 deletions java/src/main/java/com/tecnick/farmhash64/FarmHash64.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ This is a Java port of the Fingerprint64 (farmhashna::Hash64) code from Google's
*/
package com.tecnick.farmhash64;

import java.util.Arrays;

public class FarmHash64 {

private static final int c1 = 0xcc9e2d51;
Expand Down Expand Up @@ -146,12 +144,12 @@ private static UInt128 weakHashLen32WithSeedsWords(long w, long x, long y, long
return result;
}

private static UInt128 weakHashLen32WithSeeds(byte[] s, long a, long b) {
private static UInt128 weakHashLen32WithSeeds(byte[] s, int idx, long a, long b) {
return weakHashLen32WithSeedsWords(
fetch64(s, 0),
fetch64(s, 8),
fetch64(s, 16),
fetch64(s, 24),
fetch64(s, idx + 0),
fetch64(s, idx + 8),
fetch64(s, idx + 16),
fetch64(s, idx + 24),
a,
b);
}
Expand Down Expand Up @@ -205,35 +203,37 @@ public static long farmhash64(byte[] s) {
// Set end so that after the loop we have 1 to 64 bytes left to process.
int endIdx = ((slen - 1) / 64) * 64;
int last64Idx = endIdx + ((slen - 1) & 63) - 63;
byte[] last64 = Arrays.copyOfRange(s, last64Idx, slen);
int idx = 0;

while (s.length > 64) {
x = rotate64(x + y + v.lo + fetch64(s, 8), 37) * k1;
y = rotate64(y + v.hi + fetch64(s, 48), 42) * k1;
while (slen > 64) {
x = rotate64(x + y + v.lo + fetch64(s, idx + 8), 37) * k1;
y = rotate64(y + v.hi + fetch64(s, idx + 48), 42) * k1;
x ^= w.hi;
y += v.lo + fetch64(s, 40);
y += v.lo + fetch64(s, idx + 40);
z = rotate64(z + w.lo, 33) * k1;
v = weakHashLen32WithSeeds(s, v.hi * k1, x + w.lo);
w = weakHashLen32WithSeeds(Arrays.copyOfRange(s, 32, s.length), z + w.hi, y + fetch64(s, 16));
v = weakHashLen32WithSeeds(s, idx, v.hi * k1, x + w.lo);
w = weakHashLen32WithSeeds(s, idx + 32, z + w.hi, y + fetch64(s, idx + 16));
tmp = x;
x = z;
z = tmp;
s = Arrays.copyOfRange(s, 64, s.length);
idx += 64;
slen -= 64;
}

long mul = k1 + ((z & 0xFFL) << 1);

// Make s point to the last 64 bytes of input.
s = last64;
idx = last64Idx;
w.lo += ((long) slen - 1) & 63;
v.lo += w.lo;
w.lo += v.lo;
x = rotate64(x + y + v.lo + fetch64(s, 8), 37) * mul;
y = rotate64(y + v.hi + fetch64(s, 48), 42) * mul;
x = rotate64(x + y + v.lo + fetch64(s, idx + 8), 37) * mul;
y = rotate64(y + v.hi + fetch64(s, idx + 48), 42) * mul;
x ^= w.hi * 9;
y += v.lo * 9 + fetch64(s, 40);
y += v.lo * 9 + fetch64(s, idx + 40);
z = rotate64(z + w.lo, 33) * mul;
v = weakHashLen32WithSeeds(s, v.hi * mul, x + w.lo);
w = weakHashLen32WithSeeds(Arrays.copyOfRange(s, 32, s.length), z + w.hi, y + fetch64(s, 16));
v = weakHashLen32WithSeeds(s, idx, v.hi * mul, x + w.lo);
w = weakHashLen32WithSeeds(s, idx + 32, z + w.hi, y + fetch64(s, idx + 16));
tmp = x;
x = z;
z = tmp;
Expand Down
1 change: 1 addition & 0 deletions python/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
.pytest_cache
c
farmhash64.egg-info
README.md

.cache
.coverage*
Expand Down
2 changes: 1 addition & 1 deletion python/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ build: clean version
# Remove any build artifact
.PHONY: clean
clean:
rm -rf venv target c Dockerfile htmlcov build dist .pytest_cache .cache .benchmarks ./test/*.so ./test/__pycache__ ./farmhash64/__pycache__ ./farmhash64.egg-info farmhash64.*.so
rm -rf venv target c Dockerfile htmlcov build dist .pytest_cache .cache .benchmarks ./test/*.so ./test/__pycache__ ./farmhash64/__pycache__ ./farmhash64.egg-info farmhash64.*.so README.md
find . -type f -name '*.pyc' -exec rm -f {} \;

# Generate source code documentation
Expand Down
51 changes: 0 additions & 51 deletions python/README.md

This file was deleted.

2 changes: 1 addition & 1 deletion python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def run(self):

setup(
name="farmhash64",
version="1.6.0.0",
version="1.7.0.0",
keywords=("farmhash64"),
description="farmhash64 Bindings for Python",
long_description=read("../README.md"),
Expand Down
2 changes: 1 addition & 1 deletion rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "farmhash64"
version = "1.6.0"
version = "1.7.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down

0 comments on commit d43e2f6

Please sign in to comment.