From 5c60023cb8ad6f07927bd53f30c90873d07b300f Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Fri, 27 Sep 2019 06:37:54 +0200 Subject: WebIDL codegen: Replace cmake with a single Python script MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When playing around with Cargo’s new timing visualization: https://internals.rust-lang.org/t/exploring-crate-graph-build-times-with-cargo-build-ztimings/10975/21 … I was surprised to see the `script` crate’s build script take 76 seconds. I did not expect WebIDL bindings generation to be *that* computationally intensive. It turns out almost all of this time is overhead. The build script uses CMake to generate bindings for each WebIDL file in parallel, but that causes a lot of work to be repeated 366 times: * Starting up a Python VM * Importing (parts of) the Python standard library * Importing ~16k lines of our Python code * Recompiling the latter to bytecode, since we used `python -B` to disable writing `.pyc` file * Deserializing with `cPickle` and recreating in memory the results of parsing all WebIDL files ---- This commit remove the use of CMake and cPickle for the `script` crate. Instead, all WebIDL bindings generation is done sequentially in a single Python process. This takes 2 to 3 seconds. --- components/script/dom/bindings/codegen/run.py | 119 ++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 components/script/dom/bindings/codegen/run.py (limited to 'components/script/dom/bindings/codegen/run.py') diff --git a/components/script/dom/bindings/codegen/run.py b/components/script/dom/bindings/codegen/run.py new file mode 100644 index 00000000000..fd43ac32bb7 --- /dev/null +++ b/components/script/dom/bindings/codegen/run.py @@ -0,0 +1,119 @@ +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at https://mozilla.org/MPL/2.0/. + +import os +import sys +import json + + +def main(): + os.chdir(os.path.join(os.path.dirname(__file__))) + sys.path[0:0] = ["./parser", "./ply"] + + css_properties_json, out_dir, doc_servo = sys.argv[1:] + webidls_dir = "../../webidls" + config_file = "Bindings.conf" + + import WebIDL + from Configuration import Configuration + from CodegenRust import CGBindingRoot + + parser = WebIDL.Parser(make_dir(out_dir, "cache")) + webidls = [name for name in os.listdir(webidls_dir) if name.endswith(".webidl")] + for webidl in webidls: + filename = os.path.join(webidls_dir, webidl) + with open(filename, "rb") as f: + parser.parse(f.read(), filename) + + add_css_properties_attributes(css_properties_json, parser) + parser_results = parser.finish() + config = Configuration(config_file, parser_results) + make_dir(out_dir, "Bindings") + + for name, filename in [ + ("PrototypeList", "PrototypeList.rs"), + ("RegisterBindings", "RegisterBindings.rs"), + ("InterfaceObjectMap", "InterfaceObjectMap.rs"), + ("InterfaceObjectMapData", "InterfaceObjectMapData.json"), + ("InterfaceTypes", "InterfaceTypes.rs"), + ("InheritTypes", "InheritTypes.rs"), + ("Bindings", "Bindings/mod.rs"), + ("UnionTypes", "UnionTypes.rs"), + ]: + generate(config, name, os.path.join(out_dir, filename)) + make_dir(doc_servo) + generate(config, "SupportedDomApis", os.path.join(doc_servo, "apis.html")) + + for webidl in webidls: + filename = os.path.join(webidls_dir, webidl) + prefix = "Bindings/%sBinding" % webidl[:-len(".webidl")] + module = CGBindingRoot(config, prefix, filename).define() + if module: + with open(os.path.join(out_dir, prefix + ".rs"), "wb") as f: + f.write(module) + + +def make_dir(out_dir, nested=""): + path = os.path.join(out_dir, nested) + if not os.path.exists(path): + os.makedirs(path) + return path + + +def generate(config, name, filename): + from CodegenRust import GlobalGenRoots + root = getattr(GlobalGenRoots, name)(config) + code = root.define() + with open(filename, "wb") as f: + f.write(code) + + +def add_css_properties_attributes(css_properties_json, parser): + css_properties = json.load(open(css_properties_json, "rb")) + idl = "partial interface CSSStyleDeclaration {\n%s\n};\n" % "\n".join( + " [%sCEReactions, SetterThrows] attribute [TreatNullAs=EmptyString] DOMString %s;" % ( + ('Pref="%s", ' % data["pref"] if data["pref"] else ""), + attribute_name + ) + for (kind, properties_list) in sorted(css_properties.items()) + for (property_name, data) in sorted(properties_list.items()) + for attribute_name in attribute_names(property_name) + ) + parser.parse(idl.encode("utf-8"), "CSSStyleDeclaration_generated.webidl") + + +def attribute_names(property_name): + # https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-dashed-attribute + if property_name != "float": + yield property_name + else: + yield "_float" + + # https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-camel-cased-attribute + if "-" in property_name: + yield "".join(camel_case(property_name)) + + # https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-webkit-cased-attribute + if property_name.startswith("-webkit-"): + yield "".join(camel_case(property_name), True) + + +# https://drafts.csswg.org/cssom/#css-property-to-idl-attribute +def camel_case(chars, webkit_prefixed=False): + if webkit_prefixed: + chars = chars[1:] + next_is_uppercase = False + for c in chars: + if c == '-': + next_is_uppercase = True + elif next_is_uppercase: + next_is_uppercase = False + # Should be ASCII-uppercase, but all non-custom CSS property names are within ASCII + yield c.upper() + else: + yield c + + +if __name__ == "__main__": + main() -- cgit v1.2.3 From d2c299a6c79386fe91f3930914d1d3e7162112a3 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Mon, 30 Sep 2019 11:20:41 +0200 Subject: =?UTF-8?q?Don=E2=80=99t=20rely=20on=20`$CARGO=5FTARGET=5FDIR`=20i?= =?UTF-8?q?n=20build=20scripts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/script/dom/bindings/codegen/run.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'components/script/dom/bindings/codegen/run.py') diff --git a/components/script/dom/bindings/codegen/run.py b/components/script/dom/bindings/codegen/run.py index fd43ac32bb7..130d35e5268 100644 --- a/components/script/dom/bindings/codegen/run.py +++ b/components/script/dom/bindings/codegen/run.py @@ -11,7 +11,8 @@ def main(): os.chdir(os.path.join(os.path.dirname(__file__))) sys.path[0:0] = ["./parser", "./ply"] - css_properties_json, out_dir, doc_servo = sys.argv[1:] + css_properties_json, out_dir = sys.argv[1:] + doc_servo = "../../../../../target/doc/servo" webidls_dir = "../../webidls" config_file = "Bindings.conf" @@ -19,7 +20,7 @@ def main(): from Configuration import Configuration from CodegenRust import CGBindingRoot - parser = WebIDL.Parser(make_dir(out_dir, "cache")) + parser = WebIDL.Parser(make_dir(os.path.join(out_dir, "cache"))) webidls = [name for name in os.listdir(webidls_dir) if name.endswith(".webidl")] for webidl in webidls: filename = os.path.join(webidls_dir, webidl) @@ -29,7 +30,7 @@ def main(): add_css_properties_attributes(css_properties_json, parser) parser_results = parser.finish() config = Configuration(config_file, parser_results) - make_dir(out_dir, "Bindings") + make_dir(os.path.join(out_dir, "Bindings")) for name, filename in [ ("PrototypeList", "PrototypeList.rs"), @@ -54,8 +55,7 @@ def main(): f.write(module) -def make_dir(out_dir, nested=""): - path = os.path.join(out_dir, nested) +def make_dir(path): if not os.path.exists(path): os.makedirs(path) return path -- cgit v1.2.3 From a627dde0d01e35a1cbdb62ca19ee0349757c34b0 Mon Sep 17 00:00:00 2001 From: Vincent Ricard Date: Mon, 28 Dec 2020 22:31:49 +0100 Subject: Port some code to Python3 --- components/script/dom/bindings/codegen/run.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'components/script/dom/bindings/codegen/run.py') diff --git a/components/script/dom/bindings/codegen/run.py b/components/script/dom/bindings/codegen/run.py index 130d35e5268..7f58de15d69 100644 --- a/components/script/dom/bindings/codegen/run.py +++ b/components/script/dom/bindings/codegen/run.py @@ -52,7 +52,7 @@ def main(): module = CGBindingRoot(config, prefix, filename).define() if module: with open(os.path.join(out_dir, prefix + ".rs"), "wb") as f: - f.write(module) + f.write(module.encode("utf-8")) def make_dir(path): @@ -66,7 +66,7 @@ def generate(config, name, filename): root = getattr(GlobalGenRoots, name)(config) code = root.define() with open(filename, "wb") as f: - f.write(code) + f.write(code.encode("utf-8")) def add_css_properties_attributes(css_properties_json, parser): -- cgit v1.2.3