usearch 2.8.16 β 2.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/ISSUE_TEMPLATE/bug_report.yml +1 -1
- package/.github/workflows/prerelease.yml +33 -7
- package/.github/workflows/release.yml +25 -26
- package/.gitmodules +4 -1
- package/CITATION.cff +1 -1
- package/CMakeLists.txt +6 -8
- package/CONTRIBUTING.md +20 -1
- package/Cargo.toml +1 -1
- package/README.md +7 -6
- package/VERSION +1 -1
- package/conanfile.py +1 -1
- package/cpp/test.cpp +9 -2
- package/include/usearch/index.hpp +11 -11
- package/include/usearch/index_plugins.hpp +2 -1
- package/javascript/usearch.js +1 -1
- package/package.json +1 -1
- package/pyproject.toml +2 -2
- package/setup.py +10 -4
- package/simsimd/.releaserc +3 -2
- package/simsimd/Cargo.lock +708 -0
- package/simsimd/Cargo.toml +36 -0
- package/simsimd/README.md +37 -1
- package/simsimd/build.rs +20 -0
- package/simsimd/include/simsimd/simsimd.h +4 -4
- package/simsimd/include/simsimd/spatial.h +1 -1
- package/simsimd/include/simsimd/types.h +5 -1
- package/simsimd/package.json +1 -1
- package/simsimd/rust/benches/cosine.rs +27 -0
- package/simsimd/rust/benches/native.rs +27 -0
- package/simsimd/rust/benches/sqeuclidean.rs +27 -0
- package/simsimd/rust/lib.c +68 -0
- package/simsimd/rust/lib.rs +232 -0
- package/sqlite/README.md +178 -0
- package/sqlite/lib.cpp +325 -0
- package/stringzilla/CONTRIBUTING.md +429 -0
- package/stringzilla/Cargo.lock +25 -0
- package/stringzilla/Cargo.toml +26 -0
- package/stringzilla/LICENSE +201 -0
- package/stringzilla/README.md +1281 -0
- package/stringzilla/VERSION +1 -0
- package/stringzilla/assets/cover-strinzilla.jpeg +0 -0
- package/stringzilla/assets/meme-stringzilla-v2.jpeg +0 -0
- package/stringzilla/assets/meme-stringzilla-v3.jpeg +0 -0
- package/stringzilla/binding.gyp +16 -0
- package/stringzilla/build.rs +14 -0
- package/stringzilla/c/lib.c +304 -0
- package/stringzilla/cli/README.md +48 -0
- package/stringzilla/cli/split.py +72 -0
- package/stringzilla/cli/wc.py +37 -0
- package/stringzilla/include/stringzilla/experimental.h +724 -0
- package/stringzilla/include/stringzilla/module.modulemap +9 -0
- package/stringzilla/include/stringzilla/stringzilla.h +5240 -0
- package/stringzilla/include/stringzilla/stringzilla.hpp +3830 -0
- package/stringzilla/javascript/lib.c +125 -0
- package/stringzilla/javascript/stringzilla.js +22 -0
- package/stringzilla/package-ci.json +10 -0
- package/stringzilla/package.json +32 -0
- package/stringzilla/rust/lib.rs +362 -0
- package/stringzilla/swift/StringProtocol+StringZilla.swift +280 -0
- package/stringzilla/swift/Test.swift +60 -0
- package/wasmer.toml +1 -1
- package/simsimd/bench.js +0 -51
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
//
|
|
2
|
+
// StringProtocol+StringZilla.swift
|
|
3
|
+
//
|
|
4
|
+
// Created by Ash Vardanian on 18/1/24.
|
|
5
|
+
// Extension of StringProtocol to interface with StringZilla functionalities.
|
|
6
|
+
//
|
|
7
|
+
// Docs:
|
|
8
|
+
// - Accessing immutable UTF8-range:
|
|
9
|
+
// https://developer.apple.com/documentation/swift/string/utf8view
|
|
10
|
+
//
|
|
11
|
+
// More reading materials:
|
|
12
|
+
// - Stringβs ABI and UTF-8. Nov 2018
|
|
13
|
+
// https://forums.swift.org/t/string-s-abi-and-utf-8/17676
|
|
14
|
+
// - Stable pointer into a C string without copying it? Aug 2021
|
|
15
|
+
// https://forums.swift.org/t/stable-pointer-into-a-c-string-without-copying-it/51244/1
|
|
16
|
+
|
|
17
|
+
import StringZillaC
|
|
18
|
+
|
|
19
|
+
// We need to link the standard libraries.
|
|
20
|
+
#if os(Linux)
|
|
21
|
+
import Glibc
|
|
22
|
+
#else
|
|
23
|
+
import Darwin.C
|
|
24
|
+
#endif
|
|
25
|
+
|
|
26
|
+
/// Protocol defining a single-byte data type.
|
|
27
|
+
fileprivate protocol SingleByte {}
|
|
28
|
+
|
|
29
|
+
extension UInt8: SingleByte {}
|
|
30
|
+
extension Int8: SingleByte {} // This would match `CChar` as well.
|
|
31
|
+
|
|
32
|
+
@usableFromInline
|
|
33
|
+
enum StringZillaError: Error {
|
|
34
|
+
case contiguousStorageUnavailable
|
|
35
|
+
case memoryAllocationFailed
|
|
36
|
+
|
|
37
|
+
var localizedDescription: String {
|
|
38
|
+
switch self {
|
|
39
|
+
case .contiguousStorageUnavailable:
|
|
40
|
+
return "Contiguous storage for the sequence is unavailable."
|
|
41
|
+
case .memoryAllocationFailed:
|
|
42
|
+
return "Memory allocation failed."
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/// Protocol defining the interface for StringZilla-compatible byte-spans.
|
|
48
|
+
///
|
|
49
|
+
/// # Discussion:
|
|
50
|
+
/// The Swift documentation is extremely vague about the actual memory layout of a String
|
|
51
|
+
/// and the cost of obtaining the underlying UTF8 representation or any other raw pointers.
|
|
52
|
+
/// https://developer.apple.com/documentation/swift/stringprotocol/withcstring(_:)
|
|
53
|
+
/// https://developer.apple.com/documentation/swift/stringprotocol/withcstring(encodedas:_:)
|
|
54
|
+
/// https://developer.apple.com/documentation/swift/stringprotocol/data(using:allowlossyconversion:)
|
|
55
|
+
public protocol StringZillaViewable: Collection {
|
|
56
|
+
/// A type that represents a position in the collection.
|
|
57
|
+
///
|
|
58
|
+
/// Executes a closure with a pointer to the string's UTF8 C representation and its length.
|
|
59
|
+
///
|
|
60
|
+
/// - Parameters:
|
|
61
|
+
/// - body: A closure that takes a pointer to a C string and its length.
|
|
62
|
+
/// - Throws: Can throw an error.
|
|
63
|
+
/// - Returns: Returns a value of type R, which is the result of the closure.
|
|
64
|
+
func withStringZillaScope<R>(_ body: (sz_cptr_t, sz_size_t) throws -> R) rethrows -> R
|
|
65
|
+
|
|
66
|
+
/// Calculates the offset index for a given byte pointer relative to a start pointer.
|
|
67
|
+
///
|
|
68
|
+
/// - Parameters:
|
|
69
|
+
/// - bytePointer: A pointer to the byte for which the offset is calculated.
|
|
70
|
+
/// - startPointer: The starting pointer for the calculation, previously obtained from `szScope`.
|
|
71
|
+
/// - Returns: The calculated index offset.
|
|
72
|
+
func stringZillaByteOffset(forByte bytePointer: sz_cptr_t, after startPointer: sz_cptr_t) -> Index
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
extension String: StringZillaViewable {
|
|
76
|
+
public typealias Index = String.Index
|
|
77
|
+
|
|
78
|
+
@_transparent
|
|
79
|
+
public func withStringZillaScope<R>(_ body: (sz_cptr_t, sz_size_t) throws -> R) rethrows -> R {
|
|
80
|
+
let cLength = sz_size_t(utf8.count)
|
|
81
|
+
return try self.withCString { cString in
|
|
82
|
+
try body(cString, cLength)
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
@_transparent
|
|
87
|
+
public func stringZillaByteOffset(forByte bytePointer: sz_cptr_t, after startPointer: sz_cptr_t) -> Index {
|
|
88
|
+
self.utf8.index(self.utf8.startIndex, offsetBy: bytePointer - startPointer)
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
extension Substring.UTF8View: StringZillaViewable {
|
|
93
|
+
public typealias Index = Substring.UTF8View.Index
|
|
94
|
+
|
|
95
|
+
/// Executes a closure with a pointer to the UTF8View's contiguous storage of single-byte elements (UTF-8 code units).
|
|
96
|
+
/// - Parameters:
|
|
97
|
+
/// - body: A closure that takes a pointer to the contiguous storage and its size.
|
|
98
|
+
/// - Throws: An error if the storage is not contiguous.
|
|
99
|
+
@_transparent
|
|
100
|
+
public func withStringZillaScope<R>(_ body: (sz_cptr_t, sz_size_t) throws -> R) rethrows -> R {
|
|
101
|
+
return try withContiguousStorageIfAvailable { bufferPointer -> R in
|
|
102
|
+
let cLength = sz_size_t(bufferPointer.count)
|
|
103
|
+
let cString = UnsafeRawPointer(bufferPointer.baseAddress!).assumingMemoryBound(to: CChar.self)
|
|
104
|
+
return try body(cString, cLength)
|
|
105
|
+
} ?? {
|
|
106
|
+
throw StringZillaError.contiguousStorageUnavailable
|
|
107
|
+
}()
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/// Calculates the offset index for a given byte pointer relative to a start pointer.
|
|
111
|
+
/// - Parameters:
|
|
112
|
+
/// - bytePointer: A pointer to the byte for which the offset is calculated.
|
|
113
|
+
/// - startPointer: The starting pointer for the calculation, previously obtained from `szScope`.
|
|
114
|
+
/// - Returns: The calculated index offset.
|
|
115
|
+
@_transparent
|
|
116
|
+
public func stringZillaByteOffset(forByte bytePointer: sz_cptr_t, after startPointer: sz_cptr_t) -> Index {
|
|
117
|
+
return self.index(self.startIndex, offsetBy: bytePointer - startPointer)
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
extension String.UTF8View: StringZillaViewable {
|
|
122
|
+
public typealias Index = String.UTF8View.Index
|
|
123
|
+
|
|
124
|
+
/// Executes a closure with a pointer to the UTF8View's contiguous storage of single-byte elements (UTF-8 code units).
|
|
125
|
+
/// - Parameters:
|
|
126
|
+
/// - body: A closure that takes a pointer to the contiguous storage and its size.
|
|
127
|
+
/// - Throws: An error if the storage is not contiguous.
|
|
128
|
+
public func withStringZillaScope<R>(_ body: (sz_cptr_t, sz_size_t) throws -> R) rethrows -> R {
|
|
129
|
+
return try withContiguousStorageIfAvailable { bufferPointer -> R in
|
|
130
|
+
let cLength = sz_size_t(bufferPointer.count)
|
|
131
|
+
let cString = UnsafeRawPointer(bufferPointer.baseAddress!).assumingMemoryBound(to: CChar.self)
|
|
132
|
+
return try body(cString, cLength)
|
|
133
|
+
} ?? {
|
|
134
|
+
throw StringZillaError.contiguousStorageUnavailable
|
|
135
|
+
}()
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/// Calculates the offset index for a given byte pointer relative to a start pointer.
|
|
139
|
+
/// - Parameters:
|
|
140
|
+
/// - bytePointer: A pointer to the byte for which the offset is calculated.
|
|
141
|
+
/// - startPointer: The starting pointer for the calculation, previously obtained from `szScope`.
|
|
142
|
+
/// - Returns: The calculated index offset.
|
|
143
|
+
public func stringZillaByteOffset(forByte bytePointer: sz_cptr_t, after startPointer: sz_cptr_t) -> Index {
|
|
144
|
+
return self.index(self.startIndex, offsetBy: bytePointer - startPointer)
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
public extension StringZillaViewable {
|
|
149
|
+
|
|
150
|
+
/// Finds the first occurrence of the specified substring within the receiver.
|
|
151
|
+
/// - Parameter needle: The substring to search for.
|
|
152
|
+
/// - Returns: The index of the found occurrence, or `nil` if not found.
|
|
153
|
+
@_specialize(where Self == String, S == String)
|
|
154
|
+
@_specialize(where Self == String.UTF8View, S == String.UTF8View)
|
|
155
|
+
func findFirst<S: StringZillaViewable>(substring needle: S) -> Index? {
|
|
156
|
+
var result: Index?
|
|
157
|
+
withStringZillaScope { hPointer, hLength in
|
|
158
|
+
needle.withStringZillaScope { nPointer, nLength in
|
|
159
|
+
if let matchPointer = sz_find(hPointer, hLength, nPointer, nLength) {
|
|
160
|
+
result = self.stringZillaByteOffset(forByte: matchPointer, after: hPointer)
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return result
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/// Finds the last occurrence of the specified substring within the receiver.
|
|
168
|
+
/// - Parameter needle: The substring to search for.
|
|
169
|
+
/// - Returns: The index of the found occurrence, or `nil` if not found.
|
|
170
|
+
@_specialize(where Self == String, S == String)
|
|
171
|
+
@_specialize(where Self == String.UTF8View, S == String.UTF8View)
|
|
172
|
+
func findLast<S: StringZillaViewable>(substring needle: S) -> Index? {
|
|
173
|
+
var result: Index?
|
|
174
|
+
withStringZillaScope { hPointer, hLength in
|
|
175
|
+
needle.withStringZillaScope { nPointer, nLength in
|
|
176
|
+
if let matchPointer = sz_rfind(hPointer, hLength, nPointer, nLength) {
|
|
177
|
+
result = self.stringZillaByteOffset(forByte: matchPointer, after: hPointer)
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
return result
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/// Finds the first occurrence of the specified character-set members within the receiver.
|
|
185
|
+
/// - Parameter characters: A string-like collection of characters to match.
|
|
186
|
+
/// - Returns: The index of the found occurrence, or `nil` if not found.
|
|
187
|
+
@_specialize(where Self == String, S == String)
|
|
188
|
+
@_specialize(where Self == String.UTF8View, S == String.UTF8View)
|
|
189
|
+
func findFirst<S: StringZillaViewable>(characterFrom characters: S) -> Index? {
|
|
190
|
+
var result: Index?
|
|
191
|
+
withStringZillaScope { hPointer, hLength in
|
|
192
|
+
characters.withStringZillaScope { nPointer, nLength in
|
|
193
|
+
if let matchPointer = sz_find_char_from(hPointer, hLength, nPointer, nLength) {
|
|
194
|
+
result = self.stringZillaByteOffset(forByte: matchPointer, after: hPointer)
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
return result
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/// Finds the last occurrence of the specified character-set members within the receiver.
|
|
202
|
+
/// - Parameter characters: A string-like collection of characters to match.
|
|
203
|
+
/// - Returns: The index of the found occurrence, or `nil` if not found.
|
|
204
|
+
@_specialize(where Self == String, S == String)
|
|
205
|
+
@_specialize(where Self == String.UTF8View, S == String.UTF8View)
|
|
206
|
+
func findLast<S: StringZillaViewable>(characterFrom characters: S) -> Index? {
|
|
207
|
+
var result: Index?
|
|
208
|
+
withStringZillaScope { hPointer, hLength in
|
|
209
|
+
characters.withStringZillaScope { nPointer, nLength in
|
|
210
|
+
if let matchPointer = sz_rfind_char_from(hPointer, hLength, nPointer, nLength) {
|
|
211
|
+
result = self.stringZillaByteOffset(forByte: matchPointer, after: hPointer)
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
return result
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/// Finds the first occurrence of a character outside of the the given character-set within the receiver.
|
|
219
|
+
/// - Parameter characters: A string-like collection of characters to exclude.
|
|
220
|
+
/// - Returns: The index of the found occurrence, or `nil` if not found.
|
|
221
|
+
@_specialize(where Self == String, S == String)
|
|
222
|
+
@_specialize(where Self == String.UTF8View, S == String.UTF8View)
|
|
223
|
+
func findFirst<S: StringZillaViewable>(characterNotFrom characters: S) -> Index? {
|
|
224
|
+
var result: Index?
|
|
225
|
+
withStringZillaScope { hPointer, hLength in
|
|
226
|
+
characters.withStringZillaScope { nPointer, nLength in
|
|
227
|
+
if let matchPointer = sz_find_char_not_from(hPointer, hLength, nPointer, nLength) {
|
|
228
|
+
result = self.stringZillaByteOffset(forByte: matchPointer, after: hPointer)
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
return result
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/// Finds the last occurrence of a character outside of the the given character-set within the receiver.
|
|
236
|
+
/// - Parameter characters: A string-like collection of characters to exclude.
|
|
237
|
+
/// - Returns: The index of the found occurrence, or `nil` if not found.
|
|
238
|
+
@_specialize(where Self == String, S == String)
|
|
239
|
+
@_specialize(where Self == String.UTF8View, S == String.UTF8View)
|
|
240
|
+
func findLast<S: StringZillaViewable>(characterNotFrom characters: S) -> Index? {
|
|
241
|
+
var result: Index?
|
|
242
|
+
withStringZillaScope { hPointer, hLength in
|
|
243
|
+
characters.withStringZillaScope { nPointer, nLength in
|
|
244
|
+
if let matchPointer = sz_rfind_char_not_from(hPointer, hLength, nPointer, nLength) {
|
|
245
|
+
result = self.stringZillaByteOffset(forByte: matchPointer, after: hPointer)
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
return result
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/// Computes the Levenshtein edit distance between this and another string.
|
|
253
|
+
/// - Parameter other: A string-like collection of characters to exclude.
|
|
254
|
+
/// - Returns: The edit distance, as an unsigned integer.
|
|
255
|
+
/// - Throws: If a memory allocation error has happened.
|
|
256
|
+
@_specialize(where Self == String, S == String)
|
|
257
|
+
@_specialize(where Self == String.UTF8View, S == String.UTF8View)
|
|
258
|
+
func editDistance<S: StringZillaViewable>(from other: S, bound: UInt64 = 0) throws -> UInt64? {
|
|
259
|
+
var result: UInt64?
|
|
260
|
+
|
|
261
|
+
// Use a do-catch block to handle potential errors
|
|
262
|
+
do {
|
|
263
|
+
try withStringZillaScope { hPointer, hLength in
|
|
264
|
+
try other.withStringZillaScope { nPointer, nLength in
|
|
265
|
+
result = UInt64(sz_edit_distance(hPointer, hLength, nPointer, nLength, sz_size_t(bound), nil))
|
|
266
|
+
if result == SZ_SIZE_MAX {
|
|
267
|
+
result = nil
|
|
268
|
+
throw StringZillaError.memoryAllocationFailed
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
} catch {
|
|
273
|
+
// Handle or rethrow the error
|
|
274
|
+
throw error
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
return result
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Test.swift
|
|
3
|
+
//
|
|
4
|
+
//
|
|
5
|
+
// Created by Ash Vardanian on 18/1/24.
|
|
6
|
+
//
|
|
7
|
+
|
|
8
|
+
import XCTest
|
|
9
|
+
@testable import StringZilla
|
|
10
|
+
|
|
11
|
+
class StringZillaTests: XCTestCase {
|
|
12
|
+
|
|
13
|
+
var testString: String!
|
|
14
|
+
|
|
15
|
+
override func setUp() {
|
|
16
|
+
super.setUp()
|
|
17
|
+
testString = "Hello, world! Welcome to StringZilla. π"
|
|
18
|
+
XCTAssertEqual(testString.count, 39)
|
|
19
|
+
XCTAssertEqual(testString.utf8.count, 42)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
func testFindFirstSubstring() {
|
|
23
|
+
let index = testString.findFirst(substring: "world")!
|
|
24
|
+
XCTAssertEqual(testString[index...], "world! Welcome to StringZilla. π")
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
func testFindLastSubstring() {
|
|
28
|
+
let index = testString.findLast(substring: "o")!
|
|
29
|
+
XCTAssertEqual(testString[index...], "o StringZilla. π")
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
func testFindFirstCharacterFromSet() {
|
|
33
|
+
let index = testString.findFirst(characterFrom: "aeiou")!
|
|
34
|
+
XCTAssertEqual(testString[index...], "ello, world! Welcome to StringZilla. π")
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
func testFindLastCharacterFromSet() {
|
|
38
|
+
let index = testString.findLast(characterFrom: "aeiou")!
|
|
39
|
+
XCTAssertEqual(testString[index...], "a. π")
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
func testFindFirstCharacterNotFromSet() {
|
|
43
|
+
let index = testString.findFirst(characterNotFrom: "aeiou")!
|
|
44
|
+
XCTAssertEqual(testString[index...], "Hello, world! Welcome to StringZilla. π")
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// TODO: This fails!
|
|
48
|
+
// func testFindLastCharacterNotFromSet() {
|
|
49
|
+
// let index = testString.findLast(characterNotFrom: "aeiou")!
|
|
50
|
+
// XCTAssertEqual(testString.distance(from: testString.startIndex, to: index), 38)
|
|
51
|
+
// XCTAssertEqual(testString[index...], "π")
|
|
52
|
+
// }
|
|
53
|
+
|
|
54
|
+
func testEditDistance() {
|
|
55
|
+
let otherString = "Hello, world!"
|
|
56
|
+
let distance = try? testString.editDistance(from: otherString) // Using try?
|
|
57
|
+
XCTAssertNotNil(distance)
|
|
58
|
+
XCTAssertEqual(distance, 29)
|
|
59
|
+
}
|
|
60
|
+
}
|
package/wasmer.toml
CHANGED
package/simsimd/bench.js
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
const benchmark = require('benchmark');
|
|
2
|
-
|
|
3
|
-
// Assuming the vectors are of the same length
|
|
4
|
-
function cosineDistance(a, b) {
|
|
5
|
-
let dotProduct = 0;
|
|
6
|
-
let magA = 0;
|
|
7
|
-
let magB = 0;
|
|
8
|
-
for (let i = 0; i < a.length; i++) {
|
|
9
|
-
dotProduct += a[i] * b[i];
|
|
10
|
-
magA += a[i] * a[i];
|
|
11
|
-
magB += b[i] * b[i];
|
|
12
|
-
}
|
|
13
|
-
return 1 - (dotProduct / (Math.sqrt(magA) * Math.sqrt(magB)));
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
// Generate random data for testing
|
|
18
|
-
const dimensions = 1536; // Adjust dimensions as needed
|
|
19
|
-
const array1 = Array.from({ length: dimensions }, () => Math.random() * 100);
|
|
20
|
-
const array2 = Array.from({ length: dimensions }, () => Math.random() * 100);
|
|
21
|
-
const floatArray1 = new Float32Array(array1);
|
|
22
|
-
const floatArray2 = new Float32Array(array2);
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
// Create benchmark suite
|
|
26
|
-
const singleSuite = new benchmark.Suite('Single Vector Processing');
|
|
27
|
-
|
|
28
|
-
// Single-vector processing benchmarks
|
|
29
|
-
singleSuite
|
|
30
|
-
|
|
31
|
-
// Pure JavaScript
|
|
32
|
-
.add('Array of Numbers', () => {
|
|
33
|
-
cosineDistance(array1, array2);
|
|
34
|
-
})
|
|
35
|
-
.add('TypedArray of Float32', () => {
|
|
36
|
-
cosineDistance(floatArray1, floatArray2);
|
|
37
|
-
})
|
|
38
|
-
.on('cycle', (event) => {
|
|
39
|
-
if (event.target.error) {
|
|
40
|
-
console.error(String(event.target.error));
|
|
41
|
-
} else {
|
|
42
|
-
console.log(String(event.target));
|
|
43
|
-
}
|
|
44
|
-
})
|
|
45
|
-
.on('complete', () => {
|
|
46
|
-
console.log('Fastest Single-Vector Processing is ' + singleSuite.filter('fastest').map('name'));
|
|
47
|
-
})
|
|
48
|
-
.run({
|
|
49
|
-
noCache: true,
|
|
50
|
-
async: false,
|
|
51
|
-
});
|