@@ -87,14 +87,23 @@ if (result) |r| {
8787}
8888```
8989
90+ Use a ` Cache ` to skip decoding when different IPs resolve to the same record.
91+
92+ ``` zig
93+ var cache: maxminddb.Cache(maxminddb.geolite2.City) = .{};
94+ defer cache.deinit();
95+
96+ const city = try db.lookup(allocator, maxminddb.geolite2.City, ip, .{ .cache = &cache });
97+ ```
98+
9099Here are reference results on Apple M2 Pro (1M random IPv4 lookups against GeoLite2-City
91100with ` ipv4_index_first_n_bits = 16 ` ):
92101
93- | Benchmark | All fields | Filtered (city) |
94- | --- | --- | --- |
95- | ` geolite2.City ` | ~ 1,284,000 | ~ 1,348,000 |
96- | ` MyCity ` | ~ 1,383,000 | — |
97- | ` any.Value ` | ~ 1,254,000 | ~ 1,349,000 |
102+ | Type | Default | ` .only ` | ` Cache ` |
103+ | --- | --- | --- | --- |
104+ | ` geolite2.City ` | ~ 1,284,000 | ~ 1,348,000 | ~ 1,474,000 |
105+ | ` MyCity ` | ~ 1,383,000 | | |
106+ | ` any.Value ` | ~ 1,254,000 | ~ 1,349,000 | |
98107
99108<details >
100109
@@ -140,6 +149,30 @@ Lookups Per Second (avg):1315870.3443053183
140149
141150<details >
142151
152+ <summary >geolite2.City with Cache</summary >
153+
154+ ``` sh
155+ $ for i in $( seq 1 10) ; do
156+ zig build benchmark_lookup_cache -Doptimize=ReleaseFast -- GeoLite2-City.mmdb 1000000 \
157+ 2>&1 | grep ' Lookups Per Second'
158+ done
159+
160+ Lookups Per Second (avg):1493822.3908664712
161+ Lookups Per Second (avg):1503051.0049070602
162+ Lookups Per Second (avg):1499514.437731375
163+ Lookups Per Second (avg):1491749.9700251492
164+ Lookups Per Second (avg):1449924.9391983037
165+ Lookups Per Second (avg):1396100.6211600688
166+ Lookups Per Second (avg):1465750.9875955326
167+ Lookups Per Second (avg):1515611.9396877384
168+ Lookups Per Second (avg):1485235.6423035355
169+ Lookups Per Second (avg):1439334.222943596
170+ ```
171+
172+ </details >
173+
174+ <details >
175+
143176<summary >MyCity</summary >
144177
145178``` sh
@@ -203,3 +236,80 @@ Lookups Per Second (avg):1315986.2950186788
203236```
204237
205238</details >
239+
240+ Use ` within() ` to iterate over all networks in the database.
241+ A ` Cache ` avoids re-decoding networks that share the same record.
242+
243+ ``` zig
244+ var cache: maxminddb.Cache(maxminddb.any.Value) = .{};
245+ defer cache.deinit();
246+
247+ var it = try db.within(
248+ allocator,
249+ maxminddb.any.Value,
250+ maxminddb.Network.all_ipv6,
251+ .{ .cache = &cache },
252+ );
253+ defer it.deinit();
254+
255+ while (try it.next()) |item| {
256+ std.debug.print("{f} {f}\n", .{item.network, item.value});
257+ }
258+ ```
259+
260+ Without a cache each result owns its memory and must be freed with ` item.deinit() ` .
261+
262+ Here are reference results on Apple M2 Pro (full GeoLite2-City scan using ` any.Value ` ):
263+
264+ | Mode | Records/sec |
265+ | --- | --- |
266+ | Default | ~ 1,235,000 |
267+ | ` Cache ` | ~ 2,900,000 |
268+
269+ <details >
270+
271+ <summary >no cache (any.Value)</summary >
272+
273+ ``` sh
274+ $ for i in $( seq 1 10) ; do
275+ zig build benchmark_within -Doptimize=ReleaseFast -- GeoLite2-City.mmdb \
276+ 2>&1 | grep ' Records Per Second'
277+ done
278+
279+ Records Per Second: 1216758.945145436
280+ Records Per Second: 1238440.9772222256
281+ Records Per Second: 1234710.6362391203
282+ Records Per Second: 1229527.4688849829
283+ Records Per Second: 1243478.3908140333
284+ Records Per Second: 1226863.3718734735
285+ Records Per Second: 1240073.3248202254
286+ Records Per Second: 1247541.1528026997
287+ Records Per Second: 1230510.441029532
288+ Records Per Second: 1246311.587919839
289+ ```
290+
291+ </details >
292+
293+ <details >
294+
295+ <summary >cache (any.Value)</summary >
296+
297+ ``` sh
298+ $ for i in $( seq 1 10) ; do
299+ zig build benchmark_within_cache -Doptimize=ReleaseFast -- GeoLite2-City.mmdb \
300+ 2>&1 | grep ' Records Per Second'
301+ done
302+
303+ Records Per Second: 2847560.3756875996
304+ Records Per Second: 2925388.867798729
305+ Records Per Second: 2919203.9046571665
306+ Records Per Second: 2814410.555872645
307+ Records Per Second: 2933972.04386147
308+ Records Per Second: 2900700.06160036
309+ Records Per Second: 2922279.338699886
310+ Records Per Second: 2862525.847598088
311+ Records Per Second: 2916760.542913819
312+ Records Per Second: 2908245.98918392
313+ ```
314+
315+ </details >
0 commit comments