Skip to content

Commit fc08916

Browse files
committed
⚡ Use frozen const when parsing empty arrays [🚧 WIP]
TODO, record benchmarks
1 parent b205add commit fc08916

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

lib/net/imap/response_parser.rb

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ def parse(str)
6262
T_TEXT = :TEXT # any char except CRLF
6363
T_EOF = :EOF # end of response string
6464

65+
# Use to avoid allocation when we know the result is empty
66+
EMPTY_ARRAY = [].freeze
67+
private_constant :EMPTY_ARRAY
68+
6569
module ResponseConditions
6670
OK = "OK"
6771
NO = "NO"
@@ -596,7 +600,7 @@ def tagged_ext_simple
596600
# "(" [tagged-ext-comp] ")"
597601
def tagged_ext_val
598602
if lpar?
599-
_ = peek_rpar? ? [] : tagged_ext_comp
603+
_ = peek_rpar? ? EMPTY_ARRAY : tagged_ext_comp
600604
rpar
601605
_
602606
else
@@ -1367,7 +1371,7 @@ def mailbox_data__list
13671371
# ; This is the list information pointed to by the ABNF
13681372
# ; item "mailbox-data", which is defined above
13691373
def mailbox_list
1370-
lpar; attr = peek_rpar? ? [] : mbx_list_flags; rpar
1374+
lpar; attr = peek_rpar? ? EMPTY_ARRAY : mbx_list_flags; rpar
13711375
SP!; delim = nquoted
13721376
SP!; name = mailbox
13731377
# TODO: mbox-list-extended
@@ -1467,8 +1471,12 @@ def acl_data
14671471
# obsolete-search-response = "SEARCH" *(SP nz-number)
14681472
def mailbox_data__search
14691473
name = label_in("SEARCH", "SORT")
1470-
data = []
1471-
while _ = SP? && nz_number? do data << _ end
1474+
if (_ = SP? && nz_number?)
1475+
data = [_]
1476+
while _ = SP? && nz_number? do data << _ end
1477+
else
1478+
data = EMPTY_ARRAY
1479+
end
14721480
if lpar?
14731481
label("MODSEQ"); SP!
14741482
modseq = mod_sequence_value
@@ -1517,7 +1525,7 @@ def thread_members
15171525
else nested = thread_nested; break
15181526
end
15191527
end
1520-
members.reverse.inject(nested || []) {|subthreads, number|
1528+
members.reverse.inject(nested || EMPTY_ARRAY) {|subthreads, number|
15211529
[ThreadMember.new(number, subthreads)]
15221530
}.first
15231531
end
@@ -1689,7 +1697,7 @@ def namespace_response
16891697

16901698
# namespace = nil / "(" 1*namespace-descr ")"
16911699
def namespace
1692-
NIL? and return []
1700+
NIL? and return EMPTY_ARRAY
16931701
lpar
16941702
list = [namespace_descr]
16951703
list << namespace_descr until rpar?
@@ -1816,13 +1824,13 @@ def resp_text_code
18161824
data =
18171825
case name
18181826
when "CAPABILITY" then resp_code__capability
1819-
when "PERMANENTFLAGS" then SP? ? flag_perm__list : []
1827+
when "PERMANENTFLAGS" then SP? ? flag_perm__list : EMPTY_ARRAY
18201828
when "UIDNEXT" then SP!; nz_number
18211829
when "UIDVALIDITY" then SP!; nz_number
18221830
when "UNSEEN" then SP!; nz_number # rev1 only
18231831
when "APPENDUID" then SP!; resp_code_apnd__data # rev2, UIDPLUS
18241832
when "COPYUID" then SP!; resp_code_copy__data # rev2, UIDPLUS
1825-
when "BADCHARSET" then SP? ? charset__list : []
1833+
when "BADCHARSET" then SP? ? charset__list : EMPTY_ARRAY
18261834
when "ALERT", "PARSE", "READ-ONLY", "READ-WRITE", "TRYCREATE",
18271835
"UNAVAILABLE", "AUTHENTICATIONFAILED", "AUTHORIZATIONFAILED",
18281836
"EXPIRED", "PRIVACYREQUIRED", "CONTACTADMIN", "NOPERM", "INUSE",
@@ -1953,7 +1961,7 @@ def x_gm_label; accept(T_BSLASH) ? atom.capitalize.to_sym : astring end
19531961

19541962
# See https://developers.google.com/gmail/imap/imap-extensions
19551963
def x_gm_labels
1956-
lpar; return [] if rpar?
1964+
lpar; return EMPTY_ARRAY if rpar?
19571965
labels = []
19581966
labels << x_gm_label
19591967
labels << x_gm_label while SP?

0 commit comments

Comments
 (0)