Skip to content

Commit ea50d26

Browse files
committed
ComptimeStringMap: return a regular struct and optimize
this patch renames ComptimeStringMap to StaticStringMap, makes it accept only a single type parameter, and return a known struct type instead of an anonymous struct. initial motivation for these changes was to reduce the 'very long type names' issue described here #19682. this breaks the previous API. users will now need to write: `const map = std.StaticStringMap(T).initComptime(kvs_list);` * move `kvs_list` param from type param to an `initComptime()` param * new public methods * `keys()`, `values()` helpers * `init(allocator)`, `deinit(allocator)` for runtime data * `getLongestPrefix(str)`, `getLongestPrefixIndex(str)` - i'm not sure these belong but have left in for now incase they are deemed useful * performance notes: * i posted some benchmarking results here: https://github.com/travisstaloch/comptime-string-map-revised/issues/1 * i noticed a speedup reducing the size of the struct from 48 to 32 bytes and thus use u32s instead of usize for all length fields * i noticed speedup storing KVs as a struct of arrays * latest benchmark shows these wall_time improvements for debug/safe/small/fast builds: -6.6% / -10.2% / -19.1% / -8.9%. full output in link above.
1 parent c352845 commit ea50d26

25 files changed

+608
-387
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ set(ZIG_STAGE2_SOURCES
222222
"${CMAKE_SOURCE_DIR}/lib/std/c/linux.zig"
223223
"${CMAKE_SOURCE_DIR}/lib/std/child_process.zig"
224224
"${CMAKE_SOURCE_DIR}/lib/std/coff.zig"
225-
"${CMAKE_SOURCE_DIR}/lib/std/comptime_string_map.zig"
225+
"${CMAKE_SOURCE_DIR}/lib/std/static_string_map.zig"
226226
"${CMAKE_SOURCE_DIR}/lib/std/crypto.zig"
227227
"${CMAKE_SOURCE_DIR}/lib/std/crypto/blake3.zig"
228228
"${CMAKE_SOURCE_DIR}/lib/std/crypto/siphash.zig"

lib/compiler/aro/aro/LangOpts.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub const Standard = enum {
4747
/// Working Draft for ISO C23 with GNU extensions
4848
gnu23,
4949

50-
const NameMap = std.ComptimeStringMap(Standard, .{
50+
const NameMap = std.StaticStringMap(Standard).initComptime(.{
5151
.{ "c89", .c89 }, .{ "c90", .c89 }, .{ "iso9899:1990", .c89 },
5252
.{ "iso9899:199409", .iso9899 }, .{ "gnu89", .gnu89 }, .{ "gnu90", .gnu89 },
5353
.{ "c99", .c99 }, .{ "iso9899:1999", .c99 }, .{ "c9x", .c99 },

lib/compiler/aro/aro/Preprocessor.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1709,7 +1709,7 @@ fn expandFuncMacro(
17091709
}
17101710
if (!pp.comp.langopts.standard.atLeast(.c23)) break :res not_found;
17111711

1712-
const attrs = std.ComptimeStringMap([]const u8, .{
1712+
const attrs = std.StaticStringMap([]const u8).initComptime(.{
17131713
.{ "deprecated", "201904L\n" },
17141714
.{ "fallthrough", "201904L\n" },
17151715
.{ "maybe_unused", "201904L\n" },

lib/compiler/aro/aro/Tokenizer.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -872,7 +872,7 @@ pub const Token = struct {
872872
};
873873
}
874874

875-
const all_kws = std.ComptimeStringMap(Id, .{
875+
const all_kws = std.StaticStringMap(Id).initComptime(.{
876876
.{ "auto", auto: {
877877
@setEvalBranchQuota(3000);
878878
break :auto .keyword_auto;

lib/compiler/resinator/errors.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ pub const ErrorDetails = struct {
240240
// see https://github.com/ziglang/zig/issues/15395
241241
_: u26 = 0,
242242

243-
pub const strings = std.ComptimeStringMap([]const u8, .{
243+
pub const strings = std.StaticStringMap([]const u8).initComptime(.{
244244
.{ "number", "number" },
245245
.{ "number_expression", "number expression" },
246246
.{ "string_literal", "quoted string literal" },

lib/compiler/resinator/rc.zig

Lines changed: 65 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ pub const Resource = enum {
4747
fontdir_num,
4848
manifest_num,
4949

50-
const map = std.ComptimeStringMapWithEql(Resource, .{
50+
const map = std.StaticStringMapWithEql(
51+
Resource,
52+
std.static_string_map.eqlAsciiIgnoreCase,
53+
).initComptime(.{
5154
.{ "ACCELERATORS", .accelerators },
5255
.{ "BITMAP", .bitmap },
5356
.{ "CURSOR", .cursor },
@@ -67,7 +70,7 @@ pub const Resource = enum {
6770
.{ "TOOLBAR", .toolbar },
6871
.{ "VERSIONINFO", .versioninfo },
6972
.{ "VXD", .vxd },
70-
}, std.comptime_string_map.eqlAsciiIgnoreCase);
73+
});
7174

7275
pub fn fromString(bytes: SourceBytes) Resource {
7376
const maybe_ordinal = res.NameOrOrdinal.maybeOrdinalFromString(bytes);
@@ -157,20 +160,26 @@ pub const OptionalStatements = enum {
157160
menu,
158161
style,
159162

160-
pub const map = std.ComptimeStringMapWithEql(OptionalStatements, .{
163+
pub const map = std.StaticStringMapWithEql(
164+
OptionalStatements,
165+
std.static_string_map.eqlAsciiIgnoreCase,
166+
).initComptime(.{
161167
.{ "CHARACTERISTICS", .characteristics },
162168
.{ "LANGUAGE", .language },
163169
.{ "VERSION", .version },
164-
}, std.comptime_string_map.eqlAsciiIgnoreCase);
170+
});
165171

166-
pub const dialog_map = std.ComptimeStringMapWithEql(OptionalStatements, .{
172+
pub const dialog_map = std.StaticStringMapWithEql(
173+
OptionalStatements,
174+
std.static_string_map.eqlAsciiIgnoreCase,
175+
).initComptime(.{
167176
.{ "CAPTION", .caption },
168177
.{ "CLASS", .class },
169178
.{ "EXSTYLE", .exstyle },
170179
.{ "FONT", .font },
171180
.{ "MENU", .menu },
172181
.{ "STYLE", .style },
173-
}, std.comptime_string_map.eqlAsciiIgnoreCase);
182+
});
174183
};
175184

176185
pub const Control = enum {
@@ -197,7 +206,10 @@ pub const Control = enum {
197206
state3,
198207
userbutton,
199208

200-
pub const map = std.ComptimeStringMapWithEql(Control, .{
209+
pub const map = std.StaticStringMapWithEql(
210+
Control,
211+
std.static_string_map.eqlAsciiIgnoreCase,
212+
).initComptime(.{
201213
.{ "AUTO3STATE", .auto3state },
202214
.{ "AUTOCHECKBOX", .autocheckbox },
203215
.{ "AUTORADIOBUTTON", .autoradiobutton },
@@ -220,7 +232,7 @@ pub const Control = enum {
220232
.{ "SCROLLBAR", .scrollbar },
221233
.{ "STATE3", .state3 },
222234
.{ "USERBUTTON", .userbutton },
223-
}, std.comptime_string_map.eqlAsciiIgnoreCase);
235+
});
224236

225237
pub fn hasTextParam(control: Control) bool {
226238
switch (control) {
@@ -231,14 +243,17 @@ pub const Control = enum {
231243
};
232244

233245
pub const ControlClass = struct {
234-
pub const map = std.ComptimeStringMapWithEql(res.ControlClass, .{
246+
pub const map = std.StaticStringMapWithEql(
247+
res.ControlClass,
248+
std.static_string_map.eqlAsciiIgnoreCase,
249+
).initComptime(.{
235250
.{ "BUTTON", .button },
236251
.{ "EDIT", .edit },
237252
.{ "STATIC", .static },
238253
.{ "LISTBOX", .listbox },
239254
.{ "SCROLLBAR", .scrollbar },
240255
.{ "COMBOBOX", .combobox },
241-
}, std.comptime_string_map.eqlAsciiIgnoreCase);
256+
});
242257

243258
/// Like `map.get` but works on WTF16 strings, for use with parsed
244259
/// string literals ("BUTTON", or even "\x42UTTON")
@@ -280,10 +295,13 @@ pub const MenuItem = enum {
280295
menuitem,
281296
popup,
282297

283-
pub const map = std.ComptimeStringMapWithEql(MenuItem, .{
298+
pub const map = std.StaticStringMapWithEql(
299+
MenuItem,
300+
std.static_string_map.eqlAsciiIgnoreCase,
301+
).initComptime(.{
284302
.{ "MENUITEM", .menuitem },
285303
.{ "POPUP", .popup },
286-
}, std.comptime_string_map.eqlAsciiIgnoreCase);
304+
});
287305

288306
pub fn isSeparator(bytes: []const u8) bool {
289307
return std.ascii.eqlIgnoreCase(bytes, "SEPARATOR");
@@ -297,25 +315,31 @@ pub const MenuItem = enum {
297315
menubarbreak,
298316
menubreak,
299317

300-
pub const map = std.ComptimeStringMapWithEql(Option, .{
318+
pub const map = std.StaticStringMapWithEql(
319+
Option,
320+
std.static_string_map.eqlAsciiIgnoreCase,
321+
).initComptime(.{
301322
.{ "CHECKED", .checked },
302323
.{ "GRAYED", .grayed },
303324
.{ "HELP", .help },
304325
.{ "INACTIVE", .inactive },
305326
.{ "MENUBARBREAK", .menubarbreak },
306327
.{ "MENUBREAK", .menubreak },
307-
}, std.comptime_string_map.eqlAsciiIgnoreCase);
328+
});
308329
};
309330
};
310331

311332
pub const ToolbarButton = enum {
312333
button,
313334
separator,
314335

315-
pub const map = std.ComptimeStringMapWithEql(ToolbarButton, .{
336+
pub const map = std.StaticStringMapWithEql(
337+
ToolbarButton,
338+
std.static_string_map.eqlAsciiIgnoreCase,
339+
).initComptime(.{
316340
.{ "BUTTON", .button },
317341
.{ "SEPARATOR", .separator },
318-
}, std.comptime_string_map.eqlAsciiIgnoreCase);
342+
});
319343
};
320344

321345
pub const VersionInfo = enum {
@@ -327,25 +351,31 @@ pub const VersionInfo = enum {
327351
file_type,
328352
file_subtype,
329353

330-
pub const map = std.ComptimeStringMapWithEql(VersionInfo, .{
354+
pub const map = std.StaticStringMapWithEql(
355+
VersionInfo,
356+
std.static_string_map.eqlAsciiIgnoreCase,
357+
).initComptime(.{
331358
.{ "FILEVERSION", .file_version },
332359
.{ "PRODUCTVERSION", .product_version },
333360
.{ "FILEFLAGSMASK", .file_flags_mask },
334361
.{ "FILEFLAGS", .file_flags },
335362
.{ "FILEOS", .file_os },
336363
.{ "FILETYPE", .file_type },
337364
.{ "FILESUBTYPE", .file_subtype },
338-
}, std.comptime_string_map.eqlAsciiIgnoreCase);
365+
});
339366
};
340367

341368
pub const VersionBlock = enum {
342369
block,
343370
value,
344371

345-
pub const map = std.ComptimeStringMapWithEql(VersionBlock, .{
372+
pub const map = std.StaticStringMapWithEql(
373+
VersionBlock,
374+
std.static_string_map.eqlAsciiIgnoreCase,
375+
).initComptime(.{
346376
.{ "BLOCK", .block },
347377
.{ "VALUE", .value },
348-
}, std.comptime_string_map.eqlAsciiIgnoreCase);
378+
});
349379
};
350380

351381
/// Keywords that are be the first token in a statement and (if so) dictate how the rest
@@ -356,12 +386,15 @@ pub const TopLevelKeywords = enum {
356386
characteristics,
357387
stringtable,
358388

359-
pub const map = std.ComptimeStringMapWithEql(TopLevelKeywords, .{
389+
pub const map = std.StaticStringMapWithEql(
390+
TopLevelKeywords,
391+
std.static_string_map.eqlAsciiIgnoreCase,
392+
).initComptime(.{
360393
.{ "LANGUAGE", .language },
361394
.{ "VERSION", .version },
362395
.{ "CHARACTERISTICS", .characteristics },
363396
.{ "STRINGTABLE", .stringtable },
364-
}, std.comptime_string_map.eqlAsciiIgnoreCase);
397+
});
365398
};
366399

367400
pub const CommonResourceAttributes = enum {
@@ -375,7 +408,10 @@ pub const CommonResourceAttributes = enum {
375408
shared,
376409
nonshared,
377410

378-
pub const map = std.ComptimeStringMapWithEql(CommonResourceAttributes, .{
411+
pub const map = std.StaticStringMapWithEql(
412+
CommonResourceAttributes,
413+
std.static_string_map.eqlAsciiIgnoreCase,
414+
).initComptime(.{
379415
.{ "PRELOAD", .preload },
380416
.{ "LOADONCALL", .loadoncall },
381417
.{ "FIXED", .fixed },
@@ -385,7 +421,7 @@ pub const CommonResourceAttributes = enum {
385421
.{ "IMPURE", .impure },
386422
.{ "SHARED", .shared },
387423
.{ "NONSHARED", .nonshared },
388-
}, std.comptime_string_map.eqlAsciiIgnoreCase);
424+
});
389425
};
390426

391427
pub const AcceleratorTypeAndOptions = enum {
@@ -396,12 +432,15 @@ pub const AcceleratorTypeAndOptions = enum {
396432
shift,
397433
control,
398434

399-
pub const map = std.ComptimeStringMapWithEql(AcceleratorTypeAndOptions, .{
435+
pub const map = std.StaticStringMapWithEql(
436+
AcceleratorTypeAndOptions,
437+
std.static_string_map.eqlAsciiIgnoreCase,
438+
).initComptime(.{
400439
.{ "VIRTKEY", .virtkey },
401440
.{ "ASCII", .ascii },
402441
.{ "NOINVERT", .noinvert },
403442
.{ "ALT", .alt },
404443
.{ "SHIFT", .shift },
405444
.{ "CONTROL", .control },
406-
}, std.comptime_string_map.eqlAsciiIgnoreCase);
445+
});
407446
};

0 commit comments

Comments
 (0)