Skip to content

lanl/goop

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Goop

Go Reference Go Report Card

Description

The Goop (Go Object-Oriented Programming) package provides support for dynamic object-oriented programming constructs in Go, much like those that appear in various scripting languages. The goal is to integrate fast, native-Go objects and slower but more flexible Goop objects within the same program.

Features

Goop provides the following features, which are borrowed from an assortment of object-oriented programming languages:

  • a prototype-based object model

  • support for both ex nihilo and constructor-based object creation

  • the ability to add, replace, and remove data fields and method functions at will

  • multiple inheritance

  • dynamically modifiable inheritance hierarchies (even on a per-object basis)

  • type-dependent dispatch (i.e., multiple methods with the same name but different argument types)

Installation

Goop is a Go module and therefore does not need to be installed manually. Simply import it as github.com/lanl/goop, and go build should download and install the code automatically.

Documentation

Pre-built documentation for the Goop API is available online at https://pkg.go.dev/github.com/lanl/goop.

Performance

Goop is unfortunately extremely slow. Goop programs have to pay for their flexibility in terms of performance. To determine just how bad the performance is on your computer, you can run the microbenchmarks included in goop_test.go:

    go test -bench=. -benchtime=5s github.com/lanl/goop

On my computer, I get results like the following (reformatted for clarity):

    BenchmarkNativeFNV1             2000000000                 4.59 ns/op
    BenchmarkNativeFNV1Closure      2000000000                 4.59 ns/op
    BenchmarkGoopFNV1                100000000                70.5  ns/op
    BenchmarkMoreGoopFNV1             10000000               794    ns/op
    BenchmarkEvenMoreGoopFNV1          5000000              2517    ns/op

See goop_test.go for the complete source code for those benchmarks. Basically,

  • BenchmarkNativeFNV1 is native (i.e., non-Goop) Go code for computing a 64-bit FNV-1 hash on a sequence of 0xFF bytes. Each iteration ("op" in the performance results) comprises a nullary function call, a multiplication by a large prime number, and an exclusive or with an 0xFF byte.

  • BenchmarkNativeFNV1Closure is the same but instead of calling an ordinary function each iteration, it invokes a locally defined closure.

  • BenchmarkGoopFNV1 defines a Goop object that contains a single data field (the current hash value) and no methods. Each iteration performs one Get and one Set on the Goop object.

  • BenchmarkMoreGoopFNV1 replaces the hash function with an object method. Hence, each iteration performs one Get, one Set, and one Call on the Goop object.

  • BenchmarkEvenMoreGoopFNV1 adds support for type-dependent dispatch to the hash-function method. Although only one type signature is defined, Goop has to confirm at run time that the provided arguments do in fact match that signature.

Another way to interpret the data shown above is that, on my computer at least, a function closure is essentially free; Get and Set cost approximately 33 ns apiece; a Call of a nullary function costs about 724 ns; and type-dependent dispatch costs an additional 1723 ns.

How does Goop compare to various scripting languages? Not well, at least for BenchmarkMoreGoopFNV1 and its equivalents in other languages. The following table shows the cost in nanoseconds of an individual BenchmarkMoreGoopFNV1 operation (a function call, a read of a data field, a 64-bit multiply, an 8-bit exclusive or, and a write to a data field):

Language Run time (ns/op)
[Incr Tcl] 8.6.0 24490
Go 1.3.1 + Goop 794
JavaScript 1.7 (Rhino 1.7R4-2) 682
Perl 5.18.2 622
Python 2.7.6 604
Python 3.4.0 567
Ruby 1.9.3.4 513
Python 2.7.3 + PyPy 206

In short, you'll want to do most of your coding in native Go and use Goop only when your application requires the extra flexibility that Goop provides. Then, you should cache as many object members as possible in Go variables to reduce the number of Get and Set calls.

Legal statement

Copyright © 2011, Triad National Security, LLC. All rights reserved.

This software was produced under U.S. Government contract 89233218CNA000001 for Los Alamos National Laboratory (LANL), which is operated by Triad National Security, LLC for the U.S. Department of Energy/National Nuclear Security Administration. All rights in the program are reserved by Triad National Security, LLC, and the U.S. Department of Energy/National Nuclear Security Administration. The Government is granted for itself and others acting on its behalf a nonexclusive, paid-up, irrevocable worldwide license in this material to reproduce, prepare derivative works, distribute copies to the public, perform publicly and display publicly, and to permit others to do so. NEITHER THE GOVERNMENT NOR TRIAD NATIONAL SECURITY, LLC MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR ASSUMES ANY LIABILITY FOR THE USE OF THIS SOFTWARE. If software is modified to produce derivative works, such modified software should be clearly marked, so as not to confuse it with the version available from LANL.

Goop is provided under a BSD 3-clause license. See the LICENSE file for the full text.

Triad National Security, LLC (Triad) owns the copyright to Goop, a component of the LANL Go Suite (identified internally as LA-CC-11-056).

Author

Scott Pakin, pakin@lanl.gov

About

Dynamic object-oriented programming support for the Go language

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages