<commit_before>#lang racket/base

(require syntax/parse syntax/stx
         racket/match
         "../utils/utils.rkt"
         (for-template racket/base racket/unsafe/ops)
         (utils tc-utils)
         (rep type-rep)
         (types type-table utils)
         (optimizer utils logging))

(provide box-opt-expr)

(define-syntax-class box-expr
  #:commit
  (pattern e:expr
           #:when (match (type-of #'e)
                    [(tc-result1: (Box: _)) #t]
                    [_ #f])
           #:with opt ((optimize) #'e)))

(define-syntax-class box-op
  #:commit
  ;; we need the * versions of these unsafe operations to be chaperone-safe
  (pattern (~literal unbox)    #:with unsafe #'unsafe-unbox)
  (pattern (~literal set-box!) #:with unsafe #'unsafe-set-box!))

(define-syntax-class box-opt-expr
  #:commit
  (pattern (#%plain-app op:box-op b:box-expr new:expr ...)
           #:with opt
           (begin (log-optimization "box" "Box check elimination." this-syntax)
                  (add-disappeared-use #'op)
                  #`(op.unsafe b.opt #,@(stx-map (optimize) #'(new ...))))))
<commit_msg>Cleanup box optimizations.<commit_after>#lang racket/base

(require syntax/parse syntax/stx
         racket/match
         "../utils/utils.rkt"
         (for-template racket/base racket/unsafe/ops)
         (utils tc-utils)
         (rep type-rep)
         (types type-table utils)
         (optimizer utils logging))

(provide box-opt-expr)

(define-unsafe-syntax-class unbox)
(define-unsafe-syntax-class set-box!)

(define-syntax-class box-op
  #:attributes (unsafe)
  (pattern :unbox^)
  (pattern :set-box!^))

(define-syntax-class box-opt-expr
  #:commit
  (pattern (#%plain-app op:box-op b:opt-expr new:opt-expr ...)
    #:do [(log-opt "box" "Box check elimination.")]
    #:with opt #`(op.unsafe b.opt new.opt ...)))

{'fraction_non_alphanumeric': 0.1598694942903752, 'fraction_numerical': 0.000543773790103317, 'mean_word_length': 2.363802559414991, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, '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': '3317141', 'n_tokens_mistral': 654, 'n_tokens_neox': 609, 'n_words': 144}