Commit d50f760f authored by Kevin Modzelewski's avatar Kevin Modzelewski

Convert parts of the import system to use BoxedStrings

Reduces boxing during the import process
parent 2e409bdd
...@@ -417,7 +417,7 @@ extern "C" PyObject* Py_InitModule4(const char* name, PyMethodDef* methods, cons ...@@ -417,7 +417,7 @@ extern "C" PyObject* Py_InitModule4(const char* name, PyMethodDef* methods, cons
} }
} }
BoxedModule* module = createModule(name, NULL, doc); BoxedModule* module = createModule(boxString(name), NULL, doc);
// Pass self as is, even if NULL we are not allowed to change it to None // Pass self as is, even if NULL we are not allowed to change it to None
Box* passthrough = static_cast<Box*>(self); Box* passthrough = static_cast<Box*>(self);
......
...@@ -711,8 +711,8 @@ class BoxedClass; ...@@ -711,8 +711,8 @@ class BoxedClass;
// TODO these shouldn't be here // TODO these shouldn't be here
void setupRuntime(); void setupRuntime();
void teardownRuntime(); void teardownRuntime();
Box* createAndRunModule(const std::string& name, const std::string& fn); Box* createAndRunModule(BoxedString* name, const std::string& fn);
BoxedModule* createModule(const std::string& name, const char* fn = NULL, const char* doc = NULL); BoxedModule* createModule(BoxedString* name, const char* fn = NULL, const char* doc = NULL);
Box* moduleInit(BoxedModule* self, Box* name, Box* doc = NULL); Box* moduleInit(BoxedModule* self, Box* name, Box* doc = NULL);
// TODO where to put this // TODO where to put this
......
...@@ -417,7 +417,7 @@ static int main(int argc, char** argv) { ...@@ -417,7 +417,7 @@ static int main(int argc, char** argv) {
// if the user invoked `pyston -c command` // if the user invoked `pyston -c command`
if (command != NULL) { if (command != NULL) {
try { try {
main_module = createModule("__main__", "<string>"); main_module = createModule(boxString("__main__"), "<string>");
AST_Module* m = parse_string(command); AST_Module* m = parse_string(command);
compileAndRunModule(m, main_module); compileAndRunModule(m, main_module);
rtncode = 0; rtncode = 0;
...@@ -428,7 +428,7 @@ static int main(int argc, char** argv) { ...@@ -428,7 +428,7 @@ static int main(int argc, char** argv) {
} }
} else if (module != NULL) { } else if (module != NULL) {
// TODO: CPython uses the same main module for all code paths // TODO: CPython uses the same main module for all code paths
main_module = createModule("__main__", "<string>"); main_module = createModule(boxString("__main__"), "<string>");
rtncode = (RunModule(module, 1) != 0); rtncode = (RunModule(module, 1) != 0);
} else { } else {
rtncode = 0; rtncode = 0;
...@@ -455,7 +455,7 @@ static int main(int argc, char** argv) { ...@@ -455,7 +455,7 @@ static int main(int argc, char** argv) {
prependToSysPath(real_path); prependToSysPath(real_path);
free(real_path); free(real_path);
main_module = createModule("__main__", fn); main_module = createModule(boxString("__main__"), fn);
try { try {
AST_Module* ast = caching_parse_file(fn); AST_Module* ast = caching_parse_file(fn);
compileAndRunModule(ast, main_module); compileAndRunModule(ast, main_module);
...@@ -474,7 +474,7 @@ static int main(int argc, char** argv) { ...@@ -474,7 +474,7 @@ static int main(int argc, char** argv) {
Py_InspectFlag = 0; Py_InspectFlag = 0;
if (!main_module) { if (!main_module) {
main_module = createModule("__main__", "<stdin>"); main_module = createModule(boxString("__main__"), "<stdin>");
} else { } else {
// main_module->fn = "<stdin>"; // main_module->fn = "<stdin>";
} }
......
...@@ -66,7 +66,7 @@ extern "C" int PyAST_Check(PyObject* o) noexcept { ...@@ -66,7 +66,7 @@ extern "C" int PyAST_Check(PyObject* o) noexcept {
} }
void setupAST() { void setupAST() {
BoxedModule* ast_module = createModule("_ast", "__builtin__"); BoxedModule* ast_module = createModule(boxString("_ast"), "__builtin__");
ast_module->giveAttr("PyCF_ONLY_AST", boxInt(PyCF_ONLY_AST)); ast_module->giveAttr("PyCF_ONLY_AST", boxInt(PyCF_ONLY_AST));
......
...@@ -1449,9 +1449,9 @@ Box* builtinFormat(Box* value, Box* format_spec) { ...@@ -1449,9 +1449,9 @@ Box* builtinFormat(Box* value, Box* format_spec) {
} }
void setupBuiltins() { void setupBuiltins() {
builtins_module builtins_module = createModule(boxString("__builtin__"), NULL,
= createModule("__builtin__", NULL, "Built-in functions, exceptions, and other objects.\n\nNoteworthy: None is " "Built-in functions, exceptions, and other objects.\n\nNoteworthy: None is "
"the `nil' object; Ellipsis represents `...' in slices."); "the `nil' object; Ellipsis represents `...' in slices.");
BoxedHeapClass* ellipsis_cls BoxedHeapClass* ellipsis_cls
= BoxedHeapClass::create(type_cls, object_cls, NULL, 0, 0, sizeof(Box), false, "ellipsis"); = BoxedHeapClass::create(type_cls, object_cls, NULL, 0, 0, sizeof(Box), false, "ellipsis");
......
...@@ -43,7 +43,7 @@ static Box* enable() { ...@@ -43,7 +43,7 @@ static Box* enable() {
} }
void setupGC() { void setupGC() {
BoxedModule* gc_module = createModule("gc"); BoxedModule* gc_module = createModule(boxString("gc"));
gc_module->giveAttr("collect", gc_module->giveAttr("collect",
new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)gcCollect, NONE, 0), "collect")); new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)gcCollect, NONE, 0), "collect"));
......
...@@ -64,7 +64,7 @@ static Box* dumpStats(Box* includeZeros) { ...@@ -64,7 +64,7 @@ static Box* dumpStats(Box* includeZeros) {
} }
void setupPyston() { void setupPyston() {
pyston_module = createModule("__pyston__"); pyston_module = createModule(boxString("__pyston__"));
pyston_module->giveAttr("setOption", pyston_module->giveAttr("setOption",
new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)setOption, UNKNOWN, 2), "setOption")); new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)setOption, UNKNOWN, 2), "setOption"));
......
...@@ -586,7 +586,7 @@ void setupSys() { ...@@ -586,7 +586,7 @@ void setupSys() {
gc::registerPermanentRoot(sys_modules_dict); gc::registerPermanentRoot(sys_modules_dict);
// This is ok to call here because we've already created the sys_modules_dict // This is ok to call here because we've already created the sys_modules_dict
sys_module = createModule("sys"); sys_module = createModule(boxString("sys"));
sys_module->giveAttr("modules", sys_modules_dict); sys_module->giveAttr("modules", sys_modules_dict);
......
This diff is collapsed.
...@@ -21,7 +21,7 @@ namespace pyston { ...@@ -21,7 +21,7 @@ namespace pyston {
extern "C" Box* import(int level, Box* from_imports, llvm::StringRef module_name); extern "C" Box* import(int level, Box* from_imports, llvm::StringRef module_name);
extern Box* importModuleLevel(llvm::StringRef module_name, Box* globals, Box* from_imports, int level); extern Box* importModuleLevel(llvm::StringRef module_name, Box* globals, Box* from_imports, int level);
BoxedModule* importCExtension(const std::string& full_name, const std::string& last_name, const std::string& path); BoxedModule* importCExtension(BoxedString* full_name, const std::string& last_name, const std::string& path);
} }
#endif #endif
...@@ -3816,26 +3816,25 @@ void setupRuntime() { ...@@ -3816,26 +3816,25 @@ void setupRuntime() {
TRACK_ALLOCATIONS = true; TRACK_ALLOCATIONS = true;
} }
BoxedModule* createModule(const std::string& name, const char* fn, const char* doc) { BoxedModule* createModule(BoxedString* name, const char* fn, const char* doc) {
assert((!fn || strlen(fn)) && "probably wanted to set the fn to <stdin>?"); assert((!fn || strlen(fn)) && "probably wanted to set the fn to <stdin>?");
BoxedDict* d = getSysModulesDict(); BoxedDict* d = getSysModulesDict();
Box* b_name = boxString(name);
// Surprisingly, there are times that we need to return the existing module if // Surprisingly, there are times that we need to return the existing module if
// one exists: // one exists:
Box* existing = d->getOrNull(b_name); Box* existing = d->getOrNull(name);
if (existing && PyModule_Check(existing)) { if (existing && PyModule_Check(existing)) {
return static_cast<BoxedModule*>(existing); return static_cast<BoxedModule*>(existing);
} }
BoxedModule* module = new BoxedModule(); BoxedModule* module = new BoxedModule();
moduleInit(module, boxString(name), boxString(doc ? doc : "")); moduleInit(module, name, boxString(doc ? doc : ""));
if (fn) if (fn)
module->giveAttr("__file__", boxString(fn)); module->giveAttr("__file__", boxString(fn));
d->d[b_name] = module; d->d[name] = module;
if (name == "__main__") if (name->s() == "__main__")
module->giveAttr("__builtins__", builtins_module); module->giveAttr("__builtins__", builtins_module);
return module; return module;
} }
......
...@@ -40,7 +40,7 @@ TEST_F(AnalysisTest, augassign) { ...@@ -40,7 +40,7 @@ TEST_F(AnalysisTest, augassign) {
FutureFlags future_flags = getFutureFlags(module->body, fn.c_str()); FutureFlags future_flags = getFutureFlags(module->body, fn.c_str());
SourceInfo* si = new SourceInfo(createModule("augassign", fn.c_str()), scoping, future_flags, func, SourceInfo* si = new SourceInfo(createModule(boxString("augassign"), fn.c_str()), scoping, future_flags, func,
func->body, boxString(fn)); func->body, boxString(fn));
CFG* cfg = computeCFG(si, func->body); CFG* cfg = computeCFG(si, func->body);
...@@ -70,7 +70,7 @@ void doOsrTest(bool is_osr, bool i_maybe_undefined) { ...@@ -70,7 +70,7 @@ void doOsrTest(bool is_osr, bool i_maybe_undefined) {
FutureFlags future_flags = getFutureFlags(module->body, fn.c_str()); FutureFlags future_flags = getFutureFlags(module->body, fn.c_str());
ScopeInfo* scope_info = scoping->getScopeInfoForNode(func); ScopeInfo* scope_info = scoping->getScopeInfoForNode(func);
std::unique_ptr<SourceInfo> si(new SourceInfo(createModule("osr" + std::to_string((is_osr << 1) + i_maybe_undefined), std::unique_ptr<SourceInfo> si(new SourceInfo(createModule(boxString("osr" + std::to_string((is_osr << 1) + i_maybe_undefined)),
fn.c_str()), scoping, future_flags, func, func->body, boxString(fn))); fn.c_str()), scoping, future_flags, func, func->body, boxString(fn)));
CLFunction* clfunc = new CLFunction(0, 0, false, false, std::move(si)); CLFunction* clfunc = new CLFunction(0, 0, false, false, std::move(si));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment