Skip to content

Commit 66c1081

Browse files
author
Todd Wang
committed
lib: Update v.io/x/lib/timing to a single implementation
The initial implementation of the timing package had two implementations: a FullTimer with full timing info, and a CompactTimer that is smaller and faster, but doesn't have full timing info. This is annoying for the user since they have to make a choice, and adds complexity, and it bugged me. This change consolidates things down to a single implementation, and gets rid of the interfaces. This is simpler to use for the user. The main point of CompactTimer was to avoid multiple small allocations in the interval tree; the new Timer does the same thing, while retaining full timing info. As a bonus, the printing logic is faster, since we simply loop through the intervals in the slice. The printing logic for CompactTimer was quite inefficient since it caused new allocations for each list of children in compactInterval. MultiPart: 2/2 Change-Id: I2fea725ccb9cd3ec0107d2d33bf1d8c32b07b452
1 parent c29a7a7 commit 66c1081

File tree

8 files changed

+691
-715
lines changed

8 files changed

+691
-715
lines changed

cmdline/.api

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pkg cmdline, type Env struct
2525
pkg cmdline, type Env struct, Stderr io.Writer
2626
pkg cmdline, type Env struct, Stdin io.Reader
2727
pkg cmdline, type Env struct, Stdout io.Writer
28-
pkg cmdline, type Env struct, Timer timing.Timer
28+
pkg cmdline, type Env struct, Timer *timing.Timer
2929
pkg cmdline, type Env struct, Usage func(*Env, io.Writer)
3030
pkg cmdline, type Env struct, Vars map[string]string
3131
pkg cmdline, type ErrExitCode int

cmdline/cmdline.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,13 @@ func Main(root *Command) {
125125
code := ExitCode(err, env.Stderr)
126126
if *flagTime && env.Timer != nil {
127127
env.Timer.Finish()
128-
timing.IntervalPrinter{}.Print(env.Stderr, env.Timer.Root())
128+
p := timing.IntervalPrinter{Zero: env.Timer.Zero}
129+
if err := p.Print(env.Stderr, env.Timer.Intervals, env.Timer.Now()); err != nil {
130+
code2 := ExitCode(err, env.Stderr)
131+
if code == 0 {
132+
code = code2
133+
}
134+
}
129135
}
130136
os.Exit(code)
131137
}

cmdline/env.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func EnvFromOS() *Env {
2323
Stdout: os.Stdout,
2424
Stderr: os.Stderr,
2525
Vars: envvar.SliceToMap(os.Environ()),
26-
Timer: timing.NewFullTimer("root"),
26+
Timer: timing.NewTimer("root"),
2727
}
2828
}
2929

@@ -35,7 +35,7 @@ type Env struct {
3535
Stdout io.Writer
3636
Stderr io.Writer
3737
Vars map[string]string // Environment variables
38-
Timer timing.Timer
38+
Timer *timing.Timer
3939

4040
// Usage is a function that prints usage information to w. Typically set by
4141
// calls to Main or Parse to print usage of the leaf command.

timing/.api

+17-41
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,21 @@
1-
pkg timing, func IntervalDuration(Interval, time.Time) time.Duration
2-
pkg timing, func NewCompactTimer(string) *CompactTimer
3-
pkg timing, func NewFullTimer(string) *FullTimer
4-
pkg timing, method (*CompactTimer) Finish()
5-
pkg timing, method (*CompactTimer) Pop()
6-
pkg timing, method (*CompactTimer) Push(string)
7-
pkg timing, method (*CompactTimer) Root() Interval
8-
pkg timing, method (*CompactTimer) String() string
9-
pkg timing, method (*FullTimer) Finish()
10-
pkg timing, method (*FullTimer) Pop()
11-
pkg timing, method (*FullTimer) Push(string)
12-
pkg timing, method (*FullTimer) Root() Interval
13-
pkg timing, method (*FullTimer) String() string
14-
pkg timing, method (FullInterval) Child(int) Interval
15-
pkg timing, method (FullInterval) End() time.Time
16-
pkg timing, method (FullInterval) Name() string
17-
pkg timing, method (FullInterval) NumChild() int
18-
pkg timing, method (FullInterval) Start() time.Time
19-
pkg timing, method (FullInterval) String() string
20-
pkg timing, method (IntervalPrinter) Print(io.Writer, Interval) error
21-
pkg timing, type CompactTimer struct
22-
pkg timing, type FullInterval struct
23-
pkg timing, type FullInterval struct, Children []FullInterval
24-
pkg timing, type FullInterval struct, EndTime time.Time
25-
pkg timing, type FullInterval struct, Label string
26-
pkg timing, type FullInterval struct, StartTime time.Time
27-
pkg timing, type FullTimer struct
28-
pkg timing, type FullTimer struct, FullRoot FullInterval
29-
pkg timing, type Interval interface { Child, End, Name, NumChild, Start, String }
30-
pkg timing, type Interval interface, Child(int) Interval
31-
pkg timing, type Interval interface, End() time.Time
32-
pkg timing, type Interval interface, Name() string
33-
pkg timing, type Interval interface, NumChild() int
34-
pkg timing, type Interval interface, Start() time.Time
35-
pkg timing, type Interval interface, String() string
1+
pkg timing, const InvalidDuration time.Duration
2+
pkg timing, func NewTimer(string) *Timer
3+
pkg timing, method (*Timer) Finish()
4+
pkg timing, method (*Timer) Now() time.Duration
5+
pkg timing, method (*Timer) Pop()
6+
pkg timing, method (*Timer) Push(string)
7+
pkg timing, method (*Timer) String() string
8+
pkg timing, method (IntervalPrinter) Print(io.Writer, []Interval, time.Duration) error
9+
pkg timing, type Interval struct
10+
pkg timing, type Interval struct, Depth int
11+
pkg timing, type Interval struct, End time.Duration
12+
pkg timing, type Interval struct, Name string
13+
pkg timing, type Interval struct, Start time.Duration
3614
pkg timing, type IntervalPrinter struct
3715
pkg timing, type IntervalPrinter struct, Indent int
3816
pkg timing, type IntervalPrinter struct, MinGap time.Duration
3917
pkg timing, type IntervalPrinter struct, TimeFormat string
40-
pkg timing, type Timer interface { Finish, Pop, Push, Root, String }
41-
pkg timing, type Timer interface, Finish()
42-
pkg timing, type Timer interface, Pop()
43-
pkg timing, type Timer interface, Push(string)
44-
pkg timing, type Timer interface, Root() Interval
45-
pkg timing, type Timer interface, String() string
18+
pkg timing, type IntervalPrinter struct, Zero time.Time
19+
pkg timing, type Timer struct
20+
pkg timing, type Timer struct, Intervals []Interval
21+
pkg timing, type Timer struct, Zero time.Time

timing/compact_timer.go

-173
This file was deleted.

timing/full_timer.go

-117
This file was deleted.

0 commit comments

Comments
 (0)