Skip to content

Commit 4a6107e

Browse files
committed
Cache reserved keywords
1 parent fbe005f commit 4a6107e

File tree

9 files changed

+85
-15
lines changed

9 files changed

+85
-15
lines changed

naga/src/back/glsl/keywords.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
use std::sync::LazyLock;
2+
3+
use hashbrown::HashSet;
4+
15
pub const RESERVED_KEYWORDS: &[&str] = &[
26
//
37
// GLSL 4.6 keywords, from https://github.com/KhronosGroup/OpenGL-Registry/blob/d00e11dc1a1ffba581d633f21f70202051248d5c/specs/gl/GLSLangSpec.4.60.html#L2004-L2322
@@ -490,3 +494,14 @@ pub const RESERVED_KEYWORDS: &[&str] = &[
490494
super::FREXP_FUNCTION,
491495
super::FIRST_INSTANCE_BINDING,
492496
];
497+
498+
/// The above set of reserved keywords, turned into a cached HashSet. This saves
499+
/// significant time during [`Namer::reset`](crate::proc::Namer::reset).
500+
pub static RESERVED_KEYWORD_SET: LazyLock<HashSet<&'static str>> = LazyLock::new(|| {
501+
let mut set = HashSet::default();
502+
set.reserve(RESERVED_KEYWORDS.len());
503+
for &word in RESERVED_KEYWORDS {
504+
set.insert(word);
505+
}
506+
set
507+
});

naga/src/back/glsl/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -633,8 +633,7 @@ impl<'a, W: Write> Writer<'a, W> {
633633
let mut namer = proc::Namer::default();
634634
namer.reset(
635635
module,
636-
keywords::RESERVED_KEYWORDS,
637-
&[],
636+
&keywords::RESERVED_KEYWORD_SET,
638637
&[],
639638
&[
640639
"gl_", // all GL built-in variables

naga/src/back/hlsl/keywords.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
use std::sync::LazyLock;
2+
3+
use hashbrown::HashSet;
4+
15
// When compiling with FXC without strict mode, these keywords are actually case insensitive.
26
// If you compile with strict mode and specify a different casing like "Pass" instead in an identifier, FXC will give this error:
37
// "error X3086: alternate cases for 'pass' are deprecated in strict mode"
@@ -912,6 +916,20 @@ pub const TYPES: &[&str] = &{
912916
res
913917
};
914918

919+
/// The above set of reserved keywords, turned into a cached HashSet. This saves
920+
/// significant time during [`Namer::reset`](crate::proc::Namer::reset).
921+
pub static RESERVED_SET: LazyLock<HashSet<&'static str>> = LazyLock::new(|| {
922+
let mut set = HashSet::default();
923+
set.reserve(RESERVED.len() + TYPES.len());
924+
for &word in RESERVED {
925+
set.insert(word);
926+
}
927+
for &word in TYPES {
928+
set.insert(word);
929+
}
930+
set
931+
});
932+
915933
pub const RESERVED_PREFIXES: &[&str] = &[
916934
"__dynamic_buffer_offsets",
917935
super::help::IMAGE_STORAGE_LOAD_SCALAR_WRAPPER,

naga/src/back/hlsl/writer.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,7 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> {
140140
self.names.clear();
141141
self.namer.reset(
142142
module,
143-
super::keywords::RESERVED,
144-
super::keywords::TYPES,
143+
&super::keywords::RESERVED_SET,
145144
super::keywords::RESERVED_CASE_INSENSITIVE,
146145
super::keywords::RESERVED_PREFIXES,
147146
&mut self.names,

naga/src/back/msl/keywords.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
use std::sync::LazyLock;
2+
3+
use hashbrown::HashSet;
4+
15
// MSLS - Metal Shading Language Specification:
26
// https://developer.apple.com/metal/Metal-Shading-Language-Specification.pdf
37
//
@@ -347,3 +351,14 @@ pub const RESERVED: &[&str] = &[
347351
super::writer::NEG_FUNCTION,
348352
super::writer::ARGUMENT_BUFFER_WRAPPER_STRUCT,
349353
];
354+
355+
/// The above set of reserved keywords, turned into a cached HashSet. This saves
356+
/// significant time during [`Namer::reset`](crate::proc::Namer::reset).
357+
pub static RESERVED_SET: LazyLock<HashSet<&'static str>> = LazyLock::new(|| {
358+
let mut set = HashSet::default();
359+
set.reserve(RESERVED.len());
360+
for &word in RESERVED {
361+
set.insert(word);
362+
}
363+
set
364+
});

naga/src/back/msl/writer.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3804,8 +3804,7 @@ impl<W: Write> Writer<W> {
38043804
self.names.clear();
38053805
self.namer.reset(
38063806
module,
3807-
super::keywords::RESERVED,
3808-
&[],
3807+
&super::keywords::RESERVED_SET,
38093808
&[],
38103809
&[CLAMPED_LOD_LOAD_PREFIX],
38113810
&mut self.names,

naga/src/back/wgsl/writer.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,9 @@ impl<W: Write> Writer<W> {
9999
self.names.clear();
100100
self.namer.reset(
101101
module,
102-
crate::keywords::wgsl::RESERVED,
102+
&crate::keywords::wgsl::RESERVED_SET,
103103
// an identifier must not start with two underscore
104104
&[],
105-
&[],
106105
&["__", "_naga"],
107106
&mut self.names,
108107
);

naga/src/keywords/wgsl.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ Keywords for [WGSL][wgsl] (WebGPU Shading Language).
44
[wgsl]: https://gpuweb.github.io/gpuweb/wgsl.html
55
*/
66

7+
use std::sync::LazyLock;
8+
9+
use hashbrown::HashSet;
10+
711
// https://gpuweb.github.io/gpuweb/wgsl/#keyword-summary
812
// last sync: https://github.com/gpuweb/gpuweb/blob/39f2321f547c8f0b7f473cf1d47fba30b1691303/wgsl/index.bs
913
pub const RESERVED: &[&str] = &[
@@ -229,3 +233,14 @@ pub const RESERVED: &[&str] = &[
229233
"writeonly",
230234
"yield",
231235
];
236+
237+
/// The above set of reserved keywords, turned into a cached HashSet. This saves
238+
/// significant time during [`Namer::reset`](crate::proc::Namer::reset).
239+
pub static RESERVED_SET: LazyLock<HashSet<&'static str>> = LazyLock::new(|| {
240+
let mut set = HashSet::default();
241+
set.reserve(RESERVED.len());
242+
for &word in RESERVED {
243+
set.insert(word);
244+
}
245+
set
246+
});

naga/src/proc/namer.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ use alloc::{
55
vec::Vec,
66
};
77
use core::hash::{Hash, Hasher};
8+
use hashbrown::HashSet;
9+
use std::sync::LazyLock;
810

911
use crate::{arena::Handle, FastHashMap, FastHashSet};
1012

@@ -27,15 +29,27 @@ pub enum NameKey {
2729

2830
/// This processor assigns names to all the things in a module
2931
/// that may need identifiers in a textual backend.
30-
#[derive(Default)]
3132
pub struct Namer {
3233
/// The last numeric suffix used for each base name. Zero means "no suffix".
3334
unique: FastHashMap<String, u32>,
34-
keywords: FastHashSet<&'static str>,
35+
keywords: &'static HashSet<&'static str>,
3536
keywords_case_insensitive: FastHashSet<AsciiUniCase<&'static str>>,
3637
reserved_prefixes: Vec<&'static str>,
3738
}
3839

40+
impl Default for Namer {
41+
fn default() -> Self {
42+
static DEFAULT_KEYWORDS: LazyLock<HashSet<&'static str>> = LazyLock::new(HashSet::default);
43+
44+
Self {
45+
unique: Default::default(),
46+
keywords: &DEFAULT_KEYWORDS,
47+
keywords_case_insensitive: Default::default(),
48+
reserved_prefixes: Default::default(),
49+
}
50+
}
51+
}
52+
3953
impl Namer {
4054
/// Return a form of `string` suitable for use as the base of an identifier.
4155
///
@@ -157,8 +171,7 @@ impl Namer {
157171
pub fn reset(
158172
&mut self,
159173
module: &crate::Module,
160-
reserved_keywords: &[&'static str],
161-
extra_reserved_keywords: &[&'static str],
174+
reserved_keywords: &'static HashSet<&'static str>,
162175
reserved_keywords_case_insensitive: &[&'static str],
163176
reserved_prefixes: &[&'static str],
164177
output: &mut FastHashMap<NameKey, String>,
@@ -167,9 +180,7 @@ impl Namer {
167180
self.reserved_prefixes.extend(reserved_prefixes.iter());
168181

169182
self.unique.clear();
170-
self.keywords.clear();
171-
self.keywords.extend(reserved_keywords.iter());
172-
self.keywords.extend(extra_reserved_keywords.iter());
183+
self.keywords = reserved_keywords;
173184

174185
debug_assert!(reserved_keywords_case_insensitive
175186
.iter()

0 commit comments

Comments
 (0)