{"id":23208981,"url":"https://github.com/coralblocks/coralpool","last_synced_at":"2025-08-19T04:31:45.202Z","repository":{"id":268638843,"uuid":"904637717","full_name":"coralblocks/CoralPool","owner":"coralblocks","description":"Multiple fast implementations of object pooling classes designed to efficiently reuse mutable objects, reducing memory allocations and eliminating the garbage collector overhead.","archived":false,"fork":false,"pushed_at":"2024-12-18T01:49:24.000Z","size":60,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-12-18T02:38:30.172Z","etag":null,"topics":["garbage-collector","garbage-free","gc","java","low-latency","ultra-low-latency","zero-garbage","zero-gc"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/coralblocks.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-12-17T09:22:30.000Z","updated_at":"2024-12-18T01:49:25.000Z","dependencies_parsed_at":"2024-12-18T02:38:35.494Z","dependency_job_id":"347452c1-252e-4ea8-8380-89f8ac32c707","html_url":"https://github.com/coralblocks/CoralPool","commit_stats":null,"previous_names":["coralblocks/coralpool"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coralblocks%2FCoralPool","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coralblocks%2FCoralPool/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coralblocks%2FCoralPool/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coralblocks%2FCoralPool/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/coralblocks","download_url":"https://codeload.github.com/coralblocks/CoralPool/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230318623,"owners_count":18207812,"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":["garbage-collector","garbage-free","gc","java","low-latency","ultra-low-latency","zero-garbage","zero-gc"],"created_at":"2024-12-18T18:13:33.345Z","updated_at":"2025-08-19T04:31:45.176Z","avatar_url":"https://github.com/coralblocks.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# CoralPool\n\nCoralPool is a high-performance, lightweight and garbage-free Java _object pool_ implementation. It efficiently reuses mutable objects, minimizing the creation of _short-lived_ objects that would otherwise burden the garbage collector. The pool can grow to accommodate more instances by internally allocating new objects via the `get()` method or by accepting external objects (not originally allocated by the pool) through the `release(E)` method.\n\n\u003cpre\u003e\n\u003cb\u003eNote:\u003c/b\u003e For a discussion of developing garbage-free applications you should refer to \u003ca href=\"https://youtu.be/bhzv6lJtuOs\"\u003ethis video\u003c/a\u003e\n\u003c/pre\u003e\n\n## ObjectPool Interface\n```java\npublic interface ObjectPool\u003cE\u003e {\n\n   /**\n    * Retrieves an instance from this object pool. If no instances are currently available,\n    * a new instance will be created, and the pool will grow in size if necessary to \n    * accommodate more instances. This method can never return null.\n    * \n    * @return an instance from the pool\n    */\n    public E get();\n\n   /**\n    * Returns an instance to this object pool. If the pool has no available space \n    * to accommodate the instance, it will expand as needed. The pool can accept \n    * external instances that were not necessarily created by it. \n    * Passing null as the instance will result in an exception being thrown.\n    * \n    * @param instance the instance to return to the pool\n    * @throws IllegalArgumentException if the provided instance is null\n    */\n    public void release(E instance);\n}\n```\n\n#### Example:\n```java\n// the pool can grow, but it will start with 100 slots\nfinal int initialCapacity = 100;\n\n// the pool can allocate more instances later, but it will start with 50 instances\nfinal int preloadCount = 50;\n\n// the pool can use an ObjectBuilder, but it can also take a Class for creating instances through\n// the default constructor\nfinal Class\u003cStringBuilder\u003e klass = StringBuilder.class;\n\n// Create your object pool\nObjectPool\u003cStringBuilder\u003e pool = new LinkedObjectPool\u003c\u003e(initialCapacity, preloadCount, klass);\n\n// Fetch an instance from the pool\nStringBuilder sb = pool.get();\n\n// Do whatever you want with the instance\n// (...)\n\n// When you are done return the instance back to the pool\npool.release(sb);\n```\n## ObjectPool Implementations\n\n### LinkedObjectPool\n\nAn `ObjectPool` backed by an internal linked-list. It can gradually grow by adding new nodes to the list.\n\n### ArrayObjectPool\n\nAn `ObjectPool` backed by an internal array. It can expand by allocating a larger array. When that happens, the previous array is retained as a [SoftReference](https://docs.oracle.com/en/java/javase/23/docs/api/java.base/java/lang/ref/SoftReference.html) to delay garbage collection as much as possible.\nYou can manually release these previous array references by calling its `releaseSoftReferences()` public method.\n\n### MultiArrayObjectPool\n\nAn `ObjectPool` backed by an internal doubly linked-list of arrays. It expands by adding new nodes, each containing a newly allocated array, to the linked-list.\n\n### StackObjectPool\n\nAn `ObjectPool` backed by an internal stack, implemented with an array. It can expand by allocating a larger stack. When that happens, the previous array of the stack is retained as a [SoftReference](https://docs.oracle.com/en/java/javase/23/docs/api/java.base/java/lang/ref/SoftReference.html) to delay garbage collection as much as possible.\nYou can manually release these previous array references by calling its `releaseSoftReferences()` public method.\n\n### TieredObjectPool\n\nAn `ObjectPool` implementation utilizing a two-tier structure: an internal array and a linked-list.\nIt grows by adding new instances to the linked-list (second tier), ensuring the array (first tier) remains a fixed size and does not require expansion.\n\n## Benchmarks\n\nIdeally, an object pool should be configured at startup with a big enough initial capacity to avoid growth, maximizing performance. However, since growth cannot always be avoided, we conducted two benchmarks: one where the pool remains fixed in size and another where it grows. In the growth benchmark, the pool expands exclusively through additional `get()` calls, not by adding external instances via the `release(E)` method. Adding external instances, those not created internally by the pool, is rarely needed or desirable.\n\nYou can find the benchmarks [here](https://github.com/coralblocks/CoralPool/blob/main/src/main/java/com/coralblocks/coralpool/bench/ObjectPoolNoGrowthBench.java) (without growth) and [here](https://github.com/coralblocks/CoralPool/blob/main/src/main/java/com/coralblocks/coralpool/bench/ObjectPoolGrowthBench.java) (with growth). Below the results:\n\n\u003cpre\u003e\n\u003cb\u003eNote:\u003c/b\u003e We used the new \u003ca href=\"https://stackoverflow.com/questions/79275170/why-does-this-simple-and-small-java-code-runs-30x-faster-in-all-graal-jvms-but-n\"\u003e\u003cb\u003eJVMCI\u003c/b\u003e JIT\u003c/a\u003e (from Graal 23) for the benchmarks below\n\u003c/pre\u003e\n\n### Benchmark (_without_ growth)\n\n\u003cpre\u003e\nLinkedObjectPool        =\u003e 9,698,254 nanoseconds\n\u003cb\u003eArrayObjectPool         =\u003e 3,658,073 nanoseconds\u003c/b\u003e     \nMultiArrayObjectPool    =\u003e 3,900,745 nanoseconds\nStackObjectPool         =\u003e 3,813,075 nanoseconds\nTieredObjectPool        =\u003e 3,786,247 nanoseconds                   \n\u003c/pre\u003e\n\nThe results above are expected as native arrays are faster than linked lists because they store elements in contiguous memory, enhancing cache locality and sequential access. In contrast, linked lists use scattered references across memory, leading to more cache misses and slower traversal time due to the overhead of following pointers.\n\n### Benchmark (_with_ growth)\n\n\u003cpre\u003e\nLinkedObjectPool        =\u003e 241,073 microseconds\n\u003cb\u003eArrayObjectPool         =\u003e 87,784 microseconds\u003c/b\u003e     \nMultiArrayObjectPool    =\u003e 162,697 microseconds\nStackObjectPool         =\u003e 129,899 microseconds\nTieredObjectPool        =\u003e 237,377 microseconds                   \n\u003c/pre\u003e\n\nThe above results are expected because `TieredObjectPool` uses a linked list for growth, leading to the same cache miss inefficiencies observed in the previous benchmark _without_ growth. In contrast, `ArrayObjectPool` clearly outperforms all others, as its [growth implementation](https://github.com/coralblocks/CoralPool/blob/main/src/main/java/com/coralblocks/coralpool/ArrayObjectPool.java#L189) is more efficient by avoiding both array copying and nulling.\n\n\u003cpre\u003e\n\u003cb\u003eNote:\u003c/b\u003e All benchmarks were executed with \u003ci\u003e-verbose:gc\u003c/i\u003e to make sure no GC activity ever takes place\n\u003c/pre\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcoralblocks%2Fcoralpool","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcoralblocks%2Fcoralpool","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcoralblocks%2Fcoralpool/lists"}