I think bufio.ReadSlice() and bufio.ReadLine() should return the data's length

71 views
Skip to first unread message

guot...@gmail.com

unread,
Jul 18, 2013, 10:32:10 PM7/18/13
to golan...@googlegroups.com
I think bufio.ReadSlice() and bufio.ReadLine() should return the data's length. 

In many case, when someone call bufio.ReadSlice(), if he want the data's length, he must call len(data), if bufio.ReadSlice() return with data's length, it avoid recalculate, and performance is higher too.


Brad Fitzpatrick

unread,
Jul 19, 2013, 2:00:43 AM7/19/13
to guot...@gmail.com, golang-dev

A slice contains a length.

len(s) is inlined and is as cheap as a variable.

Your proposal would actually make it slower.

On Jul 19, 2013 3:56 PM, <guot...@gmail.com> wrote:
I think bufio.ReadSlice() and bufio.ReadLine() should return the data's length. 

In many case, when someone call bufio.ReadSlice(), if he want the data's length, he must call len(data), if bufio.ReadSlice() return with data's length, it avoid recalculate, and performance is higher too.


--
 
---
You received this message because you are subscribed to the Google Groups "golang-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-dev+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Rob Pike

unread,
Jul 19, 2013, 2:03:53 AM7/19/13
to guot...@gmail.com, golan...@googlegroups.com
The calculation of len(data) takes about a nanosecond, literally. You probably can't even measure it's cost in real code.

-rob

Jan Mercl

unread,
Jul 19, 2013, 2:04:36 AM7/19/13
to guot...@gmail.com, golang-dev
On some CPUs (I think Intel is the case), getting len(ofSlice) into a
CPU register could is probably done in one instruction.

-j

Rémy Oudompheng

unread,
Jul 19, 2013, 2:40:24 AM7/19/13
to Jan Mercl, guot...@gmail.com, golang-dev
With some compilers it can probably be done in zero instruction.

Rémy.
Reply all
Reply to author
Forward
0 new messages