git ssb

2+

Dominic / pull-stream



Commit 8c0347d8e7f88171cd4c45eec22b42c5c7332fac

Optimize `pull` sink callback and throw if the sink is called multiple times.

The check sets the copied arguments to `null` to minimize potential memory
leaks. The `switch` is used to try to avoid a costly `unshift` + Array `apply`,
to make the call slightly faster.
Isiah Meadows authored on 7/8/2016, 3:54:14 AM
impinball committed on 7/8/2016, 7:20:43 AM
Parent: 680e32e19ee15c33b5ad2e19c94d765616f983ae

Files changed

pull.jschanged
test/pull.jschanged
pull.jsView
@@ -6,10 +6,27 @@
66 var args = new Array(length)
77 for(var i = 0; i < length; i++)
88 args[i] = arguments[i]
99 return function (read) {
10- args.unshift(read)
11- return pull.apply(null, args)
10+ if (args == null) {
11+ throw new TypeError("partial sink should only be called once!")
12+ }
13+
14+ // Grab the reference after the check, because it's always an array now
15+ // (engines like that kind of consistency).
16+ var ref = args
17+ args = null
18+
19+ // Prioritize common case of small number of pulls.
20+ switch (length) {
21+ case 1: return pull(read, ref[0])
22+ case 2: return pull(read, ref[0], ref[1])
23+ case 3: return pull(read, ref[0], ref[1], ref[2])
24+ case 4: return pull(read, ref[0], ref[1], ref[2], ref[3])
25+ default:
26+ ref.unshift(read)
27+ return pull.apply(null, ref)
28+ }
1229 }
1330 }
1431
1532 var read = a
@@ -29,9 +46,4 @@
2946 }
3047
3148 return read
3249 }
33-
34-
35-
36-
37-
test/pull.jsView
@@ -3,10 +3,9 @@
33 function curry (fun) {
44 return function () {
55 var args = [].slice.call(arguments)
66 return function (read) {
7- args.unshift(read)
8- return fun.apply(null, args)
7+ return fun.apply(null, [read].concat(args))
98 }
109 }
1110 }
1211
@@ -94,4 +93,21 @@
9493 // })
9594 // )
9695 //
9796
97+tape("writable pull() should throw when called twice", function (t) {
98+ t.plan(2)
99+
100+ var stream = pull(
101+ map(function (e) { return e*e }),
102+ sum(function (err, value) {
103+ console.log(value)
104+ t.equal(value, 385)
105+ })
106+ )
107+
108+ stream(values([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]))
109+
110+ t.throws(function () {
111+ stream(values([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]))
112+ }, TypeError)
113+})

Built with git-ssb-web