Skip to content

Pre-allocate slice in generic Map function#469

Merged
mna merged 1 commit intoPuerkitoBio:masterfrom
jwilsson:pre-allocate-slice-in-map
Feb 29, 2024
Merged

Pre-allocate slice in generic Map function#469
mna merged 1 commit intoPuerkitoBio:masterfrom
jwilsson:pre-allocate-slice-in-map

Conversation

@jwilsson
Copy link
Copy Markdown
Contributor

Hey!
Thanks for this great library, really helped me out with some scraping!

I noticed that the generic Map function just added the other week always appends, growing the slice as needed instead of pre-allocating it upfront.

Benchmark results from my M1:

Without pre-allocation:
BenchmarkMap-10                              	  238780	      5140 ns/op

With pre-allocation:
BenchmarkMap-10                              	  277968	      4317 ns/op

If you think it's not worth it, no hard feelings. Just thought it would be a fun, easy, optimization to make.

@mna
Copy link
Copy Markdown
Member

mna commented Feb 28, 2024

Hey Jonathan,

Thanks for this! That behaviour actually predates the new Map addition as this is also how I had implemented the Selection.Map method. That's definitely worth it, I'll get that merged soon.

Martin

@mna mna merged commit 2095230 into PuerkitoBio:master Feb 29, 2024
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