{"id":16840852,"url":"https://github.com/emeryberger/cheap","last_synced_at":"2025-03-22T05:31:01.313Z","repository":{"id":47612783,"uuid":"323743481","full_name":"emeryberger/cheap","owner":"emeryberger","description":"Cheap: customized heaps for improved application performance.","archived":false,"fork":false,"pushed_at":"2022-10-11T17:45:35.000Z","size":1776,"stargazers_count":27,"open_issues_count":3,"forks_count":5,"subscribers_count":7,"default_branch":"main","last_synced_at":"2025-03-18T08:22:18.391Z","etag":null,"topics":["linux","memory-allocation","memory-allocator","memory-management","performance-optimization","profiler"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/emeryberger.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-12-22T22:09:32.000Z","updated_at":"2024-12-24T18:40:15.000Z","dependencies_parsed_at":"2023-01-19T22:46:52.162Z","dependency_job_id":null,"html_url":"https://github.com/emeryberger/cheap","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/emeryberger%2Fcheap","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/emeryberger%2Fcheap/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/emeryberger%2Fcheap/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/emeryberger%2Fcheap/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/emeryberger","download_url":"https://codeload.github.com/emeryberger/cheap/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244912800,"owners_count":20530764,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["linux","memory-allocation","memory-allocator","memory-management","performance-optimization","profiler"],"created_at":"2024-10-13T12:38:22.983Z","updated_at":"2025-03-22T05:30:57.109Z","avatar_url":"https://github.com/emeryberger.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Cheap\n\nCheap: a `malloc`/`new` optimizer\n\nby [Emery Berger](https://emeryberger.com)\n\n# About Cheap\n\nCheap is a system that makes it easy to improve the performance of\nmemory-intensive C/C++ applications. Cheap works by replacing memory\nallocation operations with a custom heap that can substantially\nimprove performance. Unlike past approaches, Cheap requires almost no\nprogramming effort. For example, by adding _just one line of code_\n(plus an `#include`), we were able to speed up the Boost JSON library\nby 30-40%.\n\n# How it works\n\nThe Cheap library (`libcheap.so`) and its header `cheap.h` let you use\na custom heap with little effort. One line of code creates a custom\nheap with certain characteristics and temporarily redirects _all_\nmemory allocation calls (`malloc`, `free`, `new`, `delete`) to that\nheap until the heap goes out of scope.\n\nTo use Cheap, you include the file `cheap.h`, and then insert a line of code creating a custom heap. For example:\n\n    cheap::cheap\u003ccheap::NONZERO | cheap::SINGLE_THREADED | cheap::DISABLE_FREE\u003e r;\n\nThe options for the heap are passed as a series of flags, each separated by `|`.\n\nCurrent options for the custom heap are the following:\n\n* `cheap::NONZERO` -- no requests for 0 bytes\n* `cheap::ALIGNED` -- all size requests are suitably aligned\n* `cheap::SINGLE_THREADED` -- all allocations and frees are by the same thread\n* `cheap::SIZE_TAKEN` -- need to track object sizes for `realloc` or `malloc_usable_size`\n* `cheap::SAME_SIZE` -- all object requests are the same size; pass the size as the second argument to the constructor\n* `cheap::DISABLE_FREE` -- turns `free` calls into no-ops\n\nOnce you place this line at the appropriate point in your program, it\nwill redirect all subsequent allocations and frees to use the\ngenerated custom heap. _Using `cheap::DISABLE_FREE` gives you the effect of a \"region-style\" allocator (a.k.a. \"arena\", \"pool\", or \"monotonic\nresource\"); otherwise, you get a customized freelist implementation._\n\nOnce this object goes out of scope ([RAII-style](https://en.cppreference.com/w/cpp/language/raii), like \n[`std::lock_guard`](https://en.cppreference.com/w/cpp/thread/lock_guard)), the program reverts to ordinary behavior, using the\nsystem-supplied memory allocator, and the custom heap's memory is\nreclaimed.\n\n## Placing a custom heap\n\nSometimes, placing a custom heap is straightforward, but it's nice to\nhave help. We provide a tool called `Cheaper` that finds places in\nyour program where a custom heap could potentially improve\nperformance, and which generates the line of code to insert.\n\n### Collecting a trace with `libcheaper`\n\nFirst, run your program with the Cheaper library, as follows:\n\n    LD_PRELOAD=libcheaper.so ./yourprogram\n\nor, on Mac:\n\n    DYLD_INSERT_LIBRARIES=libcheaper.dylib ./yourprogram\n    \nThis generates a file `cheaper.out` in the current directory. That file is a JSON file that contains information used by the `cheaper.py` script. NOTE: you need to compile your program with several flags to make sure Cheaper can get enough information to work. For example:\n\n    clang++ -g -fno-inline-functions -O0 yourprogram.cpp -o yourprogram\n\n### Analyze the trace with `cheaper.py`\n\nYou may need to install Rust (https://www.rust-lang.org/tools/install) the first time you\nrun Cheaper:\n\n    curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh\n\nNow run Cheaper as follows:\n\n    python3 cheaper.py --progname ./yourprogram\n\nFor a complete list of options, type `python3 cheaper.py --help`.\n\nCheaper will produce output, in ranked order from most to least\npromising, corresponding to places in the code where you should insert\nthe `cheap` declaration. It outputs the line of code, which you can\nthen copy and paste directly into your program.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femeryberger%2Fcheap","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femeryberger%2Fcheap","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femeryberger%2Fcheap/lists"}