summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFlorian Pritz <bluewind@xinu.at>2015-04-02 17:22:40 +0200
committerFlorian Pritz <bluewind@xinu.at>2015-04-02 17:39:45 +0200
commit6d0057b13ced5a7991ad1b921c4c208895276a2e (patch)
tree3d8222f0f8407a560cf9a725c6c286b96f961358
parent6d3fa5c8718b01babd26d63b53da984c6641c22c (diff)
Improve performance of pygments->mime2lexer
Normal arrays are rather slow when accessed sequentially. Use associative arrays and key lookups to reduce the render time of a multipaste with 180 items from 225ms to ~190ms. Signed-off-by: Florian Pritz <bluewind@xinu.at>
-rw-r--r--application/libraries/Pygments.php8
-rwxr-xr-xscripts/get_lexer_list.py9
2 files changed, 9 insertions, 8 deletions
diff --git a/application/libraries/Pygments.php b/application/libraries/Pygments.php
index 12f6d7037..3c99481d1 100644
--- a/application/libraries/Pygments.php
+++ b/application/libraries/Pygments.php
@@ -36,7 +36,7 @@ class Pygments {
foreach (self::get_pygments_info() as $lexer) {
$desc = $lexer['fullname'];
- $name = $lexer['names'][0];
+ $name = array_keys($lexer['names'])[0];
if ($desc == $last_desc) {
continue;
}
@@ -146,13 +146,11 @@ class Pygments {
if (array_key_exists($this->mimetype, $typearray)) return $typearray[$this->mimetype];
// fall back to pygments own list if not found in our list
- $typearray = array();
foreach (self::get_pygments_info() as $lexer) {
- foreach ($lexer['mimetypes'] as $type) {
- $typearray[$type] = $lexer['names'][0];
+ if (isset($lexer['mimetypes'][$this->mimetype])) {
+ return $lexer;
}
}
- if (array_key_exists($this->mimetype, $typearray)) return $typearray[$this->mimetype];
if (strpos($this->mimetype, 'text/') === 0) return 'text';
diff --git a/scripts/get_lexer_list.py b/scripts/get_lexer_list.py
index 0ef6fe97a..9453ff733 100755
--- a/scripts/get_lexer_list.py
+++ b/scripts/get_lexer_list.py
@@ -5,11 +5,14 @@ import json
ret = []
+def dictify(list):
+ return {k:True for k in list}
+
for fullname, names, exts, mimetypes in pygments.lexers.get_all_lexers():
ret.append({
'fullname': fullname,
- 'names': names,
- 'extentions': exts,
- 'mimetypes': mimetypes,
+ 'names': dictify(names),
+ 'extentions': dictify(exts),
+ 'mimetypes': dictify(mimetypes),
})
print(json.dumps(ret))