Bug 1942239 - Add option to explicitly enable incremental origin initialization in...
[gecko.git] / toolkit / components / telemetry / build_scripts / gen_userinteraction_phf.py
blobf1c7256414363177923668ea33f7cadbbb3063e0
1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 from mozparsers.shared_telemetry_utils import ParserError
6 from perfecthash import PerfectHash
8 PHFSIZE = 1024
10 import sys
12 from mozparsers import parse_user_interactions
14 banner = """/* This file is auto-generated, see gen_userinteraction_phf.py. */
15 """
17 header = """
18 #ifndef mozilla_TelemetryUserInteractionNameMap_h
19 #define mozilla_TelemetryUserInteractionNameMap_h
21 #include "mozilla/PerfectHash.h"
23 namespace mozilla {
24 namespace Telemetry {
25 """
27 footer = """
28 } // namespace mozilla
29 } // namespace Telemetry
30 #endif // mozilla_TelemetryUserInteractionNameMap_h
31 """
34 def main(output, *filenames):
35 """
36 Generate a Perfect Hash Table for the UserInteraction name -> UserInteraction ID lookup.
37 The table is immutable once generated and we can avoid any dynamic memory allocation.
38 """
40 output.write(banner)
41 output.write(header)
43 try:
44 user_interactions = list(parse_user_interactions.from_files(filenames))
45 except ParserError as ex:
46 print("\nError processing UserInteractions:\n" + str(ex) + "\n")
47 sys.exit(1)
49 user_interactions = [
50 (bytearray(ui.label, "ascii"), idx)
51 for (idx, ui) in enumerate(user_interactions)
53 name_phf = PerfectHash(user_interactions, PHFSIZE)
55 output.write(
56 name_phf.cxx_codegen(
57 name="UserInteractionIDByNameLookup",
58 entry_type="uint32_t",
59 lower_entry=lambda x: str(x[1]),
60 key_type="const nsACString&",
61 key_bytes="aKey.BeginReading()",
62 key_length="aKey.Length()",
66 output.write(footer)
69 if __name__ == "__main__":
70 main(sys.stdout, *sys.argv[1:])