All Projects → allenluce → mmap-object

allenluce / mmap-object

Licence: other
Shared Memory Objects for Node

Programming Languages

C++
36643 projects - #6 most used programming language
javascript
184084 projects - #8 most used programming language
python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to mmap-object

Flatbuffers
FlatBuffers: Memory Efficient Serialization Library
Stars: ✭ 17,180 (+18988.89%)
Mutual labels:  mmap
MPSC Queue
A multi-producer single consumer queue C++ template suitable for async logging with SHM IPC support
Stars: ✭ 51 (-43.33%)
Mutual labels:  shared-memory
python-sharearray
Share numpy arrays across processes efficiently ideal for large, read-only datasets
Stars: ✭ 32 (-64.44%)
Mutual labels:  mmap
rpmsg-lite
RPMsg implementation for small MCUs
Stars: ✭ 157 (+74.44%)
Mutual labels:  shared-memory
reactor-aeron
A reactive driver for Aeron transport (https://github.com/real-logic/aeron)
Stars: ✭ 43 (-52.22%)
Mutual labels:  shared-memory
javascript-easy-object
Now easily access or modify an object in javascript with javascript-easy-object.
Stars: ✭ 13 (-85.56%)
Mutual labels:  javascript-objects
Iceoryx
iceoryx - true zero-copy inter-process-communication
Stars: ✭ 208 (+131.11%)
Mutual labels:  shared-memory
IPC.Bond
IPC.Bond is an extension of IPC library that provides inter-process communication using shared memory on Windows with Bond serialization.
Stars: ✭ 26 (-71.11%)
Mutual labels:  shared-memory
boxtree
Quad/octree building for FMMs in Python and OpenCL
Stars: ✭ 52 (-42.22%)
Mutual labels:  shared-memory
AmeisenNavigation
Navigationmesh Server for my bot based on the TrinityCore MMAP's and Recast & Detour
Stars: ✭ 35 (-61.11%)
Mutual labels:  mmap
SPSC Queue
A highly optimized single producer single consumer message queue C++ template
Stars: ✭ 185 (+105.56%)
Mutual labels:  shared-memory
polardb-race
第一届POLARDB数据库性能大赛-初赛第5名(JAVA)-复赛第7名(CPP)
Stars: ✭ 24 (-73.33%)
Mutual labels:  mmap
pe-util
List shared object dependencies of a portable executable (PE)
Stars: ✭ 45 (-50%)
Mutual labels:  shared-memory
mmappickle
Python 3 library to store memory mappable objects into pickle-compatible files
Stars: ✭ 34 (-62.22%)
Mutual labels:  mmap
mmap-io
Clean straight forward mmap-bindings for node.js
Stars: ✭ 62 (-31.11%)
Mutual labels:  mmap
s3parcp
Faster than s3cp
Stars: ✭ 31 (-65.56%)
Mutual labels:  mmap
lua-shm-state-poc
Lua state in shared memory: a proof of concept
Stars: ✭ 22 (-75.56%)
Mutual labels:  shared-memory
box
Box - Open Standard Archive Format, a zip killer.
Stars: ✭ 38 (-57.78%)
Mutual labels:  mmap
radixmmap
Mmap radix sort file by a fixed length prefix of each line
Stars: ✭ 52 (-42.22%)
Mutual labels:  mmap
portable-memory-mapping
Portable Memory Mapping C++ Class (Windows/Linux)
Stars: ✭ 34 (-62.22%)
Mutual labels:  mmap

Shared Memory Objects

Build Status

Super-fast file-based sharing of Javascript objects among multiple processes.

This module maps Javascript objects into shared memory for simultaneous access by different Node processes running on the same machine. Shared memory is loaded via mmap. Object access is mediated by Boost's unordered map class so object property access are speedy.

Data is lazily loaded piece-by-piece as needed so opening even a huge file takes no time at all.

There are two modes:

Unshared Write-only Mode

A single process creates a new file which is mapped to a Javascript object. Setting properties on this object writes those properties to the file. You can read from the object within this mode but sharing an object in write-only mode with other processes is certain to result in crashes.

Shared Read-only mode

Open an existing file for reading. Multiple processes can safely open this file. Opening is lightning fast and only a single copy remains in memory.

Faster performance with buffers

If you use lengthy data values, buffers can speed things up considerably. In rough benchmarking, a 300% speedup was see when reading 20k-byte values as buffers instead of strings. For 200k-byte values, the speedup was 2000%.

Requirements

Binaries are provided for OSX and Linux for various node versions (check the releases page to see which). If a binary is not provided for your platform, you will need Boost and and a C++11 compliant compiler (like GCC 4.8 or better) to build the module.

Installation

npm install mmap-object

Usage

const Shared = require('mmap-object')
const shared_object = new Shared.Create('filename')

shared_object['new_key'] = 'a string value'
shared_object.new_property = Buffer.from('a buffer value, supporting Unicode™')
shared_object['useless key'] = 0

// Erase a key
delete shared_object['useless_key']
shared_object.close()

// Read a file
const read_only_shared_object = new Shared.Open('filename')
console.log(`My value is ${read_only_shared_object.new_key}`)
console.log(`My other value is ${read_only_shared_object.new_property}`)

read_only_shared_object.close()

API

new Create(path, [file_size], [initial_bucket_count], [max_file_size])

Creates a new file mapped into shared memory. Returns an object that provides access to the shared memory. Throws an exception on error.

Arguments

  • path - The path of the file to create
  • file_size - Optional The initial size of the file in kilobytes. If more space is needed, the file will automatically be grown to a larger size. Minimum is 500 bytes. Defaults to 5 megabytes.
  • initial_bucket_count - Optional The number of buckets to allocate initially. This is passed to the underlying Boost unordered_map. Defaults to 1024. Set this to the number of keys you expect to write.
  • max_file_size - Optional The largest the file is allowed to grow in kilobites. If data is added beyond this limit, an exception is thrown. Defaults to 5 gigabytes.

Example

// Create a 500K map for 300 objects.
const obj = new Shared.Create('/tmp/sharedmem', 500, 300)

new Open(path)

Maps an existing file into shared memory. Returns an object that provides read-only access to the object contained in the file. Throws an exception on error. Any number of processes can open the same file but only a single copy will reside in memory. Uses mmap under the covers, so only those parts of the file that are actually accessed will be loaded.

Arguments

  • path - The path of the file to open

Example

// Open up that shared file
const obj = new Shared.Open('/tmp/sharedmem')

close()

Unmaps a previously created or opened file. If the file was most recently opened with Create(), close() will first shrink the file to remove any unneeded space that may have been allocated.

It's important to close your unused shared files in long-running processes. Not doing so keeps shared memory from being freed.

The closing of very large objects (a few gigabytes and up) may take some time (hundreds to thousands of milliseconds). To prevent blocking the main thread, pass a callback to close(). The call to close() will return immediately while the callback will be called after the underlying munmap() operation completes. Any error will be given as the first argument to the callback.

Example

obj.close(function (err) {
  if (err) {
    console.error(`Error closing object: ${err}`)
  }
})

Iteration

The iterable protocol is supported for efficient iteration over the entire contents of the object:

const Shared = require('mmap-object')
const obj = new Shared.Open('filename')

for (let [key, value] of obj) {
    console.log(`${key} => ${value}`)
}

(This ES6 syntax is supported in Node 6+, for previous versions of node a more laborious syntax is necessary.)

isData()

When iterating, use isData() to tell if a particular key is real data or one of the underlying methods on the shared object:

const obj = new Shared.Open('/tmp/sharedmem')

for (let key in obj) {
  if (obj.isData(key)) { // Only show actual data
      console.log(key + ': ' + obj[key])
  }
}

isOpen()

Return true if this object is currently open.

isClosed()

Return true if this object has been closed.

get_free_memory()

Number of bytes of free storage left in the shared object file.

get_size()

The size of the storage in the shared object file, in bytes.

bucket_count()

The number of buckets currently allocated in the underlying hash structure.

max_bucket_count()

The maximum number of buckets that can be allocated in the underlying hash structure.

load_factor()

The average number of elements per bucket.

max_load_factor()

The current maximum load factor.

Unit tests

npm test

Limitations

It is strongly recommended to pass in the number of keys you expect to write when creating the object with Create. If you don't do this, the object will resize as you fill it up. This can be a very time-consuming process and can result in fragmentation within the shared memory object and a larger final file size.

Object values may be only string, buffer, or number values. Attempting to set a different type value results in an exception.

Symbols are not supported as properties.

Publishing a binary release

To make a new binary release:

  • Edit package.json. Increment the version property.
  • node-pre-gyp rebuild
  • node-pre-gyp package
  • node-pre-gyp-github publish
  • npm publish

You will need a NODE_PRE_GYP_GITHUB_TOKEN with repo:status, repo_deployment and public_repo access to the target repo. You'll also need write access to the npm repo.

MSVS build prerequisites

Set up Boost.

Set BOOST_ROOT environment variable.

bootstrap
b2 --build-type=complete
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].