Skip to main content
← All field notes

Field note / On-Device Vector Search

On-Device Vector Search in Swift

How to build distraction-free local semantic search on iOS using CoreML feature extraction and native Swift HNSW index graphs, grounded in Notiary.

Joshua Hrisko, Principal Engineer at MakerPortal

Joshua HriskoPrincipal Engineer

5 min readSan Francisco, CA

On-Device Vector Search in Swift
AI-generated illustration · decorative; it carries no data, and every figure in this post is cited inline

Building semantic search for local markdown notes or private documents requires running both vector embedding generation and nearest-neighbor retrieval 100% offline on-device.

In Notiary (App Store), our distraction-free iOS notes app, we execute local BERT/BGE embeddings on Apple Neural Engine (ANE) and search candidates using a native Hierarchical Navigable Small World (HNSW) vector graph in Swift.


1. Cosine Distance & Vector Normalization Math

Given a query vector q\vec{q} and document embedding d\vec{d}, the cosine similarity is:

CosSim(q,d)=qdqd\text{CosSim}(\vec{q}, \vec{d}) = \frac{\vec{q} \cdot \vec{d}}{\|\vec{q}\| \|\vec{d}\|}

If vectors are L2-normalized upon creation (q=1,d=1\|\vec{q}\| = 1, \|\vec{d}\| = 1), cosine similarity simplifies to a simple dot product:

CosSim(q,d)=i=1nqidi\text{CosSim}(\vec{q}, \vec{d}) = \sum_{i=1}^{n} q_i d_i

In SIMD-accelerated Swift (Accelerate.framework), this dot product executes in less than 1.1 microseconds per 384-dimensional vector.


2. Flat Scan vs HNSW Recall Tradeoff

For small note archives (< 2,000 items), brute-force flat linear scans across normalized float arrays in SIMD are faster than traversing graph nodes due to zero pointer-indirection overhead:

Corpus SizeExact Flat Scan (SIMD)HNSW Graph (M=16,ef=64M=16, \text{ef}=64)Memory Overhead
500 Notes0.8 ms0.4 msNone
5,000 Notes7.2 ms1.1 ms+20% Graph Nodes
50,000 Notes71.0 ms2.8 ms (98% Recall)+35% Graph Nodes

To dive deeper into deep learning algorithms and spatial audio hardware:

Try out our live interactive Vector Retrieval Recall Lab playground to measure recall loss, skipped candidates, and graph traversal performance live in your browser.


4. The Power of Local Semantics

By pairing Apple’s Neural Engine with native Swift implementations of algorithms like HNSW, we bypass network latency and privacy concerns entirely. For small to medium local knowledge bases, on-device semantic search isn’t just a gimmick—it’s a massive UX improvement, bringing the power of modern retrieval directly to the user’s pocket without sacrificing a millisecond of speed.

FAQ

Why is SIMD dot product faster than HNSW for less than 2,000 vectors?

HNSW graph traversal requires following pointer references across memory nodes. For fewer than 2,000 L2-normalized 384-dimensional vectors, contiguous array memory access with Swift's Accelerate vDSP SIMD instructions finishes in under 1.5ms with zero graph overhead.

How does CoreML target the Apple Neural Engine (ANE) for embeddings?

By setting MLModelConfiguration.computeUnits = .all, CoreML automatically offloads transformer encoder layers to the ANE coprocessor, freeing the GPU and main CPU cores for UI rendering.

Recommended Studio & Hardware Gear

Affiliate links support independent R&D

Tested studio equipment and reference hardware utilized for this build. Product images & pricing sourced from Amazon Creators API / SparkFun Electronics.