Skip to content

add AppendFrom() method - #178

Merged
deckarep merged 2 commits into
deckarep:mainfrom
mai7star:feature/import_method
Apr 28, 2026
Merged

add AppendFrom() method#178
deckarep merged 2 commits into
deckarep:mainfrom
mai7star:feature/import_method

Conversation

@mai7star

@mai7star mai7star commented Apr 27, 2026

Copy link
Copy Markdown
Contributor

Add AppendFrom() method. (shorthand of s.Append(other.ToSlice()...))

Benchmark

BenchmarkImport1Safe/Import-10                         	32167987	        36.09 ns/op	       0 B/op	       0 allocs/op
BenchmarkImport1Safe/ToSlice_+_Append-10               	27716212	        43.31 ns/op	       8 B/op	       1 allocs/op
BenchmarkImport1Unsafe/Import-10                       	42027781	        28.57 ns/op	       0 B/op	       0 allocs/op
BenchmarkImport1Unsafe/ToSlice_+_Append-10             	32386915	        37.08 ns/op	       8 B/op	       1 allocs/op

BenchmarkImport10Safe/Import-10                        	 9372235	       120.9 ns/op	       0 B/op	       0 allocs/op
BenchmarkImport10Safe/ToSlice_+_Append-10              	 8664128	       138.6 ns/op	      80 B/op	       1 allocs/op
BenchmarkImport10Unsafe/Import-10                      	10913013	       109.0 ns/op	       0 B/op	       0 allocs/op
BenchmarkImport10Unsafe/ToSlice_+_Append-10            	 9503256	       127.1 ns/op	      80 B/op	       1 allocs/op

BenchmarkImport100Safe/Import-10                       	 1349128	       925.3 ns/op	       0 B/op	       0 allocs/op
BenchmarkImport100Safe/ToSlice_+_Append-10             	 1000000	      1035 ns/op	     896 B/op	       1 allocs/op
BenchmarkImport100Unsafe/Import-10                     	 1393164	       887.7 ns/op	       0 B/op	       0 allocs/op
BenchmarkImport100Unsafe/ToSlice_+_Append-10           	 1000000	      1006 ns/op	     896 B/op	       1 allocs/op

Code

func benchImport(b *testing.B, n int, s, t Set[int]) {
	s.Append(nrand(n)...)
	t.Append(nrand(n)...)

	b.Run("Import", func(b *testing.B) {
		b.ReportAllocs()
		for i := 0; i < b.N; i++ {
			_ = s.Import(t)
		}
	})
	b.Run("ToSlice + Append", func(b *testing.B) {
		b.ReportAllocs()
		for i := 0; i < b.N; i++ {
			_ = s.Append(t.ToSlice()...)
		}
	})
}

func BenchmarkImport1Safe(b *testing.B) {
	benchImport(b, 1, NewSet[int](), NewSet[int]())
}
(..snip..)

@deckarep

Copy link
Copy Markdown
Owner

Thanks for the PR, I'm just thinking about the possibility of a different method name since Import has specific meaning for Go.

Perhaps BulkAdd, AddAll, ?? I'm still thinking about it.

@mai7star

Copy link
Copy Markdown
Contributor Author

@deckarep How about AppendFrom() ?

@deckarep

Copy link
Copy Markdown
Owner

What's better AppendFrom or AppendAll? Naming is hard. :)

@mai7star

Copy link
Copy Markdown
Contributor Author

I think AppendFrom() is better.
I can't see any difference between Append() and AppendAll().

@deckarep

Copy link
Copy Markdown
Owner

AppendFrom it is, once you make the change, I'll merge.

@mai7star
mai7star force-pushed the feature/import_method branch from 2721217 to 8755084 Compare April 28, 2026 09:25
@mai7star mai7star changed the title add Import() method add AppendFrom() method Apr 28, 2026
@deckarep
deckarep merged commit 6621cc4 into deckarep:main Apr 28, 2026
9 checks passed
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.

2 participants