mirror of
https://github.com/libretro/RetroArch.git
synced 2026-11-24 18:45:38 -10:00
Performance improvements on a 50,000-record database (8.9 MB, 10 fields per record):
Query type Before After Speedup
Single CRC lookup (1/50k match) 0.132s 0.106s 20%
CRC or() (2/50k matches) 0.128s 0.108s 16%
Year filter (1250/50k matches) 0.134s 0.111s 17%
Full scan, no query 0.131s 0.130s 0% (no regression)
B3 is the highest-value target. Right now, for a query like {crc:or(b"31B965DB",...)}, the cursor fully deserializes every record (all 10-15 fields including name, description, md5, sha1, serial, publisher, developer...) into a DOM tree, then checks just the crc field against the filter, then frees everything and moves on. For a 50,000-record database where exactly 1 record matches, that's 49,999 complete parse→allocate→filter→free cycles that do nothing useful. A streaming skip that reads only the crc field and jumps past everything else would eliminate ~90% of the work per rejected record. Fixing B3 automatically reduces the impact of B1 and B2 because you simply stop parsing the fields you don't need.
B2 compounds with B3. Even if you can't do streaming skip, replacing the per-field malloc/free with an arena allocator (allocate a 4KB chunk, bump-allocate strings into it, free the whole arena when the record is rejected) would eliminate hundreds of thousands of heap operations per scan.
B1 is moderated by fread buffering as we established, but would be fully eliminated by mmap'ing the .rdb file (approach 3 from earlier). Mmap also enables zero-copy string references for B2.
The improvement comes from skipping ~90% of per-record work for rejected records: instead of deserializing all 10 fields into a DOM tree (10 key mallocs + 10 value mallocs + 10 key frees + 10 value frees), the fast path reads 1 key into a stack buffer, skip-reads 9 values without allocating, and only mallocs the 1 field the query actually inspects.
65 lines
2.6 KiB
C
65 lines
2.6 KiB
C
/* Copyright (C) 2010-2017 The RetroArch team
|
|
*
|
|
* ---------------------------------------------------------------------------------------
|
|
* The following license statement only applies to this file (rmsgpack.h).
|
|
* ---------------------------------------------------------------------------------------
|
|
*
|
|
* Permission is hereby granted, free of charge,
|
|
* to any person obtaining a copy of this software and associated documentation files (the "Software"),
|
|
* to deal in the Software without restriction, including without limitation the rights to
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
|
|
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
|
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
*/
|
|
|
|
#ifndef __LIBRETRODB_MSGPACK_H__
|
|
#define __LIBRETRODB_MSGPACK_H__
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <streams/interface_stream.h>
|
|
|
|
struct rmsgpack_read_callbacks
|
|
{
|
|
int (*read_nil )(void *);
|
|
int (*read_bool )(int, void *);
|
|
int (*read_int )(int64_t, void *);
|
|
int (*read_uint )(uint64_t, void *);
|
|
int (*read_string )(char *, uint32_t, void *);
|
|
int (*read_bin )(void *, uint32_t, void *);
|
|
int (*read_map_start )(uint32_t, void *);
|
|
int (*read_array_start)(uint32_t, void *);
|
|
};
|
|
|
|
int rmsgpack_write_array_header(intfstream_t *stream, uint32_t size);
|
|
|
|
int rmsgpack_write_map_header(intfstream_t *stream, uint32_t size);
|
|
|
|
int rmsgpack_write_string(intfstream_t *stream, const char *s, uint32_t len);
|
|
|
|
int rmsgpack_write_bin(intfstream_t *stream, const void *s, uint32_t len);
|
|
|
|
int rmsgpack_write_nil(intfstream_t *stream);
|
|
|
|
int rmsgpack_write_bool(intfstream_t *stream, int value);
|
|
|
|
int rmsgpack_write_int(intfstream_t *stream, int64_t value);
|
|
|
|
int rmsgpack_write_uint(intfstream_t *stream, uint64_t value);
|
|
|
|
int rmsgpack_read(intfstream_t *stream, struct rmsgpack_read_callbacks *callbacks, void *data);
|
|
|
|
int rmsgpack_read_uint(intfstream_t *fd, uint64_t *s, size_t len);
|
|
|
|
int rmsgpack_skip_value(intfstream_t *fd);
|
|
|
|
#endif
|