Skip to content

Commit cd3230b

Browse files
committed
use batch
1 parent af7c8f3 commit cd3230b

File tree

3 files changed

+62
-9
lines changed

3 files changed

+62
-9
lines changed

elixir/lib.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@
2626

2727
CURRENT_DIR = os.path.abspath(os.path.dirname(os.path.abspath(__file__)) + '/../')
2828

29-
def script(*args, env=None):
29+
def script(*args, input=None, env=None):
3030
args = (os.path.join(CURRENT_DIR, 'script.sh'),) + args
3131
# subprocess.run was introduced in Python 3.5
3232
# fall back to subprocess.check_output if it's not available
3333
if hasattr(subprocess, 'run'):
34-
p = subprocess.run(args, stdout=subprocess.PIPE, env=env)
34+
p = subprocess.run(args, stdout=subprocess.PIPE, input=input, env=env)
3535
p = p.stdout
3636
else:
37-
p = subprocess.check_output(args)
37+
p = subprocess.check_output(args, input=input)
3838
return p
3939

4040
def run_cmd(*args, env=None):

elixir/query.py

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ def __init__(self, data_dir, repo_dir):
6363
self.db = data.DB(data_dir, readonly=True, dtscomp=self.dts_comp_support)
6464
self.file_cache = {}
6565

66-
def script(self, *args):
67-
return script(*args, env=self.getEnv())
66+
def script(self, *args, input=None):
67+
return script(*args, input=input, env=self.getEnv())
6868

6969
def scriptLines(self, *args):
7070
return scriptLines(*args, env=self.getEnv())
@@ -403,20 +403,41 @@ def get_idents_defs(self, version, ident, family):
403403
symbol_doccomments.append(SymbolInstance(path, docline))
404404

405405
return symbol_definitions, symbol_references, symbol_doccomments
406-
406+
407+
def get_files_and_zip(self, version, syms):
408+
batch = b"\n".join([f"{version}:{sym.path}".encode() for sym in syms])
409+
batch_res = self.script('get-files-batch', input=batch)
410+
411+
# See https://git-scm.com/docs/git-cat-file#_batch_output for the format:
412+
#
413+
# <oid> SP <type> SP <size> LF
414+
# <contents> LF
415+
# <oid> SP <type> SP <size> LF
416+
# <contents> LF
417+
# <oid> SP <type> SP <size> LF
418+
# <contents> LF
419+
#
420+
for sym in syms:
421+
meta, batch_res = batch_res.split(b"\n", 1)
422+
_, _, size = meta.split(b" ")
423+
size = int(size) + 1 # newline after each file
424+
content = batch_res[:size].split(b"\n")
425+
batch_res = batch_res[size:]
426+
yield sym, content
427+
428+
407429
def get_peeks_of_syms(self, version, symbol_definitions, symbol_references):
408430

409431
peeks = {}
410432

411433
def request_peeks(syms):
412434
if len(syms) > 100:
413435
return
414-
for sym in syms:
436+
437+
for sym, content in self.get_files_and_zip(version, syms):
415438
if sym.path not in peeks:
416439
peeks[sym.path] = {}
417440

418-
content = self.scriptLines('get-file', version, "/" + sym.path)
419-
420441
if type(sym.line) is int:
421442
lines = (sym.line,)
422443
else:
@@ -455,6 +476,24 @@ def cmd_file(q, version, path, **kwargs):
455476
code = q.query("file", version, path)
456477
print(code)
457478

479+
def profile(q, family, version, ident):
480+
return q.query('ident', version, ident, family)
481+
482+
def cmd_profile(q, family, version, ident, **kwargs):
483+
import cProfile
484+
cProfile.runctx('profile(q, family, version, ident)',
485+
globals={
486+
"profile": profile
487+
},
488+
locals={
489+
"q": q,
490+
"family": family,
491+
"version": version,
492+
"ident": ident,
493+
},
494+
sort='tottime'
495+
)
496+
458497
if __name__ == "__main__":
459498
import argparse
460499

@@ -473,5 +512,10 @@ def cmd_file(q, version, path, **kwargs):
473512
file_subparser.add_argument('path', type=str, help="The path of the source file")
474513
file_subparser.set_defaults(func=cmd_file, q=query)
475514

515+
profile_subparser = subparsers.add_parser('perf', help="Get a source file")
516+
profile_subparser.add_argument('ident', type=str, help="The name of the identifier")
517+
profile_subparser.add_argument('family', type=str, help="The file family requested")
518+
profile_subparser.set_defaults(func=cmd_profile, q=query)
519+
476520
args = parser.parse_args()
477521
args.func(**vars(args))

script.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ get_file()
8585
git cat-file blob "$v:`denormalize $opt2`" 2>/dev/null
8686
}
8787

88+
get_files_batch()
89+
{
90+
git cat-file --batch 2>/dev/null
91+
}
92+
8893
get_dir()
8994
{
9095
v=`echo $opt1 | version_rev`
@@ -263,6 +268,10 @@ case $cmd in
263268
get_file
264269
;;
265270

271+
get-files-batch)
272+
get_files_batch
273+
;;
274+
266275
get-dir)
267276
get_dir
268277
;;

0 commit comments

Comments
 (0)