Skip to content

Instantly share code, notes, and snippets.

@methane
Created February 2, 2016 18:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save methane/59c8777907b10dc983a1 to your computer and use it in GitHub Desktop.
Save methane/59c8777907b10dc983a1 to your computer and use it in GitHub Desktop.
diff --git a/tsdb/engine/tsm1/cache_test.go b/tsdb/engine/tsm1/cache_test.go
index 2151270..d74b7a9 100644
--- a/tsdb/engine/tsm1/cache_test.go
+++ b/tsdb/engine/tsm1/cache_test.go
@@ -359,3 +359,13 @@ func mustMarshalEntry(entry WALEntry) (WalEntryType, []byte) {
return entry.Type(), snappy.Encode(b, b)
}
+
+func BenchmarkCacheFloatEntries(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ cache := NewCache(10000)
+ for j := 0; j < 10000; j++ {
+ v := NewValue(time.Unix(1, 0), float64(j))
+ cache.Write("test", []Value{v})
+ }
+ }
+}
diff --git a/tsdb/engine/tsm1/encoding.go b/tsdb/engine/tsm1/encoding.go
index e5bb295..9abbf00 100644
--- a/tsdb/engine/tsm1/encoding.go
+++ b/tsdb/engine/tsm1/encoding.go
@@ -41,7 +41,7 @@ func NewValue(t time.Time, value interface{}) Value {
case int64:
return &Int64Value{time: t, value: v}
case float64:
- return &FloatValue{time: t, value: v}
+ return &FloatValue{unixnano: t.UnixNano(), value: v}
case bool:
return &BoolValue{time: t, value: v}
case string:
@@ -222,16 +222,16 @@ func (a Values) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a Values) Less(i, j int) bool { return a[i].Time().UnixNano() < a[j].Time().UnixNano() }
type FloatValue struct {
- time time.Time
- value float64
+ unixnano int64
+ value float64
}
func (f *FloatValue) Time() time.Time {
- return f.time
+ return time.Unix(0, f.unixnano)
}
func (f *FloatValue) UnixNano() int64 {
- return f.time.UnixNano()
+ return f.unixnano
}
func (f *FloatValue) Value() interface{} {
@@ -308,10 +308,10 @@ func DecodeFloatBlock(block []byte, a []FloatValue) ([]FloatValue, error) {
ts := dec.Read()
v := iter.Values()
if i < len(a) {
- a[i].time = ts
+ a[i].unixnano = ts.UnixNano()
a[i].value = v
} else {
- a = append(a, FloatValue{ts, v})
+ a = append(a, FloatValue{ts.UnixNano(), v})
}
i++
}
diff --git a/tsdb/engine/tsm1/wal.go b/tsdb/engine/tsm1/wal.go
index ff09351..4566453 100644
--- a/tsdb/engine/tsm1/wal.go
+++ b/tsdb/engine/tsm1/wal.go
@@ -462,7 +462,8 @@ func (w *WriteWALEntry) UnmarshalBinary(b []byte) error {
}
for j := 0; j < nvals; j++ {
- t := time.Unix(0, int64(btou64(b[i:i+8])))
+ un := int64(btou64(b[i : i+8]))
+ t := time.Unix(0, un)
i += 8
switch typ {
@@ -470,7 +471,7 @@ func (w *WriteWALEntry) UnmarshalBinary(b []byte) error {
v := math.Float64frombits((btou64(b[i : i+8])))
i += 8
if fv, ok := values[j].(*FloatValue); ok {
- fv.time = t
+ fv.unixnano = un
fv.value = v
}
case int64EntryType:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment