Guest User

Untitled

a guest
May 24th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. Red []
  2.  
  3. ; outright improvement of collect mezzanine's performance and RAM footprint
  4.  
  5. ; the compiled default "collect"
  6. collect1: :collect
  7.  
  8. ; the interpreted default "collect"
  9. collect2: func spec-of :collect body-of :collect
  10.  
  11. ; the improved, compilable "collect"
  12. ; 1) doesn't allocate new functions
  13. ; 2) uses `bind` instead of parse
  14. collect3: func [
  15. {Collect in a new block all the values passed to KEEP function from the body block}
  16. body [block!] "Block to evaluate"
  17. /into {Insert into a buffer instead (returns position after insert)}
  18. coll [series!] "The buffer series (modified)"
  19. ] bind [
  20. unless coll [coll: make block! 16]
  21. ; `also` here works like a stack
  22. body': also body' (
  23. body': body
  24. coll': also coll' (
  25. coll': coll
  26. shoot :keep coll
  27. ) )
  28. either into [coll] [head coll]
  29. ] context [ ; why not move this stuff out into an anonymous context?
  30. keep: func [v /only] [either only [append/only coll' v] [append coll' v] v]
  31. ; these two are here to satisfy the compiler, otherwise can be done by binding keep/shoot bodies to :collect3
  32. body': coll': none
  33. ; can't bind to a context [keep: collected: none] directly, since it will also bind `self` to it
  34. ; so here comes a func: `shoot` holds the 2 words used by body: `keep` and `collected`
  35. shoot: func [keep collected] [do bind body' 'keep]
  36. ]
  37.  
  38.  
  39. ;------------------ tests -------------------
  40.  
  41. ; recursive invocation test
  42. foreach e [
  43. [collect1 [loop 4 [ keep collect1 [repeat i 4 [keep i probe collected]] probe collected ]]]
  44. [collect2 [loop 4 [ keep collect2 [repeat i 4 [keep i probe collected]] probe collected ]]]
  45. [collect3 [loop 4 [ keep collect3 [repeat i 4 [keep i probe collected]] probe collected ]]]
  46. ] [ print [mold e "=>" mold do e lf]]
  47.  
  48.  
  49. ; benchmark
  50.  
  51. clock: func [b /local t1 t2 s1 s2 b' r] [
  52. b': copy/deep b
  53. s1: stats
  54. t1: now/precise/time
  55. r: loop 10000 [do b]
  56. t2: now/precise/time
  57. s2: stats
  58. print [pad (t2 - t1) 12 pad s2 - s1 8 mold/flat b' "=>" mold/flat r]
  59. ]
  60.  
  61.  
  62. n: 10
  63. print lf
  64. clock [collect1 [repeat i n [keep i]]]
  65. clock [collect2 [repeat i n [keep i]]]
  66. clock [collect3 [repeat i n [keep i]]]
  67. clock [collect1/into [repeat i n [keep i]] clear []]
  68. clock [collect2/into [repeat i n [keep i]] clear []]
  69. clock [collect3/into [repeat i n [keep i]] clear []]
Add Comment
Please, Sign In to add comment