<commit_before>#lang racket/base

;; Pycket does really poorly on the second version, but fine on the first, we need
;; a way to improve this.
;; Example taken from http://rfrn.org/~shu/2013/03/20/two-reasons-functional-style-is-slow-in-spidermonkey.html

(define N 50)
(define iters 50000)

(define (arr-for-each vs f)
  (let ([n (vector-length vs)])
    (let loop ([i 0])
      (if (= i n) (void)
        (begin
          (f (vector-ref vs i))
          (loop (+ i 1)))))))

(define vec
  (build-vector N
    (λ (_)
      (build-vector N
        (λ (_) (random))))))

(define max 0.0)

(define (loop f j n)
  (if (= n 1000000000)
    j
    (loop f (f j) (+ n 1))))

(displayln 'simple)

;; XXX Making use of W_PromotableClosure causes a significant performance hit here
(time (loop (lambda (x) (add1 x)) 0 0))
(time (loop (lambda (x) (sub1 x)) 0 0))
(time (loop (lambda (x) (add1 x)) 0 0))
(time (loop (lambda (x) (sub1 x)) 0 0))

(displayln 'vectors)

(displayln 'direct)
(time
  (for ([_ (in-range iters)])
    (set! max 0.0)
    (for ([i (in-range (vector-length vec))])
      (let ([vec^ (vector-ref vec i)])
      (for ([j (in-range (vector-length vec^))])
        (let ([v (vector-ref vec^ j)])
          (when (< max v) (set! max v))))))))

(displayln 'first)
(time
  (for ([_ (in-range iters)])
    (set! max 0.0)
    (arr-for-each vec
      (λ (vec^)
        (arr-for-each vec^
          (λ (v)
            (when (< max v) (set! max v))))))))

(displayln 'second)
;; This copy runs ~5.5x slower than the one above, despite being a verbatim copy.
(time
  (for ([_ (in-range iters)])
    (set! max 0.0)
    (arr-for-each vec
      (λ (vec^)
        (arr-for-each vec^
          (λ (v)
            (when (< max v) (set! max v))))))))

<commit_msg>Add a note to sample benchmarks<commit_after>#lang racket/base

;; Pycket does really poorly on the second version, but fine on the first, we need
;; a way to improve this.
;; Example taken from http://rfrn.org/~shu/2013/03/20/two-reasons-functional-style-is-slow-in-spidermonkey.html

(define N 50)
(define iters 50000)

(define (arr-for-each vs f)
  (let ([n (vector-length vs)])
    (let loop ([i 0])
      (if (= i n) (void)
        (begin
          (f (vector-ref vs i))
          (loop (+ i 1)))))))

(define vec
  (build-vector N
    (λ (_)
      (build-vector N
        (λ (_) (random))))))

(define max 0.0)

(define (loop f j n)
  (if (= n 1000000000)
    j
    (loop f (f j) (+ n 1))))

(displayln 'simple)

;; XXX Making use of W_PromotableClosure causes a significant performance hit here
;; We should probably investiage why that is. Disabling W_PromotableClosures gives
;; a more gradual performance degredation on these four runs
(time (loop (lambda (x) (add1 x)) 0 0))
(time (loop (lambda (x) (sub1 x)) 0 0))
(time (loop (lambda (x) (add1 x)) 0 0))
(time (loop (lambda (x) (sub1 x)) 0 0))

(displayln 'vectors)

(displayln 'direct)
(time
  (for ([_ (in-range iters)])
    (set! max 0.0)
    (for ([i (in-range (vector-length vec))])
      (let ([vec^ (vector-ref vec i)])
      (for ([j (in-range (vector-length vec^))])
        (let ([v (vector-ref vec^ j)])
          (when (< max v) (set! max v))))))))

(displayln 'first)
(time
  (for ([_ (in-range iters)])
    (set! max 0.0)
    (arr-for-each vec
      (λ (vec^)
        (arr-for-each vec^
          (λ (v)
            (when (< max v) (set! max v))))))))

(displayln 'second)
;; This copy runs ~5.5x slower than the one above, despite being a verbatim copy.
(time
  (for ([_ (in-range iters)])
    (set! max 0.0)
    (arr-for-each vec
      (λ (vec^)
        (arr-for-each vec^
          (λ (v)
            (when (< max v) (set! max v))))))))


{'fraction_non_alphanumeric': 0.17892156862745098, 'fraction_numerical': 0.027233115468409588, 'mean_word_length': 2.5453667953667956, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6354539', 'n_tokens_mistral': 1484, 'n_tokens_neox': 1367, 'n_words': 474}