Code Monkey home page Code Monkey logo

pool.lua's Introduction

pool.lua

Helper library for creating and managing pools of objects in Lua.

Build Status Coverage Status License

Example

local pool = require "pool"

local function newObject()
	local obj = {
		foo = "bar",
		reset = function()
			print("reset!")
		end
	}
	return obj
end

local objectPool = pool.create(newObject, 32)
local object = objectPool:obtain()

print(object.foo)

objectPool:free(object)

Explanation

Object pools are useful for objects that are frequently being created and destroyed (for instance, bullets in a game). Using pools in a situation where creating and destroying instances are expensive can offer a performance boost. For more about object pooling, look here.

Creating a pool

Naturally, a pool must be created before it can be used.

pool.create(newObjectFunc, numObjects): Creates a pool.

  • newObjectFunc is a function that must return the object that is to be inserted into the pool.
  • numObjects is the number of objects that will be immediately added into the pool. By default, this is 16.

Obtaining and freeing objects

Now that a pool has been created, we can take and put back objects from and into the pool.

pool:obtain(): Obtains an object from the set of currently free objects.

pool:free(object): Puts an object back into the pool for later use.

  • object is the object to be freed
  • If the object being freed contains a function named reset, it will be called when it is added back into the pool. Use this to reset any values on the object that you don't want sticking around between uses.

Purging objects from a pool

The pool of free objects can also be cleared.

pool:clear(): Clears the free object pool.

Usage

Copy/paste pool.lua into your source folder, then use

local pool = require "pool"

wherever you need it.

pool.lua's People

Contributors

treamology avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.