diff --git a/common/os.go b/common/os.go index 7a5ec0fa2..437b8de34 100644 --- a/common/os.go +++ b/common/os.go @@ -43,12 +43,14 @@ func AtomicWriteFile(filePath string, newBytes []byte) error { return fmt.Errorf("Failed to write file %v. %v", filePath+".bak", err) } } - // Write newBytes to filePath. - err := ioutil.WriteFile(filePath, newBytes, 0600) + // Write newBytes to filePath.new + err := ioutil.WriteFile(filePath+".new", newBytes, 0600) if err != nil { - return fmt.Errorf("Failed to write file %v. %v", filePath, err) + return fmt.Errorf("Failed to write file %v. %v", filePath+".new", err) } - return nil + // Move filePath.new to filePath + err = os.Rename(filePath+".new", filePath) + return err } func EnsureDir(dir string) error { diff --git a/merkle/iavl_test.go b/merkle/iavl_test.go index 00002fa7f..e6926c9a3 100644 --- a/merkle/iavl_test.go +++ b/merkle/iavl_test.go @@ -241,6 +241,8 @@ func BenchmarkImmutableAvlTree(b *testing.B) { b.StopTimer() t := NewIAVLTree(binary.BasicCodec, binary.BasicCodec, 0, nil) + // 23000ns/op, 43000ops/s + // for i := 0; i < 10000000; i++ { for i := 0; i < 1000000; i++ { t.Set(RandUint64(), "") }