Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unnecessary allocations in HopData encode/decode methods #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Commits on Apr 26, 2018

  1. Remove unnecessary allocations in HopData encode/decode methods

    By removing io.Writer and io.Reader abstraction from encode and decode
    methods, the following benchmars:
    
    old:
    
    func BenchmarkHopDataEncode(b *testing.B) {
    	hd := new(HopData)
    	for i := 0; i < b.N; i++ {
    		hd.Encode(ioutil.Discard)
    	}
    }
    
    type nopReader struct{}
    
    func (nopReader) Read(b []byte) (int, error) { return len(b), nil }
    
    func BenchmarkHopDataDecode(b *testing.B) {
    	hd := new(HopData)
    	src := nopReader{}
    	for i := 0; i < b.N; i++ {
    		hd.Decode(src)
    	}
    }
    
    new:
    
    func BenchmarkHopDataEncode(b *testing.B) {
    	hd := new(HopData)
    	dst := make([]byte, hopDataSize)
    	for i := 0; i < b.N; i++ {
    		hd.Encode(dst)
    	}
    }
    
    func BenchmarkHopDataDecode(b *testing.B) {
    	hd := new(HopData)
    	src := make([]byte, hopDataSize)
    	for i := 0; i < b.N; i++ {
    		hd.Decode(src)
    	}
    }
    
    show the following insrease in performance:
    
    benchmark                    old ns/op     new ns/op     delta
    BenchmarkHopDataEncode-4     80.7          12.1          -85.01%
    
    benchmark                    old allocs     new allocs     delta
    BenchmarkHopDataEncode-4     3              0              -100.00%
    
    benchmark                    old bytes     new bytes     delta
    BenchmarkHopDataEncode-4     24            0             -100.00%
    
    benchmark                    old ns/op     new ns/op     delta
    BenchmarkHopDataDecode-4     218           9.77          -95.52%
    
    benchmark                    old allocs     new allocs     delta
    BenchmarkHopDataDecode-4     4              0              -100.00%
    
    benchmark                    old bytes     new bytes     delta
    BenchmarkHopDataDecode-4     56            0             -100.00%
    mrekucci committed Apr 26, 2018
    Configuration menu
    Copy the full SHA
    b1eb2d0 View commit details
    Browse the repository at this point in the history