Skip to content

Commit

Permalink
Merge pull request #224 from sio2project/performance-improvments
Browse files Browse the repository at this point in the history
Performance improvements
  • Loading branch information
MasloMaslane authored Apr 5, 2024
2 parents 8075949 + fcf9386 commit 1669bb7
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 28 deletions.
22 changes: 12 additions & 10 deletions src/sinol_make/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,28 +50,30 @@ def main_exn():
parser = configure_parsers()
arguments = []
curr_args = []
commands = util.get_commands()
commands_dict = {command.get_name(): command for command in commands}
for arg in sys.argv[1:]:
if arg in util.get_command_names() and not (len(curr_args) > 0 and curr_args[0] == 'init'):
if arg in commands_dict.keys() and not (len(curr_args) > 0 and curr_args[0] == 'init'):
if curr_args:
arguments.append(curr_args)
curr_args = [arg]
else:
curr_args.append(arg)
if curr_args:
arguments.append(curr_args)
commands = util.get_commands()
if not arguments:
parser.print_help()
exit(1)
check_oiejq()

for curr_args in arguments:
args = parser.parse_args(curr_args)
command_found = False
for command in commands:
if command.get_name() == args.command:
command_found = True
if len(arguments) > 1:
print(f' {command.get_name()} command '.center(util.get_terminal_size()[1], '='))
command.run(args)
if not command_found:
command = commands_dict.get(args.command, None)
if command:
if len(arguments) > 1:
print(f' {command.get_name()} command '.center(util.get_terminal_size()[1], '='))
command.run(args)
else:
parser.print_help()
exit(1)

Expand Down
17 changes: 11 additions & 6 deletions src/sinol_make/helpers/compiler.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from typing import List

import subprocess
import argparse
import sys
import os

import sinol_make.util as util
import sys, subprocess

from sinol_make import util
from sinol_make.structs.compiler_structs import Compilers
from sinol_make.helpers.func_cache import cache_result


def check_if_installed(compiler):
Expand All @@ -22,6 +22,7 @@ def check_if_installed(compiler):
return True


@cache_result()
def get_c_compiler_path():
"""
Get the C compiler
Expand All @@ -41,6 +42,7 @@ def get_c_compiler_path():
return None


@cache_result()
def get_cpp_compiler_path():
"""
Get the C++ compiler
Expand All @@ -62,6 +64,7 @@ def get_cpp_compiler_path():
return None


@cache_result()
def get_python_interpreter_path():
"""
Get the Python interpreter
Expand All @@ -75,6 +78,7 @@ def get_python_interpreter_path():
return None


@cache_result()
def get_java_compiler_path():
"""
Get the Java compiler
Expand All @@ -94,7 +98,8 @@ def get_default_compilers():
c_compiler_path=get_c_compiler_path(),
cpp_compiler_path=get_cpp_compiler_path(),
python_interpreter_path=get_python_interpreter_path(),
java_compiler_path=get_java_compiler_path()
# Java is not currently supported by sinol-make
# java_compiler_path=get_java_compiler_path()
)


Expand Down Expand Up @@ -141,5 +146,5 @@ def verify_compilers(args: argparse.Namespace, solutions: List[str]) -> Compiler
c_compiler_path=args.c_compiler_path,
cpp_compiler_path=args.cpp_compiler_path,
python_interpreter_path=args.python_interpreter_path,
java_compiler_path=args.java_compiler_path
java_compiler_path=None
)
30 changes: 30 additions & 0 deletions src/sinol_make/helpers/func_cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import os

__cache = {}


def cache_result(cwd=False):
"""
Function to cache the result of a function.
"""
def decorator(func):
def wrapper(*args, **kwargs):
if cwd:
key = (func.__name__, os.getcwd())
else:
key = func.__name__

if key in __cache:
return __cache[key]
result = func(*args, **kwargs)
__cache[key] = result
return result
return wrapper
return decorator


def clear_cache():
"""
Function to clear the cache.
"""
__cache.clear()
2 changes: 2 additions & 0 deletions src/sinol_make/helpers/package_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
from enum import Enum
from typing import List, Union, Dict, Any

from sinol_make.helpers.func_cache import cache_result
from sinol_make import util
from sinol_make.helpers import paths


@cache_result(cwd=True)
def get_task_id() -> str:
config = get_config()
if "sinol_task_id" in config:
Expand Down
6 changes: 3 additions & 3 deletions src/sinol_make/helpers/parsers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import sys

import argparse

from sinol_make.helpers import compiler
Expand All @@ -20,8 +19,9 @@ def add_compilation_arguments(parser: argparse.ArgumentParser):
parser.add_argument('--python-interpreter-path', dest='python_interpreter_path', type=str,
default=compiler.get_python_interpreter_path(),
help='Python interpreter to use (default: python3)')
parser.add_argument('--java-compiler-path', dest='java_compiler_path', type=str,
default=compiler.get_java_compiler_path(), help='Java compiler to use (default: javac)')
# Java is not currently supported by sinol-make
# parser.add_argument('--java-compiler-path', dest='java_compiler_path', type=str,
# default=compiler.get_java_compiler_path(), help='Java compiler to use (default: javac)')
parser.add_argument('--compile-mode', '-C', dest='compile_mode', choices=['default', 'oioioi', 'weak', 'd', 'o', 'w'],
help='Warning flag groups used to compile C/C++ files. Available options:\n'
' default / d - uses default flags: \n'
Expand Down
10 changes: 2 additions & 8 deletions src/sinol_make/util.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import glob, importlib, os, sys, requests, yaml
import math
import multiprocessing
import platform
import tarfile
import hashlib
Expand All @@ -11,19 +10,15 @@

from sinol_make.contest_types import get_contest_type
from sinol_make.helpers import paths, cache
from sinol_make.helpers.func_cache import cache_result
from sinol_make.structs.status_structs import Status


__cache = {}


@cache_result()
def get_commands():
"""
Function to get an array of all available commands.
"""
global __cache
if 'commands' in __cache:
return __cache['commands']
commands_path = glob.glob(
os.path.join(
os.path.dirname(os.path.realpath(__file__)),
Expand All @@ -35,7 +30,6 @@ def get_commands():
temp = importlib.import_module('sinol_make.commands.' + os.path.basename(path), 'Command')
commands.append(temp.Command())

__cache['commands'] = commands
return commands


Expand Down
3 changes: 2 additions & 1 deletion tests/helpers/test_package_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from ..commands.run.util import create_ins
from ..fixtures import *
from tests import util
from sinol_make.helpers import package_util
from sinol_make.helpers import package_util, func_cache


@pytest.mark.parametrize("create_package", [util.get_long_name_package_path()], indirect=True)
Expand All @@ -12,6 +12,7 @@ def test_get_task_id(create_package):
assert package_util.get_task_id() == "lpn"
with open(os.path.join(package_path, "config.yml"), "w") as config_file:
config_file.write("title: Long package name\n")
func_cache.clear_cache()
with pytest.raises(SystemExit):
package_util.get_task_id()

Expand Down
1 change: 1 addition & 0 deletions tests/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ def create_ins_outs(package_path):
"""
os.chdir(package_path)
task_id = package_util.get_task_id()
print(task_id)
create_ins(package_path, task_id)
has_lib = package_util.any_files_matching_pattern(task_id, f"{task_id}lib.*")
if not has_lib:
Expand Down

0 comments on commit 1669bb7

Please sign in to comment.