Skip to content

Commit 11d0147

Browse files
author
Yuan Ting
committed
Fix bugs
1 parent a0f5977 commit 11d0147

File tree

9 files changed

+172
-2
lines changed

9 files changed

+172
-2
lines changed

.idea/.gitignore

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/go-gitdiff.iml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

changes.patch

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
From e1ffca3629c16a5a19eebe8f7805176ce69cbf97 Mon Sep 17 00:00:00 2001
2+
From: Alexander Morozov <lk4d4@docker.com>
3+
Date: Thu, 28 Jan 2016 09:51:40 -0800
4+
Subject: [PATCH] Use sync.Pool for small pages in db.allocate
5+
6+
Benchmark results:
7+
benchmark old ns/op new ns/op delta
8+
BenchmarkDBBatchAutomatic-4 2552625 2485200 -2.64%
9+
BenchmarkDBBatchSingle-4 59632698 50757603 -14.88%
10+
BenchmarkDBBatchManual10x100-4 2564789 2452735 -4.37%
11+
12+
benchmark old allocs new allocs delta
13+
BenchmarkDBBatchAutomatic-4 10199 10202 +0.03%
14+
BenchmarkDBBatchSingle-4 56642 56653 +0.02%
15+
BenchmarkDBBatchManual10x100-4 5986 5995 +0.15%
16+
17+
benchmark old bytes new bytes delta
18+
BenchmarkDBBatchAutomatic-4 433587 382462 -11.79%
19+
BenchmarkDBBatchSingle-4 32504533 16308931 -49.83%
20+
BenchmarkDBBatchManual10x100-4 1362370 881765 -35.28%
21+
22+
Signed-off-by: Alexander Morozov <lk4d4@docker.com>
23+
---
24+
db.go | 18 ++++++++++++++++--
25+
tx.go | 16 ++++++++++++++--
26+
2 files changed, 30 insertions(+), 4 deletions(-)
27+
28+
diff --git a/db.go b/db.go
29+
index 0f1e1bc3..5d487092 100644
30+
--- a/db.go
31+
+++ b/db.go
32+
@@ -36,6 +36,9 @@ const (
33+
DefaultAllocSize = 16 * 1024 * 1024
34+
)
35+
36+
+// default page size for db is set to the OS page size.
37+
+var defaultPageSize = os.Getpagesize()
38+
+
39+
// DB represents a collection of buckets persisted to a file on disk.
40+
// All data access is performed through transactions which can be obtained through the DB.
41+
// All the functions on DB will return a ErrDatabaseNotOpen if accessed before Open() is called.
42+
@@ -320,7 +323,7 @@ func (db *DB) mmapSize(size int) (int, error) {
43+
// init creates a new database file and initializes its meta pages.
44+
func (db *DB) init() error {
45+
// Set the page size to the OS page size.
46+
- db.pageSize = os.Getpagesize()
47+
+ db.pageSize = defaultPageSize
48+
49+
// Create two meta pages on a buffer.
50+
buf := make([]byte, db.pageSize*4)
51+
@@ -779,10 +782,21 @@ func (db *DB) meta() *meta {
52+
return db.meta1
53+
}
54+
55+
+var pagePool = sync.Pool{
56+
+ New: func() interface{} {
57+
+ return make([]byte, defaultPageSize)
58+
+ },
59+
+}
60+
+
61+
// allocate returns a contiguous block of memory starting at a given page.
62+
func (db *DB) allocate(count int) (*page, error) {
63+
// Allocate a temporary buffer for the page.
64+
- buf := make([]byte, count*db.pageSize)
65+
+ var buf []byte
66+
+ if count == 1 && db.pageSize == defaultPageSize {
67+
+ buf = pagePool.Get().([]byte)
68+
+ } else {
69+
+ buf = make([]byte, count*db.pageSize)
70+
+ }
71+
p := (*page)(unsafe.Pointer(&buf[0]))
72+
p.overflow = uint32(count - 1)
73+
74+
diff --git a/tx.go b/tx.go
75+
index e74d2cae..09a39fb0 100644
76+
--- a/tx.go
77+
+++ b/tx.go
78+
@@ -441,6 +441,8 @@ func (tx *Tx) write() error {
79+
for _, p := range tx.pages {
80+
pages = append(pages, p)
81+
}
82+
+ // Clear out page cache early.
83+
+ tx.pages = make(map[pgid]*page)
84+
sort.Sort(pages)
85+
86+
// Write pages to disk in order.
87+
@@ -485,8 +487,18 @@ func (tx *Tx) write() error {
88+
}
89+
}
90+
91+
- // Clear out page cache.
92+
- tx.pages = make(map[pgid]*page)
93+
+ // put small pages back to sync.Pool
94+
+ for _, p := range pages {
95+
+ if int(p.overflow) != 0 || tx.db.pageSize != defaultPageSize {
96+
+ continue
97+
+ }
98+
+ buf := (*[maxAllocSize]byte)(unsafe.Pointer(p))[:defaultPageSize]
99+
+ // See https://go.googlesource.com/go/+/f03c9202c43e0abb130669852082117ca50aa9b1
100+
+ for i := range buf {
101+
+ buf[i] = 0
102+
+ }
103+
+ pagePool.Put(buf)
104+
+ }
105+
106+
return nil
107+
}

gitdiff/gitdiff.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,14 @@ func (f *TextFragment) FuncNames() []string {
9090
// func (this *Object) NAME(arg1, arg2, ...)
9191
low := strings.Index(def, ")") + 2
9292
if low >= len(def) {
93-
log.Printf("Can't handle '%s'" def)
93+
log.Printf("Can't handle '%s'", def)
9494
return
9595
}
9696
hig := strings.Index(def[low:], "(") + low
9797
if low < hig && low >= 0 && hig < len(def) {
9898
funcnames = append(funcnames, def[low:hig])
9999
} else {
100-
log.Printf("Can't handle '%s'" def)
100+
log.Printf("Can't handle '%s'", def)
101101
}
102102
}
103103
addname(f.Comment)

gitdiff/parser.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ func (p *Patches) GetCommit(file *File) (Commit, bool) {
6060
return cmt, ok
6161
}
6262

63+
6364
func ParsePatch(r io.Reader) ([]*File, *Patches, error) {
6465
p := newParser(r)
6566
patches := &Patches{

main.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/timmyyuan/go-gitdiff/gitdiff"
6+
"log"
7+
"os"
8+
)
9+
10+
func main() {
11+
patch, err := os.Open("changes.patch")
12+
if err != nil {
13+
log.Fatal(err)
14+
}
15+
16+
_, ps, err := gitdiff.ParsePatch(patch)
17+
if err != nil {
18+
log.Fatal(err)
19+
}
20+
for k, v := range ps.ShaToFiles {
21+
log.Printf("%s : \n", k)
22+
for _, f := range v {
23+
for _, frag := range f.TextFragments {
24+
funcnames := frag.FuncNames()
25+
for _, name := range funcnames {
26+
fmt.Printf("%s, ", name)
27+
}
28+
}
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)