Commit 045a4279 authored by Kevin Modzelewski's avatar Kevin Modzelewski

Merge pull request #978 from undingen/pyxl2

Misc optimizations for pyxl2
parents d44733b1 82459763
This diff is collapsed.
......@@ -894,11 +894,12 @@ static PyObject* call_attribute(PyObject* self, PyObject* attr, PyObject* name)
/* Pyston change: static */ PyObject* slot_tp_getattr_hook(PyObject* self, PyObject* name) noexcept {
assert(name->cls == str_cls);
return slotTpGetattrHookInternal<CAPI, NOT_REWRITABLE>(self, (BoxedString*)name, NULL);
return slotTpGetattrHookInternal<CAPI, NOT_REWRITABLE>(self, (BoxedString*)name, NULL, false, NULL, NULL);
}
template <ExceptionStyle S, Rewritable rewritable>
Box* slotTpGetattrHookInternal(Box* self, BoxedString* name, GetattrRewriteArgs* rewrite_args) noexcept(S == CAPI) {
Box* slotTpGetattrHookInternal(Box* self, BoxedString* name, GetattrRewriteArgs* rewrite_args, bool for_call,
Box** bind_obj_out, RewriterVar** r_bind_obj_out) noexcept(S == CAPI) {
if (rewritable == NOT_REWRITABLE) {
assert(!rewrite_args);
rewrite_args = NULL;
......@@ -982,7 +983,8 @@ Box* slotTpGetattrHookInternal(Box* self, BoxedString* name, GetattrRewriteArgs*
GetattrRewriteArgs grewrite_args(rewrite_args->rewriter, rewrite_args->obj, rewrite_args->destination);
try {
assert(!PyType_Check(self)); // There would be a getattribute
res = getattrInternalGeneric<false, rewritable>(self, name, &grewrite_args, false, false, NULL, NULL);
res = getattrInternalGeneric<false, rewritable>(self, name, &grewrite_args, false, for_call,
bind_obj_out, r_bind_obj_out);
} catch (ExcInfo e) {
if (!e.matches(AttributeError)) {
if (S == CAPI) {
......@@ -1108,13 +1110,17 @@ Box* slotTpGetattrHookInternal(Box* self, BoxedString* name, GetattrRewriteArgs*
}
// Force instantiation of the template
template Box* slotTpGetattrHookInternal<CAPI, REWRITABLE>(Box* self, BoxedString* name,
GetattrRewriteArgs* rewrite_args);
template Box* slotTpGetattrHookInternal<CXX, REWRITABLE>(Box* self, BoxedString* name,
GetattrRewriteArgs* rewrite_args);
GetattrRewriteArgs* rewrite_args, bool for_call,
Box** bind_obj_out, RewriterVar** r_bind_obj_out);
template Box* slotTpGetattrHookInternal<CXX, REWRITABLE>(Box* self, BoxedString* name, GetattrRewriteArgs* rewrite_args,
bool for_call, Box** bind_obj_out,
RewriterVar** r_bind_obj_out);
template Box* slotTpGetattrHookInternal<CAPI, NOT_REWRITABLE>(Box* self, BoxedString* name,
GetattrRewriteArgs* rewrite_args);
GetattrRewriteArgs* rewrite_args, bool for_call,
Box** bind_obj_out, RewriterVar** r_bind_obj_out);
template Box* slotTpGetattrHookInternal<CXX, NOT_REWRITABLE>(Box* self, BoxedString* name,
GetattrRewriteArgs* rewrite_args);
GetattrRewriteArgs* rewrite_args, bool for_call,
Box** bind_obj_out, RewriterVar** r_bind_obj_out);
/* Pyston change: static */ PyObject* slot_tp_new(PyTypeObject* self, PyObject* args, PyObject* kwds) noexcept {
STAT_TIMER(t0, "us_timer_slot_tpnew", SLOT_AVOIDABILITY(self));
......
......@@ -56,7 +56,8 @@ int slot_tp_init(PyObject* self, PyObject* args, PyObject* kwds) noexcept;
class GetattrRewriteArgs;
template <ExceptionStyle S, Rewritable rewritable>
Box* slotTpGetattrHookInternal(Box* self, BoxedString* attr, GetattrRewriteArgs* rewrite_args) noexcept(S == CAPI);
Box* slotTpGetattrHookInternal(Box* self, BoxedString* attr, GetattrRewriteArgs* rewrite_args, bool for_call,
Box** bind_obj_out, RewriterVar** r_bind_obj_out) noexcept(S == CAPI);
}
#endif
......@@ -357,6 +357,8 @@ GCAllocation* SmallArena::realloc(GCAllocation* al, size_t bytes) {
#else
memcpy(rtn, al, std::min(bytes, size));
#endif
if (bytesAllocatedSinceCollection > size)
bytesAllocatedSinceCollection -= size;
free(al);
return rtn;
......@@ -772,6 +774,9 @@ GCAllocation* LargeArena::realloc(GCAllocation* al, size_t bytes) {
GCAllocation* rtn = heap->alloc(bytes);
memcpy(rtn, al, std::min(bytes, obj->size));
if (bytesAllocatedSinceCollection > size)
bytesAllocatedSinceCollection -= size;
_freeLargeObj(obj);
return rtn;
}
......@@ -994,6 +999,9 @@ GCAllocation* HugeArena::realloc(GCAllocation* al, size_t bytes) {
GCAllocation* rtn = heap->alloc(bytes);
memcpy(rtn, al, std::min(bytes, obj->size));
if (bytesAllocatedSinceCollection > obj->size)
bytesAllocatedSinceCollection -= obj->size;
_freeHugeObj(obj);
return rtn;
}
......
......@@ -1561,7 +1561,8 @@ Box* getattrInternalEx(Box* obj, BoxedString* attr, GetattrRewriteArgs* rewrite_
STAT_TIMER(t0, "us_timer_slowpath_tpgetattro", 10);
if (obj->cls->tp_getattro == slot_tp_getattr_hook) {
return slotTpGetattrHookInternal<S, rewritable>(obj, attr, rewrite_args);
return slotTpGetattrHookInternal<S, rewritable>(obj, attr, rewrite_args, for_call, bind_obj_out,
r_bind_obj_out);
} else if (obj->cls->tp_getattro == instance_getattro) {
return instanceGetattroInternal<S>(obj, attr, rewrite_args);
} else if (obj->cls->tp_getattro == type_getattro) {
......
......@@ -42,6 +42,7 @@ extern "C" PyObject* string_find(PyStringObject* self, PyObject* args) noexcept;
extern "C" PyObject* string_index(PyStringObject* self, PyObject* args) noexcept;
extern "C" PyObject* string_rindex(PyStringObject* self, PyObject* args) noexcept;
extern "C" PyObject* string_rfind(PyStringObject* self, PyObject* args) noexcept;
extern "C" PyObject* string_replace(PyStringObject* self, PyObject* args) noexcept;
extern "C" PyObject* string_splitlines(PyStringObject* self, PyObject* args) noexcept;
extern "C" PyObject* string__format__(PyObject* self, PyObject* args) noexcept;
......@@ -1839,53 +1840,6 @@ extern "C" PyObject* _PyString_Join(PyObject* sep, PyObject* x) noexcept {
return string_join((PyStringObject*)sep, x);
}
Box* strReplace(Box* _self, Box* _old, Box* _new, Box** _args) {
if (!PyString_Check(_self))
raiseExcHelper(TypeError, "descriptor 'replace' requires a 'str' object but received a '%s'",
getTypeName(_self));
BoxedString* self = static_cast<BoxedString*>(_self);
#ifdef Py_USING_UNICODE
if (PyUnicode_Check(_old) || PyUnicode_Check(_new))
return PyUnicode_Replace((PyObject*)self, _old, _new, -1 /*count*/);
#endif
if (!PyString_Check(_old))
raiseExcHelper(TypeError, "expected a character buffer object");
BoxedString* old = static_cast<BoxedString*>(_old);
if (!PyString_Check(_new))
raiseExcHelper(TypeError, "expected a character buffer object");
BoxedString* new_ = static_cast<BoxedString*>(_new);
Box* _maxreplace = _args[0];
if (!PyInt_Check(_maxreplace))
raiseExcHelper(TypeError, "an integer is required");
int max_replaces = static_cast<BoxedInt*>(_maxreplace)->n;
size_t start_pos = 0;
std::string s = self->s();
bool single_char = old->size() == 1;
int num_replaced = 0;
for (; num_replaced < max_replaces || max_replaces < 0; ++num_replaced) {
if (single_char)
start_pos = s.find(old->s()[0], start_pos);
else
start_pos = s.find(old->s(), start_pos);
if (start_pos == std::string::npos)
break;
s.replace(start_pos, old->size(), new_->s());
start_pos += new_->size(); // Handles case where 'to' is a substring of 'from'
}
if (num_replaced == 0 && self->cls == str_cls)
return self;
return boxString(s);
}
Box* strPartition(BoxedString* self, BoxedString* sep) {
RELEASE_ASSERT(PyString_Check(self), "");
RELEASE_ASSERT(PyString_Check(sep), "");
......@@ -2826,6 +2780,7 @@ static PyMethodDef string_methods[] = {
{ "rindex", (PyCFunction)string_rindex, METH_VARARGS, NULL },
{ "rfind", (PyCFunction)string_rfind, METH_VARARGS, NULL },
{ "expandtabs", (PyCFunction)string_expandtabs, METH_VARARGS, NULL },
{ "replace", (PyCFunction)string_replace, METH_O3 | METH_D1, NULL },
{ "splitlines", (PyCFunction)string_splitlines, METH_VARARGS, NULL },
{ "zfill", (PyCFunction)string_zfill, METH_VARARGS, NULL },
{ "__format__", (PyCFunction)string__format__, METH_VARARGS, NULL },
......@@ -2927,9 +2882,6 @@ void setupStr() {
str_cls->giveAttr("__iter__", new BoxedFunction(boxRTFunction((void*)strIter, typeFromClass(str_iterator_cls), 1)));
str_cls->giveAttr("replace",
new BoxedFunction(boxRTFunction((void*)strReplace, UNKNOWN, 4, false, false), { boxInt(-1) }));
for (auto& md : string_methods) {
str_cls->giveAttr(md.ml_name, new BoxedMethodDescriptor(&md, str_cls));
}
......
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