loon
High-performance header-only C++ library for low-latency applications
Loading...
Searching...
No Matches
loon

Build Status codecov Documentation [License: MIT](LICENSE) C++23 [Header Only]()

Loon Banner

loon – High-Performance, Header-Only Modern C++ Library

loon is a lightweight, header-only C++ library designed for low memory footprint, low latency, and maximum performance. It provides optimized data structures and utilities that outperform standard STL counterparts while maintaining full STL compatibility.


Key Features:

  • Zero-Cost Abstractions: Header-only design with no external dependencies.
  • STL-Compliant: Drop-in replacements with familiar interfaces.
  • Cache-Efficient: Optimized memory layout (SoA, pooling, alignment).
  • Low Latency: Avoids dynamic allocation on critical paths; ideal for real-time systems.
  • High Performance: Benchmarked against std:: types—faster insertion, lookup, and iteration.

Perfect for performance-critical applications in HFT, gaming, embedded systems, and real-time processing.


Components

Header Description
loon/lru.hpp LRU cache with O(1) get/put operations
loon/redis_list.hpp Redis-style list with lpush/rpush/lpop/rpop/lrange
loon/ring_buffer.hpp Fixed-size ring buffer (circular queue) with O(1) push/pop

Installation

Using Conan (recommended)

Add to your conanfile.txt:

[requires]
loon/0.1.0

Or conanfile.py:

def requirements(self):
self.requires("loon/0.1.0")

Header-only (manual)

Copy the include/loon directory to your project and add it to your include path.


Usage

CMake

find_package(loon REQUIRED)
target_link_libraries(your_target PRIVATE loon::loon)

LRU Cache

#include <loon/lru.hpp>
loon::LRU<int, std::string> cache(100); // capacity of 100
cache.put(1, "hello");
cache.put(2, "world");
auto val = cache.get(1); // returns std::optional<std::reference_wrapper<V>>
if (val) {
std::cout << val->get() << std::endl; // "hello"
}
cache.exists(1); // true
cache.remove(1);
cache.size(); // 1
A Least Recently Used (LRU) cache with O(1) access and eviction.
Definition lru.hpp:29
Thread-safe LRU (Least Recently Used) cache implementation.

LRU Cache Complexity:

Operation Time Space
get(key) O(1) O(1)
put(key, value) O(1) O(1)
exists(key) O(1) O(1)
remove(key) O(1) O(1)
size() O(1) O(1)

Redis List

list.lpush(1); // push to front
list.rpush(2); // push to back
list.lpush(0); // [0, 1, 2]
auto val = list.lpop(); // returns std::optional<T>, removes from front
auto vals = list.rpop(2); // pop multiple from back
auto range = list.lrange(0, -1); // get all elements (supports negative indices)
list.llen(); // size
A Redis-compatible list supporting operations from both ends.
size_t lpush(const T &value)
Pushes a value to the front of the list (left push).
size_t llen() const
Returns the length of the list (Redis LLEN command).
std::optional< T > rpop()
Removes and returns the last element (right pop).
std::optional< T > lpop()
Removes and returns the first element (left pop).
std::vector< T > lrange(int start, int stop) const
Returns a range of elements without removing them.
size_t rpush(const T &value)
Pushes a value to the back of the list (right push).
Redis-compatible list data structure implementation.

Redis List Complexity:

Operation Time Space
lpush(value) O(1) O(1)
rpush(value) O(1) O(1)
lpop() O(1) O(1)
rpop() O(1) O(1)
lpop(count) O(count) O(count)
rpop(count) O(count) O(count)
lrange(start, stop) O(stop - start) O(stop - start)
llen() / size() O(1) O(1)
empty() O(1) O(1)

Ring Buffer

loon::RingBuffer<int, 1024> buffer; // fixed capacity, reject when full
loon::RingBuffer<int, 1024> ring(true); // override oldest when full
buffer.push(42);
buffer.push(43);
auto val = buffer.pop(); // returns std::optional<int>, removes front
auto f = buffer.front(); // peek front without removing
auto b = buffer.back(); // peek back without removing
buffer.discard(); // drop front element without returning it
buffer.size(); // current element count
buffer.empty(); // true if no elements
buffer.full(); // true if at capacity
A fixed-size circular buffer (ring buffer) with FIFO semantics.
std::optional< T > pop()
Removes and returns the front element.
size_t size() const
Returns the current number of elements.
bool empty() const
Checks if the buffer is empty.
bool full() const
Checks if the buffer is full.
std::optional< T > back()
Returns the back element without removing it.
std::optional< T > front()
Returns the front element without removing it.
bool discard()
Discards the front element without returning it.
bool push(const T &value)
Pushes a value to the back of the buffer.
Fixed-size ring buffer (circular queue) implementation.

Ring Buffer Complexity:

Operation Time Space
push(value) O(1) O(1)
pop() O(1) O(1)
front() / back() O(1) O(1)
discard() O(1) O(1)
size() / empty() / full() O(1) O(1)

Performance notes: Stack-allocated std::array backing — zero heap allocation, fully contiguous memory layout. Ideal for real-time, embedded, and latency-critical applications.


Building from Source

Prerequisites

  • C++23 compatible compiler (GCC 13+, Clang 14+)
  • CMake 3.20+
  • Conan 2.x

Build and Test

# Setup Conan (one-time)
make conan-setup
# Install dependencies
make deps
# Build and run tests
make build
# Create and test Conan package
make package

Other Targets

make help # Show all targets
make coverage # Generate coverage report
make check-format # Check code formatting
make format # Apply clang-format
make clean # Clean build files

License

MIT License - see [LICENSE](LICENSE) for details.