You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Based on timing test on msg312733, StringTranslatePseudoMappingTest and a defaultdict have about the same performance, so this replaces the custom class with the stdlib functionality.
changed the title [-]IDLE: pyparse - replace StringTranslatePseudoMappingTest with defaultdict[/-][+]IDLE: pyparse - replace StringTranslatePseudoMapping with defaultdict[/+]on Feb 24, 2018
Tal had written this on the original bpo-21765:
----
Finally, since the defaultdict is kept around as long as IDLE is running, I decided to avoid having it grow continually and consume memory unnecessarily. So I wrote a simple Mapping class, which wraps a normal dict and uses a custom default value instead of None, ord('x') in this case. Works like a charm :)
-----
So maybe I misunderstood and this shouldn't be a defaultdict?
I forget about this defaultdict behavior: "this value is inserted in the dictionary for the key, and returned." Reason: when default_factory returns a mutable, d[key] must return the same possibly mutated object with each call. I agree that defaultdict is not the right replacement.
We need to pass to str.translate a dict that can be used by subscripting, newchar = d[char]. So partial(non-defaults.get, default_value) will not work. Instead, we need a __getitem__ that returns the same.
In msg312444 I suggested simplifying STPM (including the name) because it has unneeded complexity. Remove the buggy .get override. Combine the _get stuff in __init__ (also removed) with current __getitem__ and simplify and we get what we actually need (untested, at yet).
changed the title [-]IDLE: pyparse - replace StringTranslatePseudoMapping with defaultdict[/-][+]IDLE: pyparse - simplify StringTranslatePseudoMapping[/+]on Feb 24, 2018
A similar regular expression version was mentioned on bpo-21765 and I had run some tests on it yesterday to verify. On my system, it ran at a factor of 10x slower, so if the translate finished in 0.003, the regex took 0.03. This was consistent for me, regardless of how big I made the document.
The reason for not using a defauldict was to keep the 'x' mappings out of the dictionary so that it wouldn't grow and take up space. Although, I did realize yesterday that it wasn't really boundless because most values in source code would be ASCII. Running both the version the doesn't add the 'x' mappings and the fromkeys, there doesn't seem to be much of a difference in time when processing the doc.
ParseGet hit miss
1.104342376
1.112531999
ParseMis hit miss
0.3530207070000002
1.2165967760000003
ParseGet hit miss
1.185322191
1.1915449519999999
ParseMis hit miss
0.3477272720000002
1.317010653
Avoiding custom code for all ascii chars will be a win. I am sure that calling __missing__ for non-ascii will be at least as fast as it is presently. I will commit a revision tomorrow.
I may then compare to Serhiy's sub/replace suggestion. My experiments with 'code.translate(tran)' indicate that time grows sub-linearly up to 1000 or 10000 chars. This suggests that there are significant constant or log-like terms.
Replacing an expression with a less clear equivalent expression makes no sense to me. Anyway, having __missing__ return 120 reduces the benchmark miss time from 1.2-1.3 to .93, making ParseMis always faster than ParseGet and reducing the penalty for non-ascii chars.
The mapping passed to str.translate must map ints representing codepoints to either either ints or strings. Translate can extract binary codepoints for the new string from either. Ints are slightly faster, so I am inclined not to switch.
New changeset f0daa88 by Terry Jan Reedy (Cheryl Sabella) in branch 'master': bpo-32940: IDLE: Simplify StringTranslatePseudoMapping in pyparse (GH-5862) f0daa88
Activity
csabella commentedon Feb 24, 2018
Based on timing test on msg312733, StringTranslatePseudoMappingTest and a defaultdict have about the same performance, so this replaces the custom class with the stdlib functionality.
[-]IDLE: pyparse - replace StringTranslatePseudoMappingTest with defaultdict[/-][+]IDLE: pyparse - replace StringTranslatePseudoMapping with defaultdict[/+]csabella commentedon Feb 24, 2018
Tal had written this on the original bpo-21765:
----
Finally, since the defaultdict is kept around as long as IDLE is running, I decided to avoid having it grow continually and consume memory unnecessarily. So I wrote a simple Mapping class, which wraps a normal dict and uses a custom default value instead of None, ord('x') in this case. Works like a charm :)
-----
So maybe I misunderstood and this shouldn't be a defaultdict?
terryjreedy commentedon Feb 24, 2018
I forget about this defaultdict behavior: "this value is inserted in the dictionary for the key, and returned." Reason: when default_factory returns a mutable, d[key] must return the same possibly mutated object with each call. I agree that defaultdict is not the right replacement.
We need to pass to str.translate a dict that can be used by subscripting, newchar = d[char]. So partial(non-defaults.get, default_value) will not work. Instead, we need a __getitem__ that returns the same.
In msg312444 I suggested simplifying STPM (including the name) because it has unneeded complexity. Remove the buggy .get override. Combine the _get stuff in __init__ (also removed) with current __getitem__ and simplify and we get what we actually need (untested, at yet).
def __getitem__
return self._non_defaults.get(self._default_value)
Actually, we could hard-code the default value as 'X' as we never need anything else.
How about ParseMap for the name?
[-]IDLE: pyparse - replace StringTranslatePseudoMapping with defaultdict[/-][+]IDLE: pyparse - simplify StringTranslatePseudoMapping[/+]csabella commentedon Feb 25, 2018
New PR submitted. Really glad I worked on this today. I learned a lot of things that were new to me. :-)
terryjreedy commentedon Feb 25, 2018
I simplified ParseMap to a dict subclass with one override -- __getitem__ and then the tests. They run faster. I suspect translate is faster.
serhiy-storchaka commentedon Feb 25, 2018
For efficiency I suggest to initialize the mapping with dict.fromkeys(range(128), 'x') rather of an empty dict.
It is also possible to use regular expressions:
I didn't check what way is more efficient.
terryjreedy commentedon Feb 25, 2018
To me, it is plausible but not slam-dunk obvious that preloading ascii to 'x' mappings will make ascii lookup faster.
On bpo-21765, where the pyparse special translation was a side-issue, Tal Einat claimed that the unpublished regex he tried was 100x slower.
csabella commentedon Feb 25, 2018
A similar regular expression version was mentioned on bpo-21765 and I had run some tests on it yesterday to verify. On my system, it ran at a factor of 10x slower, so if the translate finished in 0.003, the regex took 0.03. This was consistent for me, regardless of how big I made the document.
The reason for not using a defauldict was to keep the 'x' mappings out of the dictionary so that it wouldn't grow and take up space. Although, I did realize yesterday that it wasn't really boundless because most values in source code would be ASCII. Running both the version the doesn't add the 'x' mappings and the
fromkeys, there doesn't seem to be much of a difference in time when processing the doc.terryjreedy commentedon Feb 28, 2018
I settled on the following to compare ParseMap implementations.
ParseGet hit miss
1.104342376
1.112531999
ParseMis hit miss
0.3530207070000002
1.2165967760000003
ParseGet hit miss
1.185322191
1.1915449519999999
ParseMis hit miss
0.3477272720000002
1.317010653
Avoiding custom code for all ascii chars will be a win. I am sure that calling __missing__ for non-ascii will be at least as fast as it is presently. I will commit a revision tomorrow.
I may then compare to Serhiy's sub/replace suggestion. My experiments with 'code.translate(tran)' indicate that time grows sub-linearly up to 1000 or 10000 chars. This suggests that there are significant constant or log-like terms.
serhiy-storchaka commentedon Feb 28, 2018
Don't use ord('x'). Use just 'x' or b'x'[0].
terryjreedy commentedon Feb 28, 2018
Replacing an expression with a less clear equivalent expression makes no sense to me. Anyway, having __missing__ return 120 reduces the benchmark miss time from 1.2-1.3 to .93, making ParseMis always faster than ParseGet and reducing the penalty for non-ascii chars.
re.sub + str.replace is slower than translate
print(timeit.timeit(
'code.translate(trans)',
number=10000, globals = globals()))
print(timeit.timeit(
"code1 = trans_re.sub('x', code)\n"
"code2 = code1.replace('{', '(')\n"
"code3 = code2.replace('}', ')')\n"
"code4 = code3.replace('[', '(')\n"
"code5 = code4.replace(']', '(')\n"
r"code6 = code5.replace('\nx', '\n')",
number=10000, globals = globals()))
n trans re
1 .06 .09
10 .08 .17
100 .28 1.00
1000 2.2 8.9
Multiply by 100 to get microseconds or seconds for 1000000.
terryjreedy commentedon Feb 28, 2018
The mapping passed to str.translate must map ints representing codepoints to either either ints or strings. Translate can extract binary codepoints for the new string from either. Ints are slightly faster, so I am inclined not to switch.
N 10 [0.076474600, 0.076463227, 0.076560984, 0.076581582, 0.076010091]
S 10 [0.080739106, 0.080798745, 0.08034192, 0.080987501, 0.080617369]
N 100 [0.28529922, 0.28383868, 0.283949046, 0.284461512, 0.284291203]
S 100 [0.289629521, 0.288535418, 0.289154560, 0.28811548, 0.28862180]
N1000 [2.23882157, 2.2383192, 2.2384120, 2.2377972, 2.23854982]
S1000 [2.24242237, 2.2426845, 2.2424623, 2.2420030, 2.24254871]
The pattern of all S repeats being greater than all corresponding N repeats was true for 2 other runs.
terryjreedy commentedon Feb 28, 2018
New changeset f0daa88 by Terry Jan Reedy (Cheryl Sabella) in branch 'master':
bpo-32940: IDLE: Simplify StringTranslatePseudoMapping in pyparse (GH-5862)
f0daa88
miss-islington commentedon Feb 28, 2018
New changeset 7e57634 by Miss Islington (bot) in branch '3.7':
bpo-32940: IDLE: Simplify StringTranslatePseudoMapping in pyparse (GH-5862)
7e57634
miss-islington commentedon Feb 28, 2018
New changeset 32f5392 by Miss Islington (bot) in branch '3.6':
bpo-32940: IDLE: Simplify StringTranslatePseudoMapping in pyparse (GH-5862)
32f5392
erlend-aasland commentedon Jul 27, 2022
Are there more work to be done here, or can we close this issue?
terryjreedy commentedon Jul 27, 2022
As near as I can tell now, I tested all proposed alternatives before committing. So all done.