Skip to content

Commit 1d4d4dc

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 * adds new public convenience helpers `keys()` and `values()` * adds new public methods which i'm not sure belong but have left in for now incase they are deemed useful. * `getPartial(str)`, `getIndexPartial(str)`, `initRuntime(allocator)`, `deinit(allocator)` * includes test coverage for all * performance notes: * i posted some benchmarking results here: https://github.com/travisstaloch/comptime-string-map-revised/issues/1 * i noticed 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 poop output in link above.
1 parent c352845 commit 1d4d4dc

25 files changed

+593
-374
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: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub const Resource = enum {
4747
fontdir_num,
4848
manifest_num,
4949

50-
const map = std.ComptimeStringMapWithEql(Resource, .{
50+
const map = std.StaticStringMapWithEql(Resource).initComptime(.{
5151
.{ "ACCELERATORS", .accelerators },
5252
.{ "BITMAP", .bitmap },
5353
.{ "CURSOR", .cursor },
@@ -157,13 +157,13 @@ pub const OptionalStatements = enum {
157157
menu,
158158
style,
159159

160-
pub const map = std.ComptimeStringMapWithEql(OptionalStatements, .{
160+
pub const map = std.StaticStringMapWithEql(OptionalStatements).initComptime(.{
161161
.{ "CHARACTERISTICS", .characteristics },
162162
.{ "LANGUAGE", .language },
163163
.{ "VERSION", .version },
164164
}, std.comptime_string_map.eqlAsciiIgnoreCase);
165165

166-
pub const dialog_map = std.ComptimeStringMapWithEql(OptionalStatements, .{
166+
pub const dialog_map = std.StaticStringMapWithEql(OptionalStatements).initComptime(.{
167167
.{ "CAPTION", .caption },
168168
.{ "CLASS", .class },
169169
.{ "EXSTYLE", .exstyle },
@@ -197,7 +197,7 @@ pub const Control = enum {
197197
state3,
198198
userbutton,
199199

200-
pub const map = std.ComptimeStringMapWithEql(Control, .{
200+
pub const map = std.StaticStringMapWithEql(Control).initComptime(.{
201201
.{ "AUTO3STATE", .auto3state },
202202
.{ "AUTOCHECKBOX", .autocheckbox },
203203
.{ "AUTORADIOBUTTON", .autoradiobutton },
@@ -231,7 +231,7 @@ pub const Control = enum {
231231
};
232232

233233
pub const ControlClass = struct {
234-
pub const map = std.ComptimeStringMapWithEql(res.ControlClass, .{
234+
pub const map = std.StaticStringMapWithEql(res.ControlClass).initComptime(.{
235235
.{ "BUTTON", .button },
236236
.{ "EDIT", .edit },
237237
.{ "STATIC", .static },
@@ -280,7 +280,7 @@ pub const MenuItem = enum {
280280
menuitem,
281281
popup,
282282

283-
pub const map = std.ComptimeStringMapWithEql(MenuItem, .{
283+
pub const map = std.StaticStringMapWithEql(MenuItem).initComptime(.{
284284
.{ "MENUITEM", .menuitem },
285285
.{ "POPUP", .popup },
286286
}, std.comptime_string_map.eqlAsciiIgnoreCase);
@@ -297,7 +297,7 @@ pub const MenuItem = enum {
297297
menubarbreak,
298298
menubreak,
299299

300-
pub const map = std.ComptimeStringMapWithEql(Option, .{
300+
pub const map = std.StaticStringMapWithEql(Option).initComptime(.{
301301
.{ "CHECKED", .checked },
302302
.{ "GRAYED", .grayed },
303303
.{ "HELP", .help },
@@ -312,7 +312,7 @@ pub const ToolbarButton = enum {
312312
button,
313313
separator,
314314

315-
pub const map = std.ComptimeStringMapWithEql(ToolbarButton, .{
315+
pub const map = std.StaticStringMapWithEql(ToolbarButton).initComptime(.{
316316
.{ "BUTTON", .button },
317317
.{ "SEPARATOR", .separator },
318318
}, std.comptime_string_map.eqlAsciiIgnoreCase);
@@ -327,7 +327,7 @@ pub const VersionInfo = enum {
327327
file_type,
328328
file_subtype,
329329

330-
pub const map = std.ComptimeStringMapWithEql(VersionInfo, .{
330+
pub const map = std.StaticStringMapWithEql(VersionInfo).initComptime(.{
331331
.{ "FILEVERSION", .file_version },
332332
.{ "PRODUCTVERSION", .product_version },
333333
.{ "FILEFLAGSMASK", .file_flags_mask },
@@ -342,7 +342,7 @@ pub const VersionBlock = enum {
342342
block,
343343
value,
344344

345-
pub const map = std.ComptimeStringMapWithEql(VersionBlock, .{
345+
pub const map = std.StaticStringMapWithEql(VersionBlock).initComptime(.{
346346
.{ "BLOCK", .block },
347347
.{ "VALUE", .value },
348348
}, std.comptime_string_map.eqlAsciiIgnoreCase);
@@ -356,7 +356,7 @@ pub const TopLevelKeywords = enum {
356356
characteristics,
357357
stringtable,
358358

359-
pub const map = std.ComptimeStringMapWithEql(TopLevelKeywords, .{
359+
pub const map = std.StaticStringMapWithEql(TopLevelKeywords).initComptime(.{
360360
.{ "LANGUAGE", .language },
361361
.{ "VERSION", .version },
362362
.{ "CHARACTERISTICS", .characteristics },
@@ -375,7 +375,7 @@ pub const CommonResourceAttributes = enum {
375375
shared,
376376
nonshared,
377377

378-
pub const map = std.ComptimeStringMapWithEql(CommonResourceAttributes, .{
378+
pub const map = std.StaticStringMapWithEql(CommonResourceAttributes).initComptime(.{
379379
.{ "PRELOAD", .preload },
380380
.{ "LOADONCALL", .loadoncall },
381381
.{ "FIXED", .fixed },
@@ -396,7 +396,7 @@ pub const AcceleratorTypeAndOptions = enum {
396396
shift,
397397
control,
398398

399-
pub const map = std.ComptimeStringMapWithEql(AcceleratorTypeAndOptions, .{
399+
pub const map = std.StaticStringMapWithEql(AcceleratorTypeAndOptions).initComptime(.{
400400
.{ "VIRTKEY", .virtkey },
401401
.{ "ASCII", .ascii },
402402
.{ "NOINVERT", .noinvert },

0 commit comments

Comments
 (0)