Optimize syrup encoder by reducing bv allocations
This reduces the number of allocations of bytevectors in the syrup encoder. Unfortunately, there are more allocations than I'd like, but since syrup encodes the key value pairs in hashmaps and then sorts them (on their encoded syrup representation), it's not possible to reduce them to one per syrup-encode call. Either way this had a decent speedup in ,profile.
Fix #220 (closed)
Here's a little look at the optimization
scheme@(goblins contrib syrup)> (define data (list 1 2 3 4
"fix six seven"
'eight-nine-ten
#t #f
(list 11 12 13 14)
(list (list (list)))))
;; NEW
scheme@(goblins contrib syrup)> ,profile (let lp ((x 100000))
(unless (zero? x)
(syrup-encode data) ;; new optimized version
(lp (- x 1))))
% cumulative self
time seconds seconds procedure
34.40 1.17 1.17 apply-smob/0
27.60 0.95 0.94 open-bytevector-output-port
14.40 0.52 0.49 set-port-encoding!
2.80 0.11 0.10 number->string
2.80 0.10 0.10 display
2.40 3.19 0.08 goblins/contrib/syrup.scm:236:2:encode
2.40 0.08 0.08 get-internal-run-time
2.00 0.15 0.07 ice-9/iconv.scm:50:0:string->bytevector
2.00 0.07 0.07 put-bytevector
1.60 0.07 0.05 procedure?
1.20 3.07 0.04 goblins/contrib/syrup.scm:263:12
1.20 0.08 0.04 system/vm/program.scm:104:0:program-address-range
1.20 0.08 0.04 string-ci=?
1.20 0.05 0.04 close-port
0.80 0.04 0.03 bytevector-u64-ref
0.40 5.86 0.01 ice-9/binary-ports.scm:65:0:call-with-output-bytevector
0.40 4.11 0.01 srfi/srfi-1.scm:628:2:for-each
0.40 0.83 0.01 goblins/contrib/syrup.scm:122:0:write-as-netstring!
0.40 0.01 0.01 goblins/contrib/syrup.scm:180:0:syrup-encode
0.40 0.01 0.01 goblins/contrib/syrup.scm:224:5
0.00 3.39 0.00 <current input>:409:9
0.00 0.52 0.00 ice-9/iconv.scm:41:7
0.00 0.16 0.00 statprof.scm:251:4:profile-signal-handler
0.00 0.16 0.00 anon #x150028130
0.00 0.08 0.00 make-stack
0.00 0.04 0.00 system/vm/debug.scm:221:0:find-program-debug-info
0.00 0.04 0.00 system/vm/elf.scm:373:0:parse-elf64
0.00 0.04 0.00 system/vm/debug.scm:163:0:debug-context-from-image
---
Sample count: 250
Total time: 3.393983 seconds (2.621118 seconds in GC)
;; OLD
scheme@(goblins contrib syrup)> ,profile (let lp ((x 100000))
(unless (zero? x)
(old/syrup-encode data) ;; older origin/main version
(lp (- x 1))))
% cumulative self
time seconds seconds procedure
36.40 1.50 1.42 open-bytevector-output-port
18.73 0.77 0.73 apply-smob/0
13.07 0.52 0.51 set-port-encoding!
3.89 0.29 0.15 ice-9/iconv.scm:50:0:string->bytevector
2.83 0.11 0.11 string-ci=?
2.47 0.12 0.10 system/vm/program.scm:104:0:program-address-range
2.47 0.11 0.10 make-bytevector
2.47 0.11 0.10 bytevector-copy!
2.47 0.10 0.10 procedure?
1.77 0.43 0.07 goblins/contrib/syrup.scm:97:0:bytes-append
1.41 0.05 0.05 get-internal-run-time
1.06 22.64 0.04 srfi/srfi-1.scm:584:5:map1
1.06 0.12 0.04 srfi/srfi-1.scm:452:2:fold
1.06 0.04 0.04 display
1.06 0.04 0.04 integer?
0.71 0.08 0.03 srfi/srfi-1.scm:580:2:map
0.71 0.04 0.03 number->string
0.71 0.03 0.03 goblins/contrib/syrup.scm:100:5
0.71 0.03 0.03 close-port
0.35 6.84 0.01 goblins/contrib/syrup.scm:584:2:encode
0.35 3.72 0.01 ice-9/binary-ports.scm:65:0:call-with-output-bytevector
0.35 0.84 0.01 goblins/contrib/syrup.scm:122:0:write-as-netstring!
0.35 0.19 0.01 statprof.scm:251:4:profile-signal-handler
0.35 0.03 0.01 ice-9/iconv.scm:39:6:string-ci=?@guile
0.35 0.01 0.01 goblins/contrib/syrup.scm:120:3
0.35 0.01 0.01 ice-9/eval.scm:590:16
0.35 0.01 0.01 goblins/contrib/syrup.scm:544:2:build-encode-hash
0.35 0.01 0.01 goblins/contrib/syrup.scm:118:0:netstring-encode
0.35 0.01 0.01 put-bytevector
0.35 0.01 0.01 %after-gc-thunk
0.35 0.01 0.01 bytevector->pointer
0.35 0.01 0.01 goblins/contrib/syrup.scm:584:2:encode
0.35 0.01 0.01 bytevector-u64-ref
0.00 3.89 0.00 <current input>:424:9
0.00 0.52 0.00 ice-9/iconv.scm:41:7
0.00 0.22 0.00 anon #x150028130
0.00 0.12 0.00 make-stack
0.00 0.03 0.00 system/vm/debug.scm:221:0:find-program-debug-info
0.00 0.03 0.00 system/vm/debug.scm:163:0:debug-context-from-image
0.00 0.01 0.00 goblins/contrib/syrup.scm:543:0:old/syrup-encode
0.00 0.01 0.00 system/vm/elf.scm:826:0:elf-section-by-name
0.00 0.01 0.00 system/vm/elf.scm:673:0:parse-elf64-section-header
---
Sample count: 283
Total time: 3.889716 seconds (2.948949 seconds in GC)
Edited by Jessica Tallon