Improve YAML loading performance using libyaml

This adds optional support for loading the YAML reflection data using
`libyaml`, which is faster than PyYAML's pure Python implementation.
This commit is contained in:
Dunedan 2024-10-17 17:52:08 +02:00
parent 92df50f36c
commit b2cdb1e6b4
No known key found for this signature in database
GPG key ID: 885B16854284E0B2
2 changed files with 14 additions and 1 deletions

View file

@ -7,6 +7,7 @@
```
- Install `glslc` and spirv-tools 2023+ (the easiest way is to install Vulkan SDK)
- For improved performance you may also install [libyaml](https://github.com/yaml/libyaml)
- Run the compile.py script

View file

@ -25,6 +25,7 @@ import hashlib
import itertools
import json
import os
import re
import shutil
import subprocess
import sys
@ -36,6 +37,11 @@ from pathlib import Path
import yaml
try:
from yaml import CSafeLoader as SafeLoader
except ImportError:
from yaml import SafeLoader
STAGE_EXTENSIONS = {
"vertex": ".vs",
"fragment": ".fs",
@ -43,6 +49,8 @@ STAGE_EXTENSIONS = {
"compute": ".cs",
}
YAML_DIRECTIVE_PATTERN = re.compile(b"^%YAML 1.0\n")
def execute(command):
try:
@ -158,8 +166,12 @@ def compile_and_reflect(input_mod_path, dependencies, stage, path, out_path, def
"Error: {}\n".format(ret, " ".join(command), input_path, output_path, err)
)
raise ValueError(err)
# Reflect the result SPIRV.
data = yaml.safe_load(out)
# libyaml doesn't support files with a YAML 1.0 directive, so
# let's strip the directive out before loading the data.
data = yaml.load(YAML_DIRECTIVE_PATTERN.sub(b"", out), Loader=SafeLoader)
module = data["module"]
interface_variables = []
if data.get("all_interface_variables"):