diff --git a/cpp/CMakeLists.txt b/cpp/CMakeLists.txt index a339448b..368f27e4 100644 --- a/cpp/CMakeLists.txt +++ b/cpp/CMakeLists.txt @@ -21,7 +21,7 @@ add_custom_target(check-formatting ) # Run formatter -add_custom_target(check-formatting +add_custom_target(format COMMAND ${FORMAT_COMMAND} `${FIND_COMMAND}` COMMENT "Running C++ formatter" ) diff --git a/cpp/src/Index.h b/cpp/src/Index.h index e5cca6f7..98c3e267 100644 --- a/cpp/src/Index.h +++ b/cpp/src/Index.h @@ -49,7 +49,7 @@ class Index { virtual void setEF(size_t ef) = 0; virtual int getEF() const = 0; - virtual SpaceType getSpace( ) const = 0; + virtual SpaceType getSpace() const = 0; virtual std::string getSpaceName() const = 0; virtual StorageDataType getStorageDataType() const = 0; diff --git a/cpp/src/Spaces/Euclidean.h b/cpp/src/Spaces/Euclidean.h index 252e413d..380467ff 100644 --- a/cpp/src/Spaces/Euclidean.h +++ b/cpp/src/Spaces/Euclidean.h @@ -32,10 +32,8 @@ namespace hnswlib { * should automatically do the loop unrolling for us here and vectorize as * appropriate. */ -template > -static dist_t L2Sqr(const data_t *__restrict pVect1, - const data_t *__restrict pVect2, const size_t qty) { +template > +static dist_t L2Sqr(const data_t *__restrict pVect1, const data_t *__restrict pVect2, const size_t qty) { dist_t res = 0; for (size_t i = 0; i < qty / K; i++) { @@ -51,22 +49,18 @@ static dist_t L2Sqr(const data_t *__restrict pVect1, return (res * scale * scale); } -template > -static dist_t L2SqrAtLeast(const data_t *__restrict pVect1, - const data_t *__restrict pVect2, const size_t qty) { +template > +static dist_t L2SqrAtLeast(const data_t *__restrict pVect1, const data_t *__restrict pVect2, const size_t qty) { size_t remainder = qty - K; return L2Sqr(pVect1, pVect2, K) + - L2Sqr(pVect1 + K, pVect2 + K, - remainder); + L2Sqr(pVect1 + K, pVect2 + K, remainder); } #if defined(USE_AVX512) // Favor using AVX512 if available. -static float L2SqrSIMD16Ext(const float *pVect1, const float *pVect2, - const size_t qty) { +static float L2SqrSIMD16Ext(const float *pVect1, const float *pVect2, const size_t qty) { float PORTABLE_ALIGN64 TmpRes[16]; size_t qty16 = qty >> 4; @@ -86,10 +80,8 @@ static float L2SqrSIMD16Ext(const float *pVect1, const float *pVect2, } _mm512_store_ps(TmpRes, sum); - float res = TmpRes[0] + TmpRes[1] + TmpRes[2] + TmpRes[3] + TmpRes[4] + - TmpRes[5] + TmpRes[6] + TmpRes[7] + TmpRes[8] + TmpRes[9] + - TmpRes[10] + TmpRes[11] + TmpRes[12] + TmpRes[13] + TmpRes[14] + - TmpRes[15]; + float res = TmpRes[0] + TmpRes[1] + TmpRes[2] + TmpRes[3] + TmpRes[4] + TmpRes[5] + TmpRes[6] + TmpRes[7] + + TmpRes[8] + TmpRes[9] + TmpRes[10] + TmpRes[11] + TmpRes[12] + TmpRes[13] + TmpRes[14] + TmpRes[15]; return (res); } @@ -97,8 +89,7 @@ static float L2SqrSIMD16Ext(const float *pVect1, const float *pVect2, #elif defined(USE_AVX) // Favor using AVX if available. -static float L2SqrSIMD16Ext(const float *pVect1, const float *pVect2, - const size_t qty) { +static float L2SqrSIMD16Ext(const float *pVect1, const float *pVect2, const size_t qty) { float PORTABLE_ALIGN32 TmpRes[8]; size_t qty16 = qty >> 4; @@ -124,14 +115,12 @@ static float L2SqrSIMD16Ext(const float *pVect1, const float *pVect2, } _mm256_store_ps(TmpRes, sum); - return TmpRes[0] + TmpRes[1] + TmpRes[2] + TmpRes[3] + TmpRes[4] + TmpRes[5] + - TmpRes[6] + TmpRes[7]; + return TmpRes[0] + TmpRes[1] + TmpRes[2] + TmpRes[3] + TmpRes[4] + TmpRes[5] + TmpRes[6] + TmpRes[7]; } #elif defined(USE_SSE) -static float L2SqrSIMD16Ext(const float *pVect1, const float *pVect2, - const size_t qty) { +static float L2SqrSIMD16Ext(const float *pVect1, const float *pVect2, const size_t qty) { float PORTABLE_ALIGN32 TmpRes[8]; size_t qty16 = qty >> 4; @@ -177,21 +166,18 @@ static float L2SqrSIMD16Ext(const float *pVect1, const float *pVect2, #endif #if defined(USE_SSE) || defined(USE_AVX) || defined(USE_AVX512) -static float L2SqrSIMD16ExtResiduals(const float *pVect1, const float *pVect2, - const size_t qty) { +static float L2SqrSIMD16ExtResiduals(const float *pVect1, const float *pVect2, const size_t qty) { size_t qty16 = qty >> 4 << 4; float res = L2SqrSIMD16Ext(pVect1, pVect2, qty16); size_t qty_left = qty - qty16; - float res_tail = - L2Sqr(pVect1 + qty16, pVect2 + qty16, qty_left); + float res_tail = L2Sqr(pVect1 + qty16, pVect2 + qty16, qty_left); return (res + res_tail); } #endif #ifdef USE_SSE -static float L2SqrSIMD4Ext(const float *pVect1, const float *pVect2, - const size_t qty) { +static float L2SqrSIMD4Ext(const float *pVect1, const float *pVect2, const size_t qty) { float PORTABLE_ALIGN32 TmpRes[8]; size_t qty4 = qty >> 2; @@ -212,8 +198,7 @@ static float L2SqrSIMD4Ext(const float *pVect1, const float *pVect2, return TmpRes[0] + TmpRes[1] + TmpRes[2] + TmpRes[3]; } -static float L2SqrSIMD4ExtResiduals(const float *pVect1, const float *pVect2, - const size_t qty) { +static float L2SqrSIMD4ExtResiduals(const float *pVect1, const float *pVect2, const size_t qty) { size_t qty4 = qty >> 2 << 2; float res = L2SqrSIMD4Ext(pVect1, pVect2, qty4); @@ -225,8 +210,7 @@ static float L2SqrSIMD4ExtResiduals(const float *pVect1, const float *pVect2, } #endif -template > +template > class EuclideanSpace : public Space { DISTFUNC fstdistfunc_; size_t data_size_; @@ -272,9 +256,7 @@ class EuclideanSpace : public Space { ~EuclideanSpace() {} }; -template <> -EuclideanSpace::EuclideanSpace(size_t dim) - : data_size_(dim * sizeof(float)), dim_(dim) { +template <> EuclideanSpace::EuclideanSpace(size_t dim) : data_size_(dim * sizeof(float)), dim_(dim) { fstdistfunc_ = L2Sqr; #if defined(USE_SSE) || defined(USE_AVX) || defined(USE_AVX512) if (dim % 16 == 0) diff --git a/cpp/src/Spaces/InnerProduct.h b/cpp/src/Spaces/InnerProduct.h index 5e671f6e..076c2ca7 100644 --- a/cpp/src/Spaces/InnerProduct.h +++ b/cpp/src/Spaces/InnerProduct.h @@ -32,10 +32,8 @@ namespace hnswlib { * compiler should automatically do the loop unrolling for us here and vectorize * as appropriate. */ -template > -static dist_t InnerProductWithoutScale(const data_t *pVect1, - const data_t *pVect2, size_t qty) { +template > +static dist_t InnerProductWithoutScale(const data_t *pVect1, const data_t *pVect2, size_t qty) { dist_t res = 0; qty = qty / K; @@ -51,28 +49,20 @@ static dist_t InnerProductWithoutScale(const data_t *pVect1, return res; } -template > -static dist_t InnerProduct(const data_t *pVect1, const data_t *pVect2, - size_t qty) { - dist_t res = InnerProductWithoutScale( - pVect1, pVect2, qty); +template > +static dist_t InnerProduct(const data_t *pVect1, const data_t *pVect2, size_t qty) { + dist_t res = InnerProductWithoutScale(pVect1, pVect2, qty); constexpr dist_t scale = (dist_t)scalefactor::num / (dist_t)scalefactor::den; res *= scale * scale; res = (static_cast(1.0f) - res); return res; } -template > -static dist_t InnerProductAtLeast(const data_t *__restrict pVect1, - const data_t *__restrict pVect2, - const size_t qty) { +template > +static dist_t InnerProductAtLeast(const data_t *__restrict pVect1, const data_t *__restrict pVect2, const size_t qty) { size_t remainder = qty - K; - dist_t res = InnerProductWithoutScale( - pVect1, pVect2, K) + - InnerProductWithoutScale( - pVect1 + K, pVect2 + K, remainder); + dist_t res = InnerProductWithoutScale(pVect1, pVect2, K) + + InnerProductWithoutScale(pVect1 + K, pVect2 + K, remainder); constexpr dist_t scale = (dist_t)scalefactor::num / (dist_t)scalefactor::den; res *= scale * scale; res = (static_cast(1.0f) - res); @@ -82,8 +72,7 @@ static dist_t InnerProductAtLeast(const data_t *__restrict pVect1, #if defined(USE_AVX) // Favor using AVX if available. -static float InnerProductSIMD4Ext(const float *pVect1, const float *pVect2, - const size_t qty) { +static float InnerProductSIMD4Ext(const float *pVect1, const float *pVect2, const size_t qty) { float PORTABLE_ALIGN32 TmpRes[8]; size_t qty16 = qty / 16; @@ -111,8 +100,7 @@ static float InnerProductSIMD4Ext(const float *pVect1, const float *pVect2, } __m128 v1, v2; - __m128 sum_prod = _mm_add_ps(_mm256_extractf128_ps(sum256, 0), - _mm256_extractf128_ps(sum256, 1)); + __m128 sum_prod = _mm_add_ps(_mm256_extractf128_ps(sum256, 0), _mm256_extractf128_ps(sum256, 1)); while (pVect1 < pEnd2) { v1 = _mm_loadu_ps(pVect1); @@ -130,8 +118,7 @@ static float InnerProductSIMD4Ext(const float *pVect1, const float *pVect2, #elif defined(USE_SSE) -static float InnerProductSIMD4Ext(const float *pVect1, const float *pVect2, - const size_t qty) { +static float InnerProductSIMD4Ext(const float *pVect1, const float *pVect2, const size_t qty) { float PORTABLE_ALIGN32 TmpRes[8]; size_t qty16 = qty / 16; @@ -187,8 +174,7 @@ static float InnerProductSIMD4Ext(const float *pVect1, const float *pVect2, #if defined(USE_AVX512) -static float InnerProductSIMD16Ext(const float *pVect1, const float *pVect2, - const size_t qty) { +static float InnerProductSIMD16Ext(const float *pVect1, const float *pVect2, const size_t qty) { float PORTABLE_ALIGN64 TmpRes[16]; size_t qty16 = qty / 16; @@ -208,18 +194,15 @@ static float InnerProductSIMD16Ext(const float *pVect1, const float *pVect2, } _mm512_store_ps(TmpRes, sum512); - float sum = TmpRes[0] + TmpRes[1] + TmpRes[2] + TmpRes[3] + TmpRes[4] + - TmpRes[5] + TmpRes[6] + TmpRes[7] + TmpRes[8] + TmpRes[9] + - TmpRes[10] + TmpRes[11] + TmpRes[12] + TmpRes[13] + TmpRes[14] + - TmpRes[15]; + float sum = TmpRes[0] + TmpRes[1] + TmpRes[2] + TmpRes[3] + TmpRes[4] + TmpRes[5] + TmpRes[6] + TmpRes[7] + + TmpRes[8] + TmpRes[9] + TmpRes[10] + TmpRes[11] + TmpRes[12] + TmpRes[13] + TmpRes[14] + TmpRes[15]; return 1.0f - sum; } #elif defined(USE_AVX) -static float InnerProductSIMD16Ext(const float *pVect1, const float *pVect2, - const size_t qty) { +static float InnerProductSIMD16Ext(const float *pVect1, const float *pVect2, const size_t qty) { float PORTABLE_ALIGN32 TmpRes[8]; size_t qty16 = qty / 16; @@ -245,16 +228,14 @@ static float InnerProductSIMD16Ext(const float *pVect1, const float *pVect2, } _mm256_store_ps(TmpRes, sum256); - float sum = TmpRes[0] + TmpRes[1] + TmpRes[2] + TmpRes[3] + TmpRes[4] + - TmpRes[5] + TmpRes[6] + TmpRes[7]; + float sum = TmpRes[0] + TmpRes[1] + TmpRes[2] + TmpRes[3] + TmpRes[4] + TmpRes[5] + TmpRes[6] + TmpRes[7]; return 1.0f - sum; } #elif defined(USE_SSE) -static float InnerProductSIMD16Ext(const float *pVect1, const float *pVect2, - const size_t qty) { +static float InnerProductSIMD16Ext(const float *pVect1, const float *pVect2, const size_t qty) { float PORTABLE_ALIGN32 TmpRes[8]; size_t qty16 = qty / 16; @@ -297,35 +278,28 @@ static float InnerProductSIMD16Ext(const float *pVect1, const float *pVect2, #endif #if defined(USE_SSE) || defined(USE_AVX) || defined(USE_AVX512) -static float InnerProductSIMD16ExtResiduals(const float *pVect1, - const float *pVect2, - const size_t qty) { +static float InnerProductSIMD16ExtResiduals(const float *pVect1, const float *pVect2, const size_t qty) { size_t qty16 = qty >> 4 << 4; float res = InnerProductSIMD16Ext(pVect1, pVect2, qty16); size_t qty_left = qty - qty16; - float res_tail = - InnerProduct(pVect1 + qty16, pVect2 + qty16, qty_left); + float res_tail = InnerProduct(pVect1 + qty16, pVect2 + qty16, qty_left); return res + res_tail - 1.0f; } -static float InnerProductSIMD4ExtResiduals(const float *pVect1, - const float *pVect2, - const size_t qty) { +static float InnerProductSIMD4ExtResiduals(const float *pVect1, const float *pVect2, const size_t qty) { size_t qty4 = qty >> 2 << 2; float res = InnerProductSIMD4Ext(pVect1, pVect2, qty4); size_t qty_left = qty - qty4; - float res_tail = - InnerProduct(pVect1 + qty4, pVect2 + qty4, qty_left); + float res_tail = InnerProduct(pVect1 + qty4, pVect2 + qty4, qty_left); return res + res_tail - 1.0f; } #endif -template > +template > class InnerProductSpace : public Space { DISTFUNC fstdistfunc_; size_t data_size_; @@ -371,8 +345,7 @@ class InnerProductSpace : public Space { }; template <> -InnerProductSpace::InnerProductSpace(size_t dim) - : data_size_(dim * sizeof(float)), dim_(dim) { +InnerProductSpace::InnerProductSpace(size_t dim) : data_size_(dim * sizeof(float)), dim_(dim) { fstdistfunc_ = InnerProduct; #if defined(USE_SSE) || defined(USE_AVX) || defined(USE_AVX512) if (dim % 16 == 0) diff --git a/cpp/src/Spaces/Space.h b/cpp/src/Spaces/Space.h index 672c21a5..05fd9d9f 100644 --- a/cpp/src/Spaces/Space.h +++ b/cpp/src/Spaces/Space.h @@ -26,8 +26,7 @@ namespace hnswlib { template -using DISTFUNC = - std::function; +using DISTFUNC = std::function; /** * An abstract class representing a type of space to search through, diff --git a/java/JavaInputStream.h b/java/JavaInputStream.h index ac4d8d8f..2bb2b822 100644 --- a/java/JavaInputStream.h +++ b/java/JavaInputStream.h @@ -30,8 +30,7 @@ class JavaInputStream : public InputStream { // hundreds of GB at once, which would allocate 2x that amount. static constexpr long long MAX_BUFFER_SIZE = 1024 * 1024 * 100; - JavaInputStream(JNIEnv *env, jobject inputStream) - : env(env), inputStream(inputStream) { + JavaInputStream(JNIEnv *env, jobject inputStream) : env(env), inputStream(inputStream) { jclass inputStreamClass = env->FindClass("java/io/InputStream"); if (!inputStreamClass) { @@ -39,8 +38,7 @@ class JavaInputStream : public InputStream { } if (!env->IsInstanceOf(inputStream, inputStreamClass)) { - throw std::runtime_error( - "Provided Java object is not a java.io.InputStream!"); + throw std::runtime_error("Provided Java object is not a java.io.InputStream!"); } }; @@ -49,8 +47,7 @@ class JavaInputStream : public InputStream { virtual long long getTotalLength() { return -1; } virtual long long read(char *buffer, long long bytesToRead) { - jmethodID readMethod = env->GetMethodID( - env->FindClass("java/io/InputStream"), "read", "([BII)I"); + jmethodID readMethod = env->GetMethodID(env->FindClass("java/io/InputStream"), "read", "([BII)I"); if (!readMethod) { throw std::runtime_error("Native code failed to find " @@ -62,14 +59,11 @@ class JavaInputStream : public InputStream { long long bufferSize = std::min(MAX_BUFFER_SIZE, bytesToRead); jbyteArray byteArray = env->NewByteArray(bufferSize); if (!byteArray) { - throw std::domain_error( - "Failed to instantiate Java byte array of size: " + - std::to_string(bufferSize)); + throw std::domain_error("Failed to instantiate Java byte array of size: " + std::to_string(bufferSize)); } if (peekValue.size()) { - long long bytesToCopy = - std::min(bytesToRead, (long long)peekValue.size()); + long long bytesToCopy = std::min(bytesToRead, (long long)peekValue.size()); std::memcpy(buffer, peekValue.data(), bytesToCopy); for (int i = 0; i < bytesToCopy; i++) peekValue.erase(peekValue.begin()); @@ -78,26 +72,21 @@ class JavaInputStream : public InputStream { } while (bytesRead < bytesToRead) { - int readResult = env->CallIntMethod( - inputStream, readMethod, byteArray, 0, - (int)(std::min(bufferSize, bytesToRead - bytesRead))); + int readResult = env->CallIntMethod(inputStream, readMethod, byteArray, 0, + (int)(std::min(bufferSize, bytesToRead - bytesRead))); if (env->ExceptionCheck()) { return 0; } if (readResult > 0) { if (bytesRead + readResult > bytesToRead) { - throw std::domain_error("java.io.InputStream#read(byte[]) returned " + - std::to_string(readResult) + ", but only " + - std::to_string(bytesToRead - bytesRead) + - " bytes were required."); + throw std::domain_error("java.io.InputStream#read(byte[]) returned " + std::to_string(readResult) + + ", but only " + std::to_string(bytesToRead - bytesRead) + " bytes were required."); } if (readResult > bufferSize) { - throw std::domain_error("java.io.InputStream#read(byte[]) returned " + - std::to_string(readResult) + - ", but buffer is only " + - std::to_string(bufferSize) + " bytes."); + throw std::domain_error("java.io.InputStream#read(byte[]) returned " + std::to_string(readResult) + + ", but buffer is only " + std::to_string(bufferSize) + " bytes."); } env->GetByteArrayRegion(byteArray, 0, readResult, (jbyte *)buffer); bytesRead += readResult; @@ -132,10 +121,8 @@ class JavaInputStream : public InputStream { peekValue.push_back(resultAsCharacters[3]); return result; } else { - throw std::runtime_error("Failed to peek " + - std::to_string(sizeof(result)) + - " bytes from JavaInputStream at index " + - std::to_string(lastPosition) + "."); + throw std::runtime_error("Failed to peek " + std::to_string(sizeof(result)) + + " bytes from JavaInputStream at index " + std::to_string(lastPosition) + "."); } } diff --git a/java/JavaOutputStream.h b/java/JavaOutputStream.h index 593da0dc..d91db1a3 100644 --- a/java/JavaOutputStream.h +++ b/java/JavaOutputStream.h @@ -25,23 +25,19 @@ class JavaOutputStream : public OutputStream { static constexpr unsigned long long MAX_BUFFER_SIZE = 1024 * 1024 * 100; public: - JavaOutputStream(JNIEnv *env, jobject outputStream) - : env(env), outputStream(outputStream) { + JavaOutputStream(JNIEnv *env, jobject outputStream) : env(env), outputStream(outputStream) { jclass outputStreamClass = env->FindClass("java/io/OutputStream"); if (!outputStreamClass) { - throw std::runtime_error( - "Native code failed to find OutputStream class!"); + throw std::runtime_error("Native code failed to find OutputStream class!"); } if (!env->IsInstanceOf(outputStream, outputStreamClass)) { - throw std::runtime_error( - "Provided Java object is not a java.io.OutputStream!"); + throw std::runtime_error("Provided Java object is not a java.io.OutputStream!"); } }; virtual void flush() { - jmethodID flushMethod = env->GetMethodID( - env->FindClass("java/io/OutputStream"), "flush", "()V"); + jmethodID flushMethod = env->GetMethodID(env->FindClass("java/io/OutputStream"), "flush", "()V"); env->CallVoidMethod(outputStream, flushMethod); if (env->ExceptionCheck()) { @@ -50,8 +46,7 @@ class JavaOutputStream : public OutputStream { } virtual bool write(const char *ptr, unsigned long long numBytes) { - jmethodID writeMethod = env->GetMethodID( - env->FindClass("java/io/OutputStream"), "write", "([B)V"); + jmethodID writeMethod = env->GetMethodID(env->FindClass("java/io/OutputStream"), "write", "([B)V"); if (!writeMethod) { throw std::runtime_error("Native code failed to find " @@ -63,9 +58,7 @@ class JavaOutputStream : public OutputStream { jbyteArray byteArray = env->NewByteArray(chunkSize); if (!byteArray) { - throw std::domain_error( - "Failed to instantiate Java byte array of size: " + - std::to_string(chunkSize)); + throw std::domain_error("Failed to instantiate Java byte array of size: " + std::to_string(chunkSize)); } env->SetByteArrayRegion(byteArray, 0, chunkSize, (const jbyte *)ptr); diff --git a/java/com_spotify_voyager_jni_Index.h b/java/com_spotify_voyager_jni_Index.h index c10ae6ed..d7edf057 100644 --- a/java/com_spotify_voyager_jni_Index.h +++ b/java/com_spotify_voyager_jni_Index.h @@ -12,240 +12,219 @@ extern "C" { * Method: nativeConstructor * Signature: (Lcom/spotify/voyager/jni/Index/SpaceType;IJJJJLcom/spotify/voyager/jni/Index/StorageDataType;)V */ -JNIEXPORT void JNICALL Java_com_spotify_voyager_jni_Index_nativeConstructor - (JNIEnv *, jobject, jobject, jint, jlong, jlong, jlong, jlong, jobject); +JNIEXPORT void JNICALL Java_com_spotify_voyager_jni_Index_nativeConstructor(JNIEnv *, jobject, jobject, jint, jlong, + jlong, jlong, jlong, jobject); /* * Class: com_spotify_voyager_jni_Index * Method: nativeLoadFromFileWithParameters - * Signature: (Ljava/lang/String;Lcom/spotify/voyager/jni/Index/SpaceType;ILcom/spotify/voyager/jni/Index/StorageDataType;)V + * Signature: + * (Ljava/lang/String;Lcom/spotify/voyager/jni/Index/SpaceType;ILcom/spotify/voyager/jni/Index/StorageDataType;)V */ -JNIEXPORT void JNICALL Java_com_spotify_voyager_jni_Index_nativeLoadFromFileWithParameters - (JNIEnv *, jobject, jstring, jobject, jint, jobject); +JNIEXPORT void JNICALL Java_com_spotify_voyager_jni_Index_nativeLoadFromFileWithParameters(JNIEnv *, jobject, jstring, + jobject, jint, jobject); /* * Class: com_spotify_voyager_jni_Index * Method: nativeLoadFromFile * Signature: (Ljava/lang/String;)V */ -JNIEXPORT void JNICALL Java_com_spotify_voyager_jni_Index_nativeLoadFromFile - (JNIEnv *, jobject, jstring); +JNIEXPORT void JNICALL Java_com_spotify_voyager_jni_Index_nativeLoadFromFile(JNIEnv *, jobject, jstring); /* * Class: com_spotify_voyager_jni_Index * Method: nativeLoadFromInputStreamWithParameters - * Signature: (Ljava/io/InputStream;Lcom/spotify/voyager/jni/Index/SpaceType;ILcom/spotify/voyager/jni/Index/StorageDataType;)V + * Signature: + * (Ljava/io/InputStream;Lcom/spotify/voyager/jni/Index/SpaceType;ILcom/spotify/voyager/jni/Index/StorageDataType;)V */ -JNIEXPORT void JNICALL Java_com_spotify_voyager_jni_Index_nativeLoadFromInputStreamWithParameters - (JNIEnv *, jobject, jobject, jobject, jint, jobject); +JNIEXPORT void JNICALL Java_com_spotify_voyager_jni_Index_nativeLoadFromInputStreamWithParameters(JNIEnv *, jobject, + jobject, jobject, + jint, jobject); /* * Class: com_spotify_voyager_jni_Index * Method: nativeLoadFromInputStream * Signature: (Ljava/io/InputStream;)V */ -JNIEXPORT void JNICALL Java_com_spotify_voyager_jni_Index_nativeLoadFromInputStream - (JNIEnv *, jobject, jobject); +JNIEXPORT void JNICALL Java_com_spotify_voyager_jni_Index_nativeLoadFromInputStream(JNIEnv *, jobject, jobject); /* * Class: com_spotify_voyager_jni_Index * Method: nativeDestructor * Signature: ()V */ -JNIEXPORT void JNICALL Java_com_spotify_voyager_jni_Index_nativeDestructor - (JNIEnv *, jobject); +JNIEXPORT void JNICALL Java_com_spotify_voyager_jni_Index_nativeDestructor(JNIEnv *, jobject); /* * Class: com_spotify_voyager_jni_Index * Method: setEf * Signature: (J)V */ -JNIEXPORT void JNICALL Java_com_spotify_voyager_jni_Index_setEf - (JNIEnv *, jobject, jlong); +JNIEXPORT void JNICALL Java_com_spotify_voyager_jni_Index_setEf(JNIEnv *, jobject, jlong); /* * Class: com_spotify_voyager_jni_Index * Method: getEf * Signature: ()I */ -JNIEXPORT jint JNICALL Java_com_spotify_voyager_jni_Index_getEf - (JNIEnv *, jobject); +JNIEXPORT jint JNICALL Java_com_spotify_voyager_jni_Index_getEf(JNIEnv *, jobject); /* * Class: com_spotify_voyager_jni_Index * Method: getSpace * Signature: ()Lcom/spotify/voyager/jni/Index/SpaceType; */ -JNIEXPORT jobject JNICALL Java_com_spotify_voyager_jni_Index_getSpace - (JNIEnv *, jobject); +JNIEXPORT jobject JNICALL Java_com_spotify_voyager_jni_Index_getSpace(JNIEnv *, jobject); /* * Class: com_spotify_voyager_jni_Index * Method: getNumDimensions * Signature: ()I */ -JNIEXPORT jint JNICALL Java_com_spotify_voyager_jni_Index_getNumDimensions - (JNIEnv *, jobject); +JNIEXPORT jint JNICALL Java_com_spotify_voyager_jni_Index_getNumDimensions(JNIEnv *, jobject); /* * Class: com_spotify_voyager_jni_Index * Method: setNumThreads * Signature: (I)V */ -JNIEXPORT void JNICALL Java_com_spotify_voyager_jni_Index_setNumThreads - (JNIEnv *, jobject, jint); +JNIEXPORT void JNICALL Java_com_spotify_voyager_jni_Index_setNumThreads(JNIEnv *, jobject, jint); /* * Class: com_spotify_voyager_jni_Index * Method: getNumThreads * Signature: ()I */ -JNIEXPORT jint JNICALL Java_com_spotify_voyager_jni_Index_getNumThreads - (JNIEnv *, jobject); +JNIEXPORT jint JNICALL Java_com_spotify_voyager_jni_Index_getNumThreads(JNIEnv *, jobject); /* * Class: com_spotify_voyager_jni_Index * Method: saveIndex * Signature: (Ljava/lang/String;)V */ -JNIEXPORT void JNICALL Java_com_spotify_voyager_jni_Index_saveIndex__Ljava_lang_String_2 - (JNIEnv *, jobject, jstring); +JNIEXPORT void JNICALL Java_com_spotify_voyager_jni_Index_saveIndex__Ljava_lang_String_2(JNIEnv *, jobject, jstring); /* * Class: com_spotify_voyager_jni_Index * Method: saveIndex * Signature: (Ljava/io/OutputStream;)V */ -JNIEXPORT void JNICALL Java_com_spotify_voyager_jni_Index_saveIndex__Ljava_io_OutputStream_2 - (JNIEnv *, jobject, jobject); +JNIEXPORT void JNICALL Java_com_spotify_voyager_jni_Index_saveIndex__Ljava_io_OutputStream_2(JNIEnv *, jobject, + jobject); /* * Class: com_spotify_voyager_jni_Index * Method: addItem * Signature: ([F)V */ -JNIEXPORT void JNICALL Java_com_spotify_voyager_jni_Index_addItem___3F - (JNIEnv *, jobject, jfloatArray); +JNIEXPORT void JNICALL Java_com_spotify_voyager_jni_Index_addItem___3F(JNIEnv *, jobject, jfloatArray); /* * Class: com_spotify_voyager_jni_Index * Method: addItem * Signature: ([FJ)V */ -JNIEXPORT void JNICALL Java_com_spotify_voyager_jni_Index_addItem___3FJ - (JNIEnv *, jobject, jfloatArray, jlong); +JNIEXPORT void JNICALL Java_com_spotify_voyager_jni_Index_addItem___3FJ(JNIEnv *, jobject, jfloatArray, jlong); /* * Class: com_spotify_voyager_jni_Index * Method: addItems * Signature: ([[FI)V */ -JNIEXPORT void JNICALL Java_com_spotify_voyager_jni_Index_addItems___3_3FI - (JNIEnv *, jobject, jobjectArray, jint); +JNIEXPORT void JNICALL Java_com_spotify_voyager_jni_Index_addItems___3_3FI(JNIEnv *, jobject, jobjectArray, jint); /* * Class: com_spotify_voyager_jni_Index * Method: addItems * Signature: ([[F[JI)V */ -JNIEXPORT void JNICALL Java_com_spotify_voyager_jni_Index_addItems___3_3F_3JI - (JNIEnv *, jobject, jobjectArray, jlongArray, jint); +JNIEXPORT void JNICALL Java_com_spotify_voyager_jni_Index_addItems___3_3F_3JI(JNIEnv *, jobject, jobjectArray, + jlongArray, jint); /* * Class: com_spotify_voyager_jni_Index * Method: getVector * Signature: (J)[F */ -JNIEXPORT jfloatArray JNICALL Java_com_spotify_voyager_jni_Index_getVector - (JNIEnv *, jobject, jlong); +JNIEXPORT jfloatArray JNICALL Java_com_spotify_voyager_jni_Index_getVector(JNIEnv *, jobject, jlong); /* * Class: com_spotify_voyager_jni_Index * Method: getVectors * Signature: ([J)[[F */ -JNIEXPORT jobjectArray JNICALL Java_com_spotify_voyager_jni_Index_getVectors - (JNIEnv *, jobject, jlongArray); +JNIEXPORT jobjectArray JNICALL Java_com_spotify_voyager_jni_Index_getVectors(JNIEnv *, jobject, jlongArray); /* * Class: com_spotify_voyager_jni_Index * Method: getIDs * Signature: ()[J */ -JNIEXPORT jlongArray JNICALL Java_com_spotify_voyager_jni_Index_getIDs - (JNIEnv *, jobject); +JNIEXPORT jlongArray JNICALL Java_com_spotify_voyager_jni_Index_getIDs(JNIEnv *, jobject); /* * Class: com_spotify_voyager_jni_Index * Method: query * Signature: ([FIJ)Lcom/spotify/voyager/jni/Index/QueryResults; */ -JNIEXPORT jobject JNICALL Java_com_spotify_voyager_jni_Index_query___3FIJ - (JNIEnv *, jobject, jfloatArray, jint, jlong); +JNIEXPORT jobject JNICALL Java_com_spotify_voyager_jni_Index_query___3FIJ(JNIEnv *, jobject, jfloatArray, jint, jlong); /* * Class: com_spotify_voyager_jni_Index * Method: query * Signature: ([[FIIJ)[Lcom/spotify/voyager/jni/Index/QueryResults; */ -JNIEXPORT jobjectArray JNICALL Java_com_spotify_voyager_jni_Index_query___3_3FIIJ - (JNIEnv *, jobject, jobjectArray, jint, jint, jlong); +JNIEXPORT jobjectArray JNICALL Java_com_spotify_voyager_jni_Index_query___3_3FIIJ(JNIEnv *, jobject, jobjectArray, jint, + jint, jlong); /* * Class: com_spotify_voyager_jni_Index * Method: markDeleted * Signature: (J)V */ -JNIEXPORT void JNICALL Java_com_spotify_voyager_jni_Index_markDeleted - (JNIEnv *, jobject, jlong); +JNIEXPORT void JNICALL Java_com_spotify_voyager_jni_Index_markDeleted(JNIEnv *, jobject, jlong); /* * Class: com_spotify_voyager_jni_Index * Method: unmarkDeleted * Signature: (J)V */ -JNIEXPORT void JNICALL Java_com_spotify_voyager_jni_Index_unmarkDeleted - (JNIEnv *, jobject, jlong); +JNIEXPORT void JNICALL Java_com_spotify_voyager_jni_Index_unmarkDeleted(JNIEnv *, jobject, jlong); /* * Class: com_spotify_voyager_jni_Index * Method: resizeIndex * Signature: (J)V */ -JNIEXPORT void JNICALL Java_com_spotify_voyager_jni_Index_resizeIndex - (JNIEnv *, jobject, jlong); +JNIEXPORT void JNICALL Java_com_spotify_voyager_jni_Index_resizeIndex(JNIEnv *, jobject, jlong); /* * Class: com_spotify_voyager_jni_Index * Method: getMaxElements * Signature: ()J */ -JNIEXPORT jlong JNICALL Java_com_spotify_voyager_jni_Index_getMaxElements - (JNIEnv *, jobject); +JNIEXPORT jlong JNICALL Java_com_spotify_voyager_jni_Index_getMaxElements(JNIEnv *, jobject); /* * Class: com_spotify_voyager_jni_Index * Method: getNumElements * Signature: ()J */ -JNIEXPORT jlong JNICALL Java_com_spotify_voyager_jni_Index_getNumElements - (JNIEnv *, jobject); +JNIEXPORT jlong JNICALL Java_com_spotify_voyager_jni_Index_getNumElements(JNIEnv *, jobject); /* * Class: com_spotify_voyager_jni_Index * Method: getEfConstruction * Signature: ()J */ -JNIEXPORT jlong JNICALL Java_com_spotify_voyager_jni_Index_getEfConstruction - (JNIEnv *, jobject); +JNIEXPORT jlong JNICALL Java_com_spotify_voyager_jni_Index_getEfConstruction(JNIEnv *, jobject); /* * Class: com_spotify_voyager_jni_Index * Method: getM * Signature: ()J */ -JNIEXPORT jlong JNICALL Java_com_spotify_voyager_jni_Index_getM - (JNIEnv *, jobject); +JNIEXPORT jlong JNICALL Java_com_spotify_voyager_jni_Index_getM(JNIEnv *, jobject); #ifdef __cplusplus } diff --git a/java/thread_pool.h b/java/thread_pool.h index 6d137eb7..da113477 100644 --- a/java/thread_pool.h +++ b/java/thread_pool.h @@ -22,8 +22,7 @@ namespace similarity { // See sample usage below -template -bool GetNextQueueObj(std::mutex &mtx, std::queue &queue, T &obj) { +template bool GetNextQueueObj(std::mutex &mtx, std::queue &queue, T &obj) { std::unique_lock lock(mtx); if (queue.empty()) { return false; @@ -60,9 +59,7 @@ bool GetNextQueueObj(std::mutex &mtx, std::queue &queue, T &obj) { * only handles a subset of functionality (no reductions etc) * Process ids from start (inclusive) to end (EXCLUSIVE) */ -template -inline void ParallelFor(size_t start, size_t end, size_t numThreads, - Function fn) { +template inline void ParallelFor(size_t start, size_t end, size_t numThreads, Function fn) { if (numThreads <= 0) { numThreads = std::thread::hardware_concurrency(); } diff --git a/python/src/PythonInputStream.h b/python/src/PythonInputStream.h index 6a1d8dd8..99391a2f 100644 --- a/python/src/PythonInputStream.h +++ b/python/src/PythonInputStream.h @@ -23,8 +23,8 @@ namespace py = pybind11; #include bool isReadableFileLike(py::object fileLike) { - return py::hasattr(fileLike, "read") && py::hasattr(fileLike, "seek") && - py::hasattr(fileLike, "tell") && py::hasattr(fileLike, "seekable"); + return py::hasattr(fileLike, "read") && py::hasattr(fileLike, "seek") && py::hasattr(fileLike, "tell") && + py::hasattr(fileLike, "seekable"); } /** @@ -74,9 +74,8 @@ class PythonInputStream : public InputStream, PythonFileLike { long long read(char *buffer, long long bytesToRead) { py::gil_scoped_acquire acquire; if (buffer == nullptr) { - throw py::buffer_error( - "C++ code attempted to read from a Python file-like object into a " - "null destination buffer."); + throw py::buffer_error("C++ code attempted to read from a Python file-like object into a " + "null destination buffer."); } if (bytesToRead < 0) { @@ -87,8 +86,7 @@ class PythonInputStream : public InputStream, PythonFileLike { long long bytesRead = 0; if (peekValue.size()) { - long long bytesToCopy = - std::min(bytesToRead, (long long)peekValue.size()); + long long bytesToCopy = std::min(bytesToRead, (long long)peekValue.size()); std::memcpy(buffer, peekValue.data(), bytesToCopy); for (int i = 0; i < bytesToCopy; i++) peekValue.erase(peekValue.begin()); @@ -97,19 +95,15 @@ class PythonInputStream : public InputStream, PythonFileLike { } while (bytesRead < bytesToRead) { - auto readResult = fileLike.attr("read")( - std::min(MAX_BUFFER_SIZE, bytesToRead - bytesRead)); + auto readResult = fileLike.attr("read")(std::min(MAX_BUFFER_SIZE, bytesToRead - bytesRead)); if (!py::isinstance(readResult)) { std::string message = "Python file-like object was expected to return " "bytes from its read(...) method, but " "returned " + - py::str(readResult.get_type().attr("__name__")) - .cast() + - "."; + py::str(readResult.get_type().attr("__name__")).cast() + "."; - if (py::hasattr(fileLike, "mode") && - py::str(fileLike.attr("mode")).cast() == "r") { + if (py::hasattr(fileLike, "mode") && py::str(fileLike.attr("mode")).cast() == "r") { message += " (Try opening the stream in \"rb\" mode instead of " "\"r\" mode if possible.)"; } @@ -122,10 +116,8 @@ class PythonInputStream : public InputStream, PythonFileLike { char *pythonBuffer = nullptr; py::ssize_t pythonLength = 0; - if (PYBIND11_BYTES_AS_STRING_AND_SIZE(bytesObject.ptr(), &pythonBuffer, - &pythonLength)) { - throw py::buffer_error( - "Internal error: failed to read bytes from bytes object!"); + if (PYBIND11_BYTES_AS_STRING_AND_SIZE(bytesObject.ptr(), &pythonBuffer, &pythonLength)) { + throw py::buffer_error("Internal error: failed to read bytes from bytes object!"); } if (!buffer && pythonLength > 0) { @@ -134,10 +126,8 @@ class PythonInputStream : public InputStream, PythonFileLike { } if (bytesRead + pythonLength > bytesToRead) { - throw py::buffer_error( - "Python returned " + std::to_string(pythonLength) + - " bytes, but only " + std::to_string(bytesToRead - bytesRead) + - " bytes were requested."); + throw py::buffer_error("Python returned " + std::to_string(pythonLength) + " bytes, but only " + + std::to_string(bytesToRead - bytesRead) + " bytes were requested."); } if (buffer && pythonLength > 0) { @@ -190,10 +180,8 @@ class PythonInputStream : public InputStream, PythonFileLike { peekValue.push_back(resultAsCharacters[3]); return result; } else { - throw std::runtime_error("Failed to peek " + - std::to_string(sizeof(result)) + - " bytes from file-like object at index " + - std::to_string(lastPosition) + "."); + throw std::runtime_error("Failed to peek " + std::to_string(sizeof(result)) + + " bytes from file-like object at index " + std::to_string(lastPosition) + "."); } } diff --git a/python/src/PythonOutputStream.h b/python/src/PythonOutputStream.h index fc30b5c5..379c0667 100644 --- a/python/src/PythonOutputStream.h +++ b/python/src/PythonOutputStream.h @@ -23,8 +23,8 @@ namespace py = pybind11; #include bool isWriteableFileLike(py::object fileLike) { - return py::hasattr(fileLike, "write") && py::hasattr(fileLike, "seek") && - py::hasattr(fileLike, "tell") && py::hasattr(fileLike, "seekable"); + return py::hasattr(fileLike, "write") && py::hasattr(fileLike, "seek") && py::hasattr(fileLike, "tell") && + py::hasattr(fileLike, "seekable"); } /** @@ -55,9 +55,7 @@ class PythonOutputStream : public OutputStream, public PythonFileLike { for (unsigned long long i = 0; i < numBytes; i += MAX_BUFFER_SIZE) { unsigned long long chunkSize = std::min(numBytes - i, MAX_BUFFER_SIZE); - int bytesWritten = - fileLike.attr("write")(py::bytes((const char *)ptr, chunkSize)) - .cast(); + int bytesWritten = fileLike.attr("write")(py::bytes((const char *)ptr, chunkSize)).cast(); if (bytesWritten < 0) return false;