Random access file: keep os.File in memory or allocate one for every read (with buffer of course)

871 views
Skip to first unread message

周益琰

unread,
Jun 14, 2013, 8:28:04 AM6/14/13
to golan...@googlegroups.com
Dears,

I've been rewritten my Java project to Go and I met a question lately. In Java, RandomAccessFile is used to do random read, and it's safe to keep RandomAccessFile instance in memory, with proper close() when it's done. For Go version, is it a good practise to keep os.File in memory, or should I allocate one for every read (with buffer enabled of course)?

It's a performance question and I can benchmark myself later...but I'm wondering if anyone has some quick suggestion first.

Thanks,

-- Ian

Ian Lance Taylor

unread,
Jun 14, 2013, 9:23:51 AM6/14/13
to 周益琰, golan...@googlegroups.com
Normally you would use a single os.File to read from a file, and use
the ReadAt method to read data from random locations.

Ian

周益琰

unread,
Jun 16, 2013, 3:20:56 AM6/16/13
to golan...@googlegroups.com, 周益琰
It makes sense to me.

One more question, should I prefer ReadAt to Seek/Read if I don't keep real location? Or, does underlying infrastructure pre-buffer or benefit from persisted position by using seek method?

在 2013年6月14日星期五UTC+8下午9时23分51秒,Ian Lance Taylor写道:

Ian Lance Taylor

unread,
Jun 16, 2013, 6:47:24 PM6/16/13
to 周益琰, golan...@googlegroups.com
On Sun, Jun 16, 2013 at 12:20 AM, 周益琰 <balz...@gmail.com> wrote:
>
> One more question, should I prefer ReadAt to Seek/Read if I don't keep real
> location? Or, does underlying infrastructure pre-buffer or benefit from
> persisted position by using seek method?

In general a single ReadAt call is going to be better than separate
Seek and Read calls. It doesn't make a big difference, but if you
have an easy choice in your program you should prefer ReadAt.

Ian

voidl...@gmail.com

unread,
Jun 16, 2013, 11:15:06 PM6/16/13
to golan...@googlegroups.com, 周益琰
Also if you have multiple goroutines reading the same os.File you definatly need to use ReadAt. If you do this you probably want to use a RW mutex to guard the os.File.
Reply all
Reply to author
Forward
0 new messages