javascript - OS.File check last modified date before OS.read -
i use os.file.read read files contents. on focus of window os.file.stat , check it's last modified date. record date. on next focus of window os.file.stat file again, , if modified date new, os.file.read on it.
i wondering there more efficient way? example: start os.file.read, , default gets file header should include date last modified, check reading , if not new date cancel read. more efficient coding perspective, performance perspective maybe not much?
short answer: depends?!
what more efficient depend on file , usage pattern , system workload.
normally, os.file.stat , selectively read more efficient, because file i/o bottleneck , you'd avoid i/o (most of time).
for small files read write occasionally, might more effective read file. file meta-data , data in os disk cache file i/o becomes fast, , actual inter-thread messaging overhead , js-ctypes overhead of os.file become bottlenecks. particular case, however.
also, if file changes extremely often, every os.stat + check result in having read file again, might more efficient read file. in case ponder if choice use file right way go, or if mode of communication wouldn't better (such sockets).
conclusion
so, i'd go .stat/check/.read, because making assumptions on disk cache , system work load might bad.
can disk i/o avoided?
however, i'd avoid polling file in first place, if possible. if file specific add-on not expect other processes write to, read once , after keep data in memory in shared place, such js code module or main.js of sdk add-ons or bootstrap.js of bootstrapped add-ons. upon writes, update cached data well.
if you're concerned other process might write file in mean time, open file exclusive lock, , not close again while add-on running.
let options = { winshare: 0 // exclusive lock on windows }; if (os.constants.libc.o_exlock) { // exclusive lock on *nix options.unixflags = os.constants.libc.o_exlock; } let file = yield os.file.open(..., options); if expect other programs write file , fine that, or if use file way of inter-process-communication in first place, cannot avoid re-reading file, of course.
Comments
Post a Comment