Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Matrix
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Container Registry
Model registry
Operate
Environments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
TeDomum
Matrix
Commits
7af825ba
Commit
7af825ba
authored
8 years ago
by
Erik Johnston
Committed by
GitHub
8 years ago
Browse files
Options
Downloads
Plain Diff
Merge pull request #2155 from matrix-org/erikj/string_intern
Only intern ascii strings
parents
26bcda31
d134d093
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
synapse/util/caches/__init__.py
+11
-18
11 additions, 18 deletions
synapse/util/caches/__init__.py
with
11 additions
and
18 deletions
synapse/util/caches/__init__.py
+
11
−
18
View file @
7af825ba
...
...
@@ -14,7 +14,6 @@
# limitations under the License.
import
synapse.metrics
from
lrucache
import
LruCache
import
os
CACHE_SIZE_FACTOR
=
float
(
os
.
environ
.
get
(
"
SYNAPSE_CACHE_FACTOR
"
,
0.1
))
...
...
@@ -40,10 +39,6 @@ def register_cache(name, cache):
)
_string_cache
=
LruCache
(
int
(
100000
*
CACHE_SIZE_FACTOR
))
_stirng_cache_metrics
=
register_cache
(
"
string_cache
"
,
_string_cache
)
KNOWN_KEYS
=
{
key
:
key
for
key
in
(
...
...
@@ -67,14 +62,16 @@ KNOWN_KEYS = {
def
intern_string
(
string
):
"""
Takes a (potentially) unicode string and interns
using custom cache
"""
Takes a (potentially) unicode string and interns
it if it
'
s ascii
"""
new_str
=
_string_cache
.
setdefault
(
string
,
string
)
if
new_str
is
string
:
_stirng_cache_metrics
.
inc_hits
()
else
:
_stirng_cache_metrics
.
inc_misses
()
return
new_str
if
string
is
None
:
return
None
try
:
string
=
string
.
encode
(
"
ascii
"
)
return
intern
(
string
)
except
UnicodeEncodeError
:
return
string
def
intern_dict
(
dictionary
):
...
...
@@ -87,13 +84,9 @@ def intern_dict(dictionary):
def
_intern_known_values
(
key
,
value
):
intern_str_keys
=
(
"
event_id
"
,
"
room_id
"
)
intern_unicode_keys
=
(
"
sender
"
,
"
user_id
"
,
"
type
"
,
"
state_key
"
)
if
key
in
intern_str_keys
:
return
intern
(
value
.
encode
(
'
ascii
'
))
intern_keys
=
(
"
event_id
"
,
"
room_id
"
,
"
sender
"
,
"
user_id
"
,
"
type
"
,
"
state_key
"
,)
if
key
in
intern_
unicode_
keys
:
if
key
in
intern_keys
:
return
intern_string
(
value
)
return
value
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment