Giter Site home page Giter Site logo

safetypes2's Introduction

SafeTypes2

Introduction

Reference counting is a popular method for resource management. Its advantages include avoid copying, small overhead, and easy to reason with. However, when there's reference cycle, memory leak occurs. Typical solution to this is to use weak references. My comment on this is:

memory leak caused by reference cycle is a by-product of conflating memory management and data structure.

Instead of requiring the programmer to carefully implant weak references, in the original SafeTypes library, a different approach was chosen, where users are required to specify a "semantic" when assigning objects to containers. Those semantics included:

  • keep

    The object will be used later elsewhere, do not decrease reference count.

  • gave:

    The object will not be used any more elsewhere, change one of its reference to a container reference (known as "kept count" in the implementation). In other word: decrease the reference count and increase its kept count. This is most used when an object is created exclusively for saving on that container object.

  • fast:

    The gave semantic invokes garbage collector after altering the reference and kept count - in case a reference cycle is created exactly when the assignment occurs. If the programmer can be sure that this won't occur, they can use this semantic for optimization.

A sophisticated procedure was used to clean up circular references in the original SafeTypes library.

In the new SafeTypes2, the fast is subsummed into gave, and a "precise" garbage collection scheme is used. The reason the GC is able to be "precise" is because references had been categorized into: refcnt - references from lexical variables, and keptcnt - references from container objects.

The SafeType2 library is also "threading-aware" - the GC system of SafeTypes2 incorporates facility that allow safe creation and destruction of objects in multi-threaded programs; explicit synchronization from application is HOWEVER still needed to safely operate on objects created using the library.

Considering:

  1. explicitly locking every individual objects incurs too much overhead.
  2. group of objects (and related resources obtained elsewhere) may be operated on in concert, and a single "big" mutex may be able to offer "necessary and sufficient" protection.

Applications are recommended to use "cluster mutex" to protect clusters of related objects, and not to too frequently call synchronization functions.

Features

The library implements 3 types that're safe with regard to memory management:

  • data: for storing arbitrary byte string, and can be resized safely.
  • dict: a dictionary type that stores key-value pairs.
  • list: the ordered set type.

The library uses SipHash in the dictionary type. According to its designer, this hash function can be keyed to deter attacks on hash collision in dict. Users are strongly advised to set the key to one that's securely generated and that's different each time the program is run.

safetypes2's People

Contributors

dannyniu avatar

Watchers

 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.