OPIC
Object Persistence In C
|
OPIC is a revolutionary serialization framework for C. Unlike traditional approaches which walk through the in-memory objects and write it to disk, OPIC itself is a memory allocator where all the objects created are backed by a memory mapped file. To save the current snapshot of objects , simply call OPHeapMSync()
and all the changes are flushed to disk. Regular serialization may take several seconds, even up to minutes to write large amont of data. In contrast, OPIC only takes few milliseconds to perform both read and write. For reading, mmap()
system call only takes 0.0005 seconds to map the file into memory region. The data is not loaded into memory until the program access it. For writing, msync()
is quite fast as well.
Using OPIC we can quickly draft out a poor man's key value store. OPIC provides two types of hash table: OPHashTable
for fixed length key (like CHAR(20)
in database), and PascalHashTable
for length varying key. PascalHashTable
provides a short string optimization similar to what C++ does. Instead of using 24 bytes for inline string in hash table buckets, user can specify the size of the inline buffer. If the size of the key exceeds the inline buffer, extra space is allocated in OPIC to hold the key and the bucket would represent as pointer to key instead of inlined key.
See the example below on how to write a mini key-value store in few lines of C.
I wrote a initial draft to explain how it works. Check out my blog post: http://www.idryman.org/blog/2017/06/28/opic-a-memory-allocator-for-fast-serialization/
Copyright (c) 2016, 2017 Felix Chern
OPIC is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. OPIC is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with OPIC. If not, see <http://www.gnu.org/licenses/>.
Cityhash is included in this project. Here is the copyright statements for cityhash:
Copyright (c) 2011 Google, Inc. 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.
Please follow the GNU Coding Standards. On the moment of this writing we still have some portion of the code hasn't yet convert to GNU formatting styles, but this should be fixed in near future.