Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename matcher parameters (and attributes?) to match regexes.yaml #215

Closed
masklinn opened this issue Jul 13, 2024 · 1 comment
Closed

Rename matcher parameters (and attributes?) to match regexes.yaml #215

masklinn opened this issue Jul 13, 2024 · 1 comment

Comments

@masklinn
Copy link
Contributor

#213 is updating matchers to make regex the string attribute for matchers (and pattern the compiled regexes), bringing it closer to raw regexes.yaml names.

But that's also a minor issue with every other attribute of these things, they could and should match the source exactly. This would allow simplifying the loaders because then the data from deserialization could be splatted directly into the attributs.

While doing this for codegen was found to be a lot less efficient (#214) this concern should be much less so here, because currently the loader code has to retrieve and apply individual attributes:

matchers.UserAgentMatcher(
p["regex"],
p.get("family_replacement"),
p.get("v1_replacement"),
p.get("v2_replacement"),
p.get("v3_replacement"),
p.get("v4_replacement"),
)

  0           0 RESUME                   0

  1           2 LOAD_NAME                0 (matchers)
              4 LOAD_ATTR                3 (NULL|self + UserAgentMatcher)

  2          24 LOAD_NAME                2 (p)
             26 LOAD_CONST               0 ('regex')
             28 BINARY_SUBSCR

  3          32 LOAD_NAME                2 (p)
             34 LOAD_ATTR                7 (NULL|self + get)
             54 LOAD_CONST               1 ('family_replacement')
             56 CALL                     1

  4          64 LOAD_NAME                2 (p)
             66 LOAD_ATTR                7 (NULL|self + get)
             86 LOAD_CONST               2 ('v1_replacement')
             88 CALL                     1

  5          96 LOAD_NAME                2 (p)
             98 LOAD_ATTR                7 (NULL|self + get)
            118 LOAD_CONST               3 ('v2_replacement')
            120 CALL                     1

  6         128 LOAD_NAME                2 (p)
            130 LOAD_ATTR                7 (NULL|self + get)
            150 LOAD_CONST               4 ('v3_replacement')
            152 CALL                     1

  7         160 LOAD_NAME                2 (p)
            162 LOAD_ATTR                7 (NULL|self + get)
            182 LOAD_CONST               5 ('v4_replacement')
            184 CALL                     1

  1         192 CALL                     6
            200 RETURN_VALUE

versus matchers.UserAgentMatcher(**p):

  0           0 RESUME                   0

  1           2 PUSH_NULL
              4 LOAD_NAME                0 (matchers)
              6 LOAD_ATTR                2 (UserAgentMatcher)
             26 LOAD_CONST               0 (())
             28 BUILD_MAP                0
             30 LOAD_NAME                2 (p)
             32 DICT_MERGE               1
             34 CALL_FUNCTION_EX         1
             36 RETURN_VALUE
@masklinn
Copy link
Contributor Author

Welp, turns out this also doesn't really track in the end, even in the worst case scenario the positional version wins handily, sadge.

> python -mtimeit -s 'foo = lambda a, b=None, c=None, d=None, e=None, f=None, g=None: None; d = {"a": "bar"}' 'foo(d["a"], d.get("b"), d.get("c"), d.get("e"), d.get("f"), d.get("g"))'
5000000 loops, best of 5: 96.4 nsec per loop
> python -mtimeit -s 'foo = lambda a, b=None, c=None, d=None, e=None, f=None, g=None: None; d = {"a": "bar"}' 'foo(**d)'
2000000 loops, best of 5: 108 nsec per loop

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant