Skip to content

Commit

Permalink
Optimize incrementing values in DirectFileStore adapter
Browse files Browse the repository at this point in the history
* Introduce FileMappedDict#increment_value
* Check for internal_storage only once (Process.pid is slow on Linux)
* Lookup file position only once
  • Loading branch information
splattael committed Feb 28, 2023
1 parent db756a1 commit 4c89123
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions lib/prometheus/client/data_stores/direct_file_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@ def increment(labels:, by: 1)

key = store_key(labels)
in_process_sync do
value = internal_store.read_value(key)
internal_store.write_value(key, value + by.to_f)
internal_store.increment_value(key, by.to_f)
end
end

Expand Down Expand Up @@ -286,6 +285,21 @@ def write_value(key, value)
@f.flush
end

def increment_value(key, by)
if !@positions.has_key?(key)
init_value(key)
end

pos = @positions[key]
@f.seek(pos)
value = @f.read(8).unpack('d')[0]

now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
@f.seek(-8, :CUR)
@f.write([value + by, now].pack('dd'))
@f.flush
end

def close
@f.close
end
Expand Down

0 comments on commit 4c89123

Please sign in to comment.