Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 7b6dbeb

Browse files
committed
More micro-optimizations
1 parent b5d226b commit 7b6dbeb

File tree

2 files changed

+151
-105
lines changed

2 files changed

+151
-105
lines changed

bin/ybench

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
require "bundler/inline"
5+
6+
gemfile do
7+
source "https://rubygems.org"
8+
gem "benchmark-ips"
9+
end
10+
11+
string = "a" * 1000 + "\n"
12+
13+
Benchmark.ips do |x|
14+
x.report("chomp") { string.chomp }
15+
x.report("chop") { string.chop }
16+
x.compare!
17+
end

lib/syntax_tree/node.rb

Lines changed: 134 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1413,17 +1413,21 @@ class Labels
14131413

14141414
def format_key(q, key)
14151415
case key
1416-
in Label
1416+
when Label
14171417
q.format(key)
1418-
in SymbolLiteral
1418+
when SymbolLiteral
14191419
q.format(key.value)
14201420
q.text(":")
1421-
in DynaSymbol[parts: [TStringContent[value: LABEL] => part]]
1422-
q.format(part)
1423-
q.text(":")
1424-
in DynaSymbol
1425-
q.format(key)
1426-
q.text(":")
1421+
when DynaSymbol
1422+
parts = key.parts
1423+
1424+
if parts.length == 1 && (part = parts.first) && part.is_a?(TStringContent) && part.value.match?(LABEL)
1425+
q.format(part)
1426+
q.text(":")
1427+
else
1428+
q.format(key)
1429+
q.text(":")
1430+
end
14271431
end
14281432
end
14291433
end
@@ -1433,8 +1437,7 @@ class Rockets
14331437
def format_key(q, key)
14341438
case key
14351439
when Label
1436-
q.text(":")
1437-
q.text(key.value.chomp(":"))
1440+
q.text(":#{key.value.chomp(":")}")
14381441
when DynaSymbol
14391442
q.text(":")
14401443
q.format(key)
@@ -2145,105 +2148,128 @@ def format(q)
21452148
q.group do
21462149
q.text(keyword)
21472150

2148-
case node.arguments.parts
2149-
in []
2151+
parts = node.arguments.parts
2152+
length = parts.length
2153+
2154+
if length == 0
21502155
# Here there are no arguments at all, so we're not going to print
21512156
# anything. This would be like if we had:
21522157
#
21532158
# break
21542159
#
2155-
in [
2156-
Paren[
2157-
contents: {
2158-
body: [ArrayLiteral[contents: { parts: [_, _, *] }] => array] }
2159-
]
2160-
]
2161-
# Here we have a single argument that is a set of parentheses wrapping
2162-
# an array literal that has at least 2 elements. We're going to print
2163-
# the contents of the array directly. This would be like if we had:
2164-
#
2165-
# break([1, 2, 3])
2166-
#
2167-
# which we will print as:
2168-
#
2169-
# break 1, 2, 3
2170-
#
2171-
q.text(" ")
2172-
format_array_contents(q, array)
2173-
in [Paren[contents: { body: [ArrayLiteral => statement] }]]
2174-
# Here we have a single argument that is a set of parentheses wrapping
2175-
# an array literal that has 0 or 1 elements. We're going to skip the
2176-
# parentheses but print the array itself. This would be like if we
2177-
# had:
2178-
#
2179-
# break([1])
2180-
#
2181-
# which we will print as:
2182-
#
2183-
# break [1]
2184-
#
2185-
q.text(" ")
2186-
q.format(statement)
2187-
in [Paren[contents: { body: [statement] }]] if skip_parens?(statement)
2188-
# Here we have a single argument that is a set of parentheses that
2189-
# themselves contain a single statement. That statement is a simple
2190-
# value that we can skip the parentheses for. This would be like if we
2191-
# had:
2192-
#
2193-
# break(1)
2194-
#
2195-
# which we will print as:
2196-
#
2197-
# break 1
2198-
#
2199-
q.text(" ")
2200-
q.format(statement)
2201-
in [Paren => part]
2202-
# Here we have a single argument that is a set of parentheses. We're
2203-
# going to print the parentheses themselves as if they were the set of
2204-
# arguments. This would be like if we had:
2205-
#
2206-
# break(foo.bar)
2207-
#
2208-
q.format(part)
2209-
in [ArrayLiteral[contents: { parts: [_, _, *] }] => array]
2210-
# Here there is a single argument that is an array literal with at
2211-
# least two elements. We skip directly into the array literal's
2212-
# elements in order to print the contents. This would be like if we
2213-
# had:
2214-
#
2215-
# break [1, 2, 3]
2216-
#
2217-
# which we will print as:
2218-
#
2219-
# break 1, 2, 3
2220-
#
2221-
q.text(" ")
2222-
format_array_contents(q, array)
2223-
in [ArrayLiteral => part]
2224-
# Here there is a single argument that is an array literal with 0 or 1
2225-
# elements. In this case we're going to print the array as it is
2226-
# because skipping the brackets would change the remaining. This would
2227-
# be like if we had:
2228-
#
2229-
# break []
2230-
# break [1]
2231-
#
2232-
q.text(" ")
2233-
q.format(part)
2234-
in [_]
2235-
# Here there is a single argument that hasn't matched one of our
2236-
# previous cases. We're going to print the argument as it is. This
2237-
# would be like if we had:
2238-
#
2239-
# break foo
2240-
#
2241-
format_arguments(q, "(", ")")
2242-
else
2160+
elsif length >= 2
22432161
# If there are multiple arguments, format them all. If the line is
22442162
# going to break into multiple, then use brackets to start and end the
22452163
# expression.
22462164
format_arguments(q, " [", "]")
2165+
else
2166+
# If we get here, then we're formatting a single argument to the flow
2167+
# control keyword.
2168+
part = parts.first
2169+
2170+
case part
2171+
when Paren
2172+
statements = part.contents.body
2173+
2174+
if statements.length == 1
2175+
statement = statements.first
2176+
2177+
if statement.is_a?(ArrayLiteral)
2178+
contents = statement.contents
2179+
2180+
if contents && contents.parts.length >= 2
2181+
# Here we have a single argument that is a set of parentheses
2182+
# wrapping an array literal that has at least 2 elements.
2183+
# We're going to print the contents of the array directly.
2184+
# This would be like if we had:
2185+
#
2186+
# break([1, 2, 3])
2187+
#
2188+
# which we will print as:
2189+
#
2190+
# break 1, 2, 3
2191+
#
2192+
q.text(" ")
2193+
format_array_contents(q, statement)
2194+
else
2195+
# Here we have a single argument that is a set of parentheses
2196+
# wrapping an array literal that has 0 or 1 elements. We're
2197+
# going to skip the parentheses but print the array itself.
2198+
# This would be like if we had:
2199+
#
2200+
# break([1])
2201+
#
2202+
# which we will print as:
2203+
#
2204+
# break [1]
2205+
#
2206+
q.text(" ")
2207+
q.format(statement)
2208+
end
2209+
elsif skip_parens?(statement)
2210+
# Here we have a single argument that is a set of parentheses
2211+
# that themselves contain a single statement. That statement is
2212+
# a simple value that we can skip the parentheses for. This
2213+
# would be like if we had:
2214+
#
2215+
# break(1)
2216+
#
2217+
# which we will print as:
2218+
#
2219+
# break 1
2220+
#
2221+
q.text(" ")
2222+
q.format(statement)
2223+
else
2224+
# Here we have a single argument that is a set of parentheses.
2225+
# We're going to print the parentheses themselves as if they
2226+
# were the set of arguments. This would be like if we had:
2227+
#
2228+
# break(foo.bar)
2229+
#
2230+
q.format(part)
2231+
end
2232+
else
2233+
q.format(part)
2234+
end
2235+
when ArrayLiteral
2236+
contents = part.contents
2237+
2238+
if contents && contents.parts.length >= 2
2239+
# Here there is a single argument that is an array literal with at
2240+
# least two elements. We skip directly into the array literal's
2241+
# elements in order to print the contents. This would be like if
2242+
# we had:
2243+
#
2244+
# break [1, 2, 3]
2245+
#
2246+
# which we will print as:
2247+
#
2248+
# break 1, 2, 3
2249+
#
2250+
q.text(" ")
2251+
format_array_contents(q, part)
2252+
else
2253+
# Here there is a single argument that is an array literal with 0
2254+
# or 1 elements. In this case we're going to print the array as it
2255+
# is because skipping the brackets would change the remaining.
2256+
# This would be like if we had:
2257+
#
2258+
# break []
2259+
# break [1]
2260+
#
2261+
q.text(" ")
2262+
q.format(part)
2263+
end
2264+
else
2265+
# Here there is a single argument that hasn't matched one of our
2266+
# previous cases. We're going to print the argument as it is. This
2267+
# would be like if we had:
2268+
#
2269+
# break foo
2270+
#
2271+
format_arguments(q, "(", ")")
2272+
end
22472273
end
22482274
end
22492275
end
@@ -3791,15 +3817,18 @@ def initialize(operator, node)
37913817
end
37923818

37933819
def format(q)
3794-
space = [If, IfMod, Unless, UnlessMod].include?(q.parent.class)
3795-
37963820
left = node.left
37973821
right = node.right
37983822

37993823
q.format(left) if left
3800-
q.text(" ") if space
3801-
q.text(operator)
3802-
q.text(" ") if space
3824+
3825+
case q.parent
3826+
when If, IfMod, Unless, UnlessMod
3827+
q.text(" #{operator} ")
3828+
else
3829+
q.text(operator)
3830+
end
3831+
38033832
q.format(right) if right
38043833
end
38053834
end

0 commit comments

Comments
 (0)