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

Added TriangleData benchmarks and improvements #183

Merged
merged 3 commits into from
Apr 25, 2019

Conversation

bcvery1
Copy link
Contributor

@bcvery1 bcvery1 commented Apr 24, 2019

Raising as draft as I haven't had time to fully test this hasn't broken things. I'll mark as ready when I've checked some examples, etc

Starting to address #46 - thought it'd be too big for one go. This PR adds benchmarks for all exported methods of pixel.TriangleData, I've gone for the "low hanging fruit" and trimmed off a decent amount of heap allocation, and sped some of the more heavily used functions.

These are the benchmarks on the code unaltered:

goarch: amd64
pkg: github.com/faiface/pixel
BenchmarkMakeTrianglesData/Small_slice-24         	 1000000	      1518 ns/op	    2240 B/op	       5 allocs/op
BenchmarkMakeTrianglesData/Large_slice-24         	    1000	   1779970 ns/op	 4086468 B/op	      21 allocs/op
BenchmarkTrianglesData_Len/Small_slice-24         	2000000000	         0.74 ns/op	       0 B/op	       0 allocs/op
BenchmarkTrianglesData_Len/Large_slice-24         	2000000000	         0.78 ns/op	       0 B/op	       0 allocs/op
BenchmarkTrianglesData_SetLen/Stay_same_size-24   	200000000	         7.53 ns/op	       0 B/op	       0 allocs/op
BenchmarkTrianglesData_SetLen/Change_size-24      	100000000	        11.2 ns/op	       0 B/op	       0 allocs/op
BenchmarkTrianglesData_Slice/Basic_slice-24       	2000000000	         0.80 ns/op	       0 B/op	       0 allocs/op
BenchmarkTrianglesData_Update/Small_Triangles-24  	20000000	        59.4 ns/op	       0 B/op	       0 allocs/op
BenchmarkTrianglesData_Update/Large_Triangles-24  	   30000	     57695 ns/op	       0 B/op	       0 allocs/op
BenchmarkTrianglesData_Copy/Small_copy-24         	 1000000	      3478 ns/op	    4576 B/op	       7 allocs/op
BenchmarkTrianglesData_Copy/Large_copy-24         	    1000	   1821426 ns/op	 4086500 B/op	      22 allocs/op
BenchmarkTrianglesData_Position/Getting_beginning_position-24         	2000000000	         0.81 ns/op	       0 B/op	       0 allocs/op
BenchmarkTrianglesData_Position/Getting_middle_position-24            	2000000000	         0.78 ns/op	       0 B/op	       0 allocs/op
BenchmarkTrianglesData_Position/Getting_end_position-24               	2000000000	         0.81 ns/op	       0 B/op	       0 allocs/op
BenchmarkTrianglesData_Color/Getting_beginning_position-24            	2000000000	         0.79 ns/op	       0 B/op	       0 allocs/op
BenchmarkTrianglesData_Color/Getting_middle_position-24               	2000000000	         0.81 ns/op	       0 B/op	       0 allocs/op
BenchmarkTrianglesData_Color/Getting_end_position-24                  	2000000000	         0.75 ns/op	       0 B/op	       0 allocs/op
BenchmarkTrianglesData_Picture/Getting_beginning_position-24          	2000000000	         0.78 ns/op	       0 B/op	       0 allocs/op
BenchmarkTrianglesData_Picture/Getting_middle_position-24             	2000000000	         0.77 ns/op	       0 B/op	       0 allocs/op
BenchmarkTrianglesData_Picture/Getting_end_position-24                	2000000000	         0.76 ns/op	       0 B/op	       0 allocs/op
PASS

Benchmarks after these PR changes:

goarch: amd64
pkg: github.com/faiface/pixel
BenchmarkMakeTrianglesData/Small_slice-24         	 3000000	       498 ns/op	     800 B/op	       2 allocs/op
BenchmarkMakeTrianglesData/Large_slice-24         	    5000	    286247 ns/op	  720928 B/op	       2 allocs/op
BenchmarkTrianglesData_Len/Small_slice-24         	2000000000	         0.77 ns/op	       0 B/op	       0 allocs/op
BenchmarkTrianglesData_Len/Large_slice-24         	2000000000	         0.74 ns/op	       0 B/op	       0 allocs/op
BenchmarkTrianglesData_SetLen/Stay_same_size-24   	200000000	         7.15 ns/op	       0 B/op	       0 allocs/op
BenchmarkTrianglesData_SetLen/Change_size-24      	 2000000	       989 ns/op	    1954 B/op	       0 allocs/op
BenchmarkTrianglesData_Slice/Basic_slice-24       	2000000000	         0.79 ns/op	       0 B/op	       0 allocs/op
BenchmarkTrianglesData_Update/Small_Triangles-24  	20000000	        52.9 ns/op	       0 B/op	       0 allocs/op
BenchmarkTrianglesData_Update/Large_Triangles-24  	   30000	     50046 ns/op	       0 B/op	       0 allocs/op
BenchmarkTrianglesData_Copy/Small_copy-24         	 1000000	      1257 ns/op	    1568 B/op	       2 allocs/op
BenchmarkTrianglesData_Copy/Large_copy-24         	    3000	    400064 ns/op	  720929 B/op	       2 allocs/op
BenchmarkTrianglesData_Position/Getting_beginning_position-24         	2000000000	         0.76 ns/op	       0 B/op	       0 allocs/op
BenchmarkTrianglesData_Position/Getting_middle_position-24            	2000000000	         0.71 ns/op	       0 B/op	       0 allocs/op
BenchmarkTrianglesData_Position/Getting_end_position-24               	2000000000	         0.68 ns/op	       0 B/op	       0 allocs/op
BenchmarkTrianglesData_Color/Getting_beginning_position-24            	2000000000	         0.68 ns/op	       0 B/op	       0 allocs/op
BenchmarkTrianglesData_Color/Getting_middle_position-24               	2000000000	         0.68 ns/op	       0 B/op	       0 allocs/op
BenchmarkTrianglesData_Color/Getting_end_position-24                  	2000000000	         0.68 ns/op	       0 B/op	       0 allocs/op
BenchmarkTrianglesData_Picture/Getting_beginning_position-24          	2000000000	         0.68 ns/op	       0 B/op	       0 allocs/op
BenchmarkTrianglesData_Picture/Getting_middle_position-24             	2000000000	         0.69 ns/op	       0 B/op	       0 allocs/op
BenchmarkTrianglesData_Picture/Getting_end_position-24                	2000000000	         0.76 ns/op	       0 B/op	       0 allocs/op
PASS

Improvements to note:
MakeTrianglesData() for slice of 10,000 reduced op-time by ~1.5ms (~16% original time)
TriangleData.Update() for slice of 10,000 reduced op-time by ~7000ns (~86% original time)
TriangleData.Copy() for slice of 20 reduced op time by >2000ns (~36% original time).
TriangleData.Copy() for slice of 10,000 reduced op-time by ~1.4ms (~21% original time)
TriangleData.Copy() for slice of 10,000 reduced allocations per op by 20 (~9% original allocs per op)

This does not resolve the issue, and does not attempt to implement the suggested new interface. Until there are benchmarks, we won't know whether speed is improving; this PR is intended as a stepping stone.

@bcvery1 bcvery1 marked this pull request as ready for review April 24, 2019 15:34
@faiface
Copy link
Owner

faiface commented Apr 25, 2019

Wow, I wouldn't have guessed it made such an impact! Very good job @bcvery1 ! And you're right, this is a very important stepping stone on the path to solve #46! Thanks a lot!

@faiface faiface merged commit a68a4e3 into faiface:master Apr 25, 2019
@bcvery1 bcvery1 deleted the TriangleEfficiency branch April 25, 2019 12:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants