ruby-changes:64066
From: Lourens <ko1@a...>
Date: Thu, 10 Dec 2020 18:15:42 +0900 (JST)
Subject: [ruby-changes:64066] 9a17437558 (master): Right size literal regular expression buffers on compile
https://git.ruby-lang.org/ruby.git/commit/?id=9a17437558 From 9a17437558e42aa1da372b515ba8bc18067d578c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lourens=20Naud=C3=A9?= <lourens@b...> Date: Tue, 26 Nov 2019 13:07:49 +0000 Subject: Right size literal regular expression buffers on compile diff --git a/regcomp.c b/regcomp.c index df7f73b..7799e1d 100644 --- a/regcomp.c +++ b/regcomp.c @@ -138,6 +138,25 @@ bitset_on_num(BitSetRef bs) https://github.com/ruby/ruby/blob/trunk/regcomp.c#L138 } #endif +// Attempt to right size allocated buffers for a regex post compile +static void +onig_reg_resize(regex_t *reg) +{ + resize: + if (reg->alloc > reg->used) { + unsigned char *new_ptr = xrealloc(reg->p, reg->used); + // Skip the right size optimization if memory allocation fails + if (new_ptr) { + reg->alloc = reg->used; + reg->p = new_ptr; + } + } + if (reg->chain) { + reg = reg->chain; + goto resize; + } +} + extern int onig_bbuf_init(BBuf* buf, OnigDistance size) { @@ -5886,6 +5905,7 @@ onig_compile(regex_t* reg, const UChar* pattern, const UChar* pattern_end, https://github.com/ruby/ruby/blob/trunk/regcomp.c#L5905 #endif end: + onig_reg_resize(reg); return r; err_unset: -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/