Skip to content
This repository was archived by the owner on Feb 3, 2026. It is now read-only.

Commit abe406d

Browse files
committed
Add write_punctuator_char and optimize minifier
1 parent 3ea476f commit abe406d

1 file changed

Lines changed: 35 additions & 27 deletions

File tree

src/parser/query/minify.rs

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ impl Minifier {
7575
self.last_was_non_punctuator = false;
7676
}
7777

78+
#[inline(always)]
79+
fn write_punctuator_char(&mut self, c: char) {
80+
self.buf.push(c);
81+
self.last_was_non_punctuator = false;
82+
}
83+
84+
#[inline]
7885
fn write_document<'a, T: Text<'a>>(&mut self, doc: &Document<'a, T>) {
7986
for def in &doc.definitions {
8087
self.write_definition(def);
@@ -134,11 +141,11 @@ impl Minifier {
134141

135142
#[inline]
136143
fn write_selection_set<'a, T: Text<'a>>(&mut self, set: &SelectionSet<'a, T>) {
137-
self.write_punctuator("{");
144+
self.write_punctuator_char('{');
138145
for item in &set.items {
139146
self.write_selection(item);
140147
}
141-
self.write_punctuator("}");
148+
self.write_punctuator_char('}');
142149
}
143150

144151
#[inline]
@@ -147,7 +154,7 @@ impl Minifier {
147154
Selection::Field(f) => {
148155
if let Some(ref alias) = f.alias {
149156
self.write_non_punctuator(alias.as_ref());
150-
self.write_punctuator(":");
157+
self.write_punctuator_char(':');
151158
}
152159
self.write_non_punctuator(f.name.as_ref());
153160
self.write_arguments(&f.arguments);
@@ -187,56 +194,57 @@ impl Minifier {
187194
if vars.is_empty() {
188195
return;
189196
}
190-
self.write_punctuator("(");
197+
self.write_punctuator_char('(');
191198
for var in vars {
192-
self.write_punctuator("$");
199+
self.write_punctuator_char('$');
193200
self.write_non_punctuator(var.name.as_ref());
194-
self.write_punctuator(":");
201+
self.write_punctuator_char(':');
195202
self.write_type(&var.var_type);
196203
if let Some(ref def) = var.default_value {
197-
self.write_punctuator("=");
204+
self.write_punctuator_char('=');
198205
self.write_value(def);
199206
}
200207
}
201-
self.write_punctuator(")");
208+
self.write_punctuator_char(')');
202209
}
203210

204211
#[inline]
205212
fn write_type<'a, T: Text<'a>>(&mut self, ty: &Type<'a, T>) {
206213
match ty {
207214
Type::NamedType(name) => self.write_non_punctuator(name.as_ref()),
208215
Type::ListType(inner) => {
209-
self.write_punctuator("[");
216+
self.write_punctuator_char('[');
210217
self.write_type(inner);
211-
self.write_punctuator("]");
218+
self.write_punctuator_char(']');
212219
}
213220
Type::NonNullType(inner) => {
214221
self.write_type(inner);
215-
self.write_punctuator("!");
222+
self.write_punctuator_char('!');
216223
}
217224
}
218225
}
219226

220227
#[inline]
221228
fn write_directives<'a, T: Text<'a>>(&mut self, dirs: &[Directive<'a, T>]) {
222229
for dir in dirs {
223-
self.write_punctuator("@");
230+
self.write_punctuator_char('@');
224231
self.write_non_punctuator(dir.name.as_ref());
225232
self.write_arguments(&dir.arguments);
226233
}
227234
}
228235

236+
#[inline]
229237
fn write_arguments<'a, T: Text<'a>>(&mut self, args: &[(T::Value, Value<'a, T>)]) {
230238
if args.is_empty() {
231239
return;
232240
}
233-
self.write_punctuator("(");
241+
self.write_punctuator_char('(');
234242
for (name, val) in args {
235243
self.write_non_punctuator(name.as_ref());
236-
self.write_punctuator(":");
244+
self.write_punctuator_char(':');
237245
self.write_value(val);
238246
}
239-
self.write_punctuator(")");
247+
self.write_punctuator_char(')');
240248
}
241249

242250
#[inline(always)]
@@ -250,20 +258,19 @@ impl Minifier {
250258
let mut needs_escaping = false;
251259

252260
for &byte in bytes {
253-
match byte {
254-
b'\n' => has_newline = true,
255-
b'"' | b'\\' | b'\r' | b'\t' => needs_escaping = true,
256-
0..=0x1F | 0x7F => needs_escaping = true, // Control chars
257-
_ => {}
261+
if byte == b'\n' {
262+
has_newline = true;
263+
} else if byte == b'"' || byte == b'\\' || byte < 0x20 || byte == 0x7F {
264+
needs_escaping = true;
258265
}
259266

260-
// Early exit if we found everything we need to know
261267
if has_newline && needs_escaping {
262268
break;
263269
}
264270
}
265271

266272
if !needs_escaping && !has_newline {
273+
self.buf.reserve(s.len() + 2);
267274
self.buf.push('"');
268275
self.buf.push_str(s);
269276
self.buf.push('"');
@@ -273,6 +280,7 @@ impl Minifier {
273280

274281
if !has_newline {
275282
use std::fmt::Write;
283+
self.buf.reserve(s.len() + 16);
276284
self.buf.push('"');
277285
for c in s.chars() {
278286
match c {
@@ -325,7 +333,7 @@ impl Minifier {
325333
fn write_value<'a, T: Text<'a>>(&mut self, val: &Value<'a, T>) {
326334
match val {
327335
Value::Variable(name) => {
328-
self.write_punctuator("$");
336+
self.write_punctuator_char('$');
329337
self.write_non_punctuator(name.as_ref());
330338
}
331339
Value::Int(n) => {
@@ -350,20 +358,20 @@ impl Minifier {
350358
Value::Null => self.write_non_punctuator("null"),
351359
Value::Enum(name) => self.write_non_punctuator(name.as_ref()),
352360
Value::List(items) => {
353-
self.write_punctuator("[");
361+
self.write_punctuator_char('[');
354362
for item in items {
355363
self.write_value(item);
356364
}
357-
self.write_punctuator("]");
365+
self.write_punctuator_char(']');
358366
}
359367
Value::Object(fields) => {
360-
self.write_punctuator("{");
368+
self.write_punctuator_char('{');
361369
for (name, val) in fields {
362370
self.write_non_punctuator(name.as_ref());
363-
self.write_punctuator(":");
371+
self.write_punctuator_char(':');
364372
self.write_value(val);
365373
}
366-
self.write_punctuator("}");
374+
self.write_punctuator_char('}');
367375
}
368376
}
369377
}

0 commit comments

Comments
 (0)