Skip to content

Commit f64e836

Browse files
b5essobedo
authored andcommitted
perf(time): don't allocate int on time & duration Hash methods
1 parent 8d56b95 commit f64e836

2 files changed

Lines changed: 18 additions & 51 deletions

File tree

lib/time/doc.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ package from the go standard library.
55
time defines time primitives for starlark
66
path: time
77
functions:
8-
duration(string) duration
8+
parse_duration(string) duration
99
parse a duration
10-
location(string) location
10+
parse_location(string) location
1111
parse a location
12-
time(string, format=..., location=...) time
12+
parse_time(string, format=..., location=...) time
1313
parse a time
14-
fromtimestamptime(int) time
14+
from_timestamp(int) time
1515
parse a Unix timestamp
1616
now() time
17-
implementations would be able to make this a constant
17+
internal call to time.Time, returns the current time by default
1818
zero() time
1919
a constant
2020
@@ -32,15 +32,15 @@ package from the go standard library.
3232
duration < duration = booleans
3333
time
3434
functions:
35-
year() int
36-
month() int
37-
day() int
38-
hour() int
39-
minute() int
40-
second() int
41-
nanosecond() int
42-
unix() int
43-
unix_nano() int
35+
year duration
36+
month int
37+
day int
38+
hour int
39+
minute int
40+
second int
41+
nanosecond int
42+
unix int
43+
unix_nano int
4444
in_location(string) time
4545
get time representing the same instant but in a different location
4646
format(string) string

lib/time/time.go

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ var Module = &starlarkstruct.Module{
2626
"time": starlark.NewBuiltin("time", newTime),
2727
"parse_time": starlark.NewBuiltin("time", parseTime),
2828
"sleep": starlark.NewBuiltin("sleep", sleep),
29-
"from_timestamp": starlark.NewBuiltin("fromtimestamp", fromUnixTime),
29+
"from_timestamp": starlark.NewBuiltin("from_timestamp", fromTimestamp),
3030

3131
"zero": Time{},
3232

@@ -114,7 +114,7 @@ func parseTime(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple
114114
return Time(t), nil
115115
}
116116

117-
func fromUnixTime(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
117+
func fromTimestamp(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
118118
var x int
119119
if err := starlark.UnpackPositionalArgs("from_timestamp", args, kwargs, 1, &x); err != nil {
120120
return nil, err
@@ -179,7 +179,7 @@ func (d Duration) Freeze() {}
179179
// Hash returns a function of x such that Equals(x, y) => Hash(x) == Hash(y)
180180
// required by starlark.Value interface.
181181
func (d Duration) Hash() (uint32, error) {
182-
return starlark.MakeInt64(int64(d)).Hash()
182+
return uint32(d) ^ uint32(int64(d)>>32), nil
183183
}
184184

185185
// Truth returns the truth value of an object required by starlark.Value
@@ -253,7 +253,6 @@ func (d Duration) Binary(op syntax.Token, yV starlark.Value, _ starlark.Side) (s
253253
return Duration(x - time.Duration(i)), nil
254254
case Time:
255255
// duration - time = duration
256-
return nil, nil
257256
}
258257

259258
case syntax.SLASH:
@@ -272,16 +271,6 @@ func (d Duration) Binary(op syntax.Token, yV starlark.Value, _ starlark.Side) (s
272271
return nil, fmt.Errorf("%s division by zero", d.Type())
273272
}
274273
return Duration(d / Duration(i)), nil
275-
case Time:
276-
// y := time.Time(yV.(Time))
277-
// switch op {
278-
// case syntax.PLUS:
279-
// // duration + time = time
280-
// return Time(y.Add(x)), nil
281-
// case syntax.MINUS:
282-
// // duration - time = duration
283-
// return nil, nil
284-
// }
285274
}
286275

287276
// if int64(y) == 0 {
@@ -302,16 +291,6 @@ func (d Duration) Binary(op syntax.Token, yV starlark.Value, _ starlark.Side) (s
302291
return nil, fmt.Errorf("int value out of range (want signed 64-bit value)")
303292
}
304293
return d / Duration(i), nil
305-
case Time:
306-
// y := time.Time(yV.(Time))
307-
// switch op {
308-
// case syntax.PLUS:
309-
// // duration + time = time
310-
// return Time(y.Add(x)), nil
311-
// case syntax.MINUS:
312-
// // duration - time = duration
313-
// return nil, nil
314-
// }
315294
}
316295

317296
// if int64(y) == 0 {
@@ -329,19 +308,7 @@ func (d Duration) Binary(op syntax.Token, yV starlark.Value, _ starlark.Side) (s
329308
return nil, fmt.Errorf("int value out of range (want signed 64-bit value)")
330309
}
331310
return d * Duration(i), nil
332-
case Time:
333-
// y := time.Time(yV.(Time))
334-
// switch op {
335-
// case syntax.PLUS:
336-
// // duration + time = time
337-
// return Time(y.Add(x)), nil
338-
// case syntax.MINUS:
339-
// // duration - time = duration
340-
// return nil, nil
341-
// }
342311
}
343-
344-
// return Duration(x * y), nil
345312
}
346313

347314
return nil, nil
@@ -378,7 +345,7 @@ func (t Time) Freeze() {}
378345
// Hash returns a function of x such that Equals(x, y) => Hash(x) == Hash(y)
379346
// required by starlark.Value interface.
380347
func (t Time) Hash() (uint32, error) {
381-
return starlark.MakeInt64(time.Time(t).UnixNano()).Hash()
348+
return uint32(time.Time(t).UnixNano()) ^ uint32(int64(time.Time(t).UnixNano())>>32), nil
382349
}
383350

384351
// Truth returns the truth value of an object required by starlark.Value

0 commit comments

Comments
 (0)